Postgresql 查询速度非常慢

Postgresql 查询速度非常慢,postgresql,Postgresql,我有两张桌子: 表A +----+---------+---------+---------------------+--------+ | id | columnA | columnB | record_date | result | +----+---------+---------+---------------------+--------+ | 1 | 1111 | 1317 | 23.07.2018 02:11:01 | 538 | | 2 |

我有两张桌子:

表A

+----+---------+---------+---------------------+--------+
| id | columnA | columnB |     record_date     | result |
+----+---------+---------+---------------------+--------+
|  1 |   1111  |   1317  | 23.07.2018 02:11:01 |   538  |
|  2 |   0411  |   1317  | 23.07.2018 02:11:01 |     0  |
|  3 |   1111  |   1317  | 24.07.2018 13:08:17 |    13  |
|  4 |   0411  |   1317  | 24.07.2018 13:08:17 |   120  |
|  5 |   0506  |   1317  | 24.07.2018 13:08:17 |    17  |
|  6 |   0273  |   1317  | 24.07.2018 13:08:17 |  3256  |
| .. |     ..  |     ..  |          ..         |    ..  |
+----+---------+---------+---------------------+--------+
表B

+----+---------+---------+---------------------+--------+------------+-------+
| id | columnA | columnB |     record_date     | result |  date_add  | delta |
+----+---------+---------+---------------------+--------+------------+-------+
|  1 |   1111  |   1317  | 23.07.2018 02:11:01 |   538  | 23.07.2018 |   0   |
|  2 |   0411  |   1317  | 23.07.2018 02:11:01 |     0  | 23.07.2018 |   0   |
| .. |     ..  |     ..  |          ..         |    ..  |     ..     |  ..   |
+----+---------+---------+---------------------+--------+------------+-------+
两个表都有字段索引:columnA、columnB、record_date

我编写了一个函数,用于查找
tableB
中缺少的行,计算delta并将缺少的行插入
tableB
,但是对于大数据集(超过10000行),该函数的运行速度非常慢。如何提高函数的性能

以下是我的功能:

declare
  delta numeric(38,0);
  lastresult numeric(38,0);
  row record;

begin
  for row in select A.*
    from tableA as A
    where not exists (select 1 
                      from tableB B
                      where B.id = A.id
                      order by A.record_date) loop
    select B.result into lastresult
    from tableB B
    group by B.result, B.columnA, B.columnB, B.record_date 
    having row.columnA = B.columnA 
        and row.columnB = B.columnB 
        and row.record_date > B.record_date
    order by B.record_date desc
    limit 1;
    if lastresult is null then 
        delta = 0;
    else
        delta = row.result - lastresult;
    end if;

    lastresult := null;

    insert into tableB select
        row.id,
        row.columnA,
        row.columnB,
        row.record_date,
        row.result,
        now(),
        delta; 

  end loop;
end;
功能操作后
表B
如下:

+----+---------+---------+---------------------+--------+------------+-------+
| id | columnA | columnB |     record_date     | result |  date_add  | delta |
+----+---------+---------+---------------------+--------+------------+-------+
|  1 |   1111  |   1317  | 23.07.2018 02:11:01 |   538  | 23.07.2018 |   0   |
|  2 |   0411  |   1317  | 23.07.2018 02:11:01 |     0  | 23.07.2018 |   0   |
|  3 |   1111  |   1317  | 24.07.2018 13:08:17 |    13  | 24.07.2018 | -525  |
|  4 |   0411  |   1317  | 24.07.2018 13:08:17 |   120  | 24.07.2018 |  120  |
|  5 |   0506  |   1317  | 24.07.2018 13:08:17 |    17  | 24.07.2018 |   0   |
|  6 |   0273  |   1317  | 24.07.2018 13:08:17 |  3256  | 24.07.2018 |   0   |
| .. |     ..  |     ..  |         ..          |    ..  | 24.07.2018 |  ..   |
+----+---------+---------+---------------------+--------+------------+-------+
我不能使用
INSERT
SELECT
FROM
的主要问题是,在
表A
中,可能有几行必须添加相同值的列columnA、columnB和列记录日期的最小时差,如下所示:

+----+---------+---------+---------------------+--------+
| id | columnA | columnB |     record_date     | result |
+----+---------+---------+---------------------+--------+
| .. |     ..  |     ..  |           ..        |    ..  |
|  9 |   3456  |   1317  | 24.07.2018 12:55:57 |   324  |
| 10 |   3456  |   1317  | 24.07.2018 13:08:17 |    13  |
+----+---------+---------+---------------------+--------+

必须将这两行都添加到
表B
中,并且必须计算增量。

给定示例数据,应该在
表B
中插入什么内容?假设表B中缺少id=4,那么增量列应该得到什么值?假设缺少另一行(id=100,columna=0411,columnb=1317,result=200),该行的delta的值应该是多少?为什么delta存储在单独的表中?看起来你可以完全移除tablea。我认为您可以用一个
插入来替换函数(从而替换循环)。。。选择。。。来自
语句,这将是非常多的faster@a_horse_with_no_name我更新了问题,我认为使用
lag()
计算单个语句中的增量应该是possible@a_horse_with_no_name你能展示一下它应该是什么样子吗?