查询工作非常缓慢

时间:2018-07-25 06:53:03

标签: postgresql

我有两个表:

表格表格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中查找丢失的行,计算增量并将丢失的行插入到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;

函数操作tableB之后将如下:

+----+---------+---------+---------------------+--------+------------+-------+
| 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 |  ..   |
+----+---------+---------+---------------------+--------+------------+-------+

为什么我不能使用INSERTSELECTFROM的主要问题是在tableA中可能有几行必须与相同的列值相加columnA,columnB和record_date列的最小时间差,例如:

+----+---------+---------+---------------------+--------+
| 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  |
+----+---------+---------+---------------------+--------+

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

0 个答案:

没有答案