postgres函数更新大表和删除性能

时间:2019-03-06 08:59:41

标签: postgresql performance cpu-usage postgresql-9.3

postgres版本:9.3 postgres.conf:所有默认配置

我有2个表,A和B,都有100万行。 有一个postgres函数每2秒执行一次,它将更新表A中的数组(数组大小= 20)中的id,然后删除表B中的行。 数据库功能如下:

CREATE OR REPLACE FUNCTION test_function (ids NUMERIC[])
RETURNS void AS $$
BEGIN
  UPDATE A a
  SET status = 'begin', end_time = (NOW() AT TIME ZONE 'UTC')
  WHERE a.id = ANY (ids);

  DELETE FROM B b
  WHERE b.aid = ANY (ids)
    AND b.status = 'end';
END;
$$ LANGUAGE plpgsql;

分析如下:

explain(ANALYZE,BUFFERS,VERBOSE) select test_function('{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}');

查询计划

Result  (cost=0.00..0.26 rows=1 width=0) (actual time=14030.435..14030.436 rows=1 loops=1)
   Output: test_function('{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}'::numeric[])
   Buffers: shared hit=24297 read=26137 dirtied=20
 Total runtime: 14030.444 ms
(4 rows)

我的问题是:

  1. 在生产环境中,为什么此功能需要在成功之前最多执行7秒;
  2. 执行此功能时,此过程将占用60%的时间。 ->这是关键问题

编辑: 分析每个单独的sql:

explain(ANALYZE,VERBOSE,BUFFERS) UPDATE A a SET status = 'begin', 
end_time = (now()) WHERE a.id = ANY 
('{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}');

                                                                           QUERY PLAN                                                                               

 Update on public.A a  (cost=0.45..99.31 rows=20 width=143) (actual time=1.206..1.206 rows=0 loops=1)
   Buffers: shared hit=206 read=27 dirtied=30
   ->  Index Scan using A_pkey on public.a a  (cost=0.45..99.31 rows=20 width=143) (actual time=0.019..0.116 rows=19 loops=1)
         Output: id, start_time, now(), 'begin'::character varying(255), xxxx... ctid
         Index Cond: (t.id = ANY('{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}'::integer[]))
         Buffers: shared hit=75 read=11
 Trigger test_trigger: time=5227.111 calls=1
 Total runtime: 5228.357 ms
(8 rows)
 explain(ANALYZE,BUFFERS,VERBOSE) DELETE FROM 
B b WHERE tq.aid = ANY 
('{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}');
                                                            QUERY PLAN                                                                
 Delete on B b  (cost=0.00..1239.11 rows=20 width=6) (actual time=6.013..6.013 rows=0 loops=1)
   Buffers: shared hit=448
   ->  Seq Scan on B b (cost=0.00..1239.11 rows=20 width=6) (actual time=6.011..6.011 rows=0 loops=1)
         Output: ctid
         Filter: (b.aid = ANY ('{2,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}'::bigint[]))
         Rows Removed by Filter: 21743
         Buffers: shared hit=448
 Total runtime: 6.029 ms
(8 rows)

CPU使用率

在致电之前: enter image description here

经常进行以下操作: enter image description here

0 个答案:

没有答案