簡體   English   中英

查找Unix時間戳之間的最新差距

[英]Find a latest gap between Unix timestamps

我當前有兩個函數應該返回設備再次開始記錄的時間,即上一行之前的時間超過60秒。 這些功能可能工作正常,但我必須看到它永遠有效。 是否有任何快捷方式可以使此操作更快?

CREATE OR REPLACE FUNCTION findNextTime(startt integer)
    RETURNS integer AS
$nextTime$
DECLARE
    nextTime integer;
BEGIN
    select time into nextTime from m01 where time < startt ORDER BY time DESC LIMIT 1;
    return nextTime;
END;
$nextTime$ LANGUAGE plpgsql;

CREATE OR REPlACE FUNCTION findStart()
    RETURNS integer AS
$lastTime$
DECLARE
    currentTime integer;
    lastTime integer;
BEGIN
    select time into currentTime from m01 ORDER BY time DESC LIMIT 1;
    LOOP
        RAISE NOTICE 'Current Time: %', currentTime;
        select findNextTime(currentTime) into lastTime;
        EXIT WHEN ((currentTime - lastTime) > 60);
        currentTime := lastTime;
    END LOOP;
    return lastTime;
END;
$lastTime$ LANGUAGE plpgsql;

需要澄清的是,我基本上想找到上兩行之間的間隔超過60秒的最后一次。

CREATE TABLE IF NOT EXISTS m01 (
   time integer,
   value decimal,
   id smallint,
   driveId smallint
)

樣本數據:
在這種情況下,它將返回1520376063,因為下一個條目(1520375766)相隔60秒以上。

| time       | value              | id   | driveid |
|------------|--------------------|------|---------|
| 1520376178 | 516.2              | 5116 | 2       |
| 1520376173 | 507.8              | 5116 | 2       |
| 1520376168 | 499.5              | 5116 | 2       |
| 1520376163 | 491.1              | 5116 | 2       |
| 1520376158 | 482.90000000000003 | 5116 | 2       |
| 1520376153 | 474.5              | 5116 | 2       |
| 1520376148 | 466.20000000000005 | 5116 | 2       |
| 1520376143 | 457.8              | 5116 | 2       |
| 1520376138 | 449.5              | 5116 | 2       |
| 1520376133 | 441.20000000000005 | 5116 | 2       |
| 1520376128 | 432.90000000000003 | 5116 | 2       |
| 1520376123 | 424.6              | 5116 | 2       |
| 1520376118 | 416.20000000000005 | 5116 | 2       |
| 1520376113 | 407.8              | 5116 | 2       |
| 1520376108 | 399.5              | 5116 | 2       |
| 1520376103 | 391.20000000000005 | 5116 | 2       |
| 1520376098 | 382.90000000000003 | 5116 | 2       |
| 1520376093 | 374.5              | 5116 | 2       |
| 1520376088 | 366.20000000000005 | 5116 | 2       |
| 1520376083 | 357.8              | 5116 | 2       |
| 1520376078 | 349.5              | 5116 | 2       |
| 1520376073 | 341.20000000000005 | 5116 | 2       |
| 1520376068 | 332.90000000000003 | 5116 | 2       |
| 1520376063 | 324.5              | 5116 | 2       |
| 1520375766 | 102.5              | 5116 | 2       |

這個簡單的查詢應該替換您的兩個函數。 注意子查詢中的窗口函數lead()

SELECT *
FROM  (
   SELECT time, lead(time) OVER (ORDER BY time DESC) AS last_time
   FROM   m01
   WHERE  time < _startt
   ) sub
WHERE  time > last_time + 60
ORDER  BY time DESC
LIMIT  1;

無論哪種方式,性能的關鍵部分都是正確的索引。 理想情況下(time DESC)

假設time定義為NOT NULL可能應該這樣,但是問題中的表定義卻沒有這樣說。 另外,您可能希望ORDER BY time DESC NULLS LAST以及匹配的索引。 看到:

我希望此plpgsql函數的執行速度更快,但是,如果通常會較早出現差距:

CREATE OR REPLACE FUNCTION find_gap_before_time(_startt int)
  RETURNS int AS
$func$
DECLARE
   _current_time int;
   _last_time    int;
BEGIN
   FOR _last_time IN  -- single loop is enough!
      SELECT time
      FROM   m01
      WHERE  time < _startt
      ORDER  BY time DESC  -- NULLS LAST?
   LOOP
      IF _current_time > _last_time + 60 THEN  -- never true for 1st row
         RETURN _current_time;
      END IF;
      _current_time := _last_time;
   END LOOP;
END
$func$  LANGUAGE plpgsql;

呼叫:

SELECT find_gap_before_time(1520376200);

結果按要求。


另外:通過將列value放在最后或第一位,通常可以在存儲中每行節省幾個字節,從而最大程度地減少對齊填充。 喜歡:

CREATE TABLE m01 (
   time integer,
   id smallint,
   driveId smallint,
   value decimal
);

詳細說明:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM