查找 Unix 时间戳之间的最新差距

Find a latest gap between Unix timestamps

我目前有两个函数应该 return 设备再次开始记录的时间,即前一行距离超过 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
)

示例数据:
在这种情况下,它将 return 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       |

这个简单的查询应该可以替换您的两个函数。注意子查询中的window function 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 - 它可能 应该 是,但是问题中的 table 定义并没有这么说.否则你可能想要 ORDER BY time DESC NULLS LAST - 和一个匹配的索引。参见:

  • PostgreSQL sort by datetime asc, null first?

我希望这个 plpgsql 函数执行得更快,但是,如果间隙通常出现 early:

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
);

详细解释:

  • Calculating and saving space in PostgreSQL