如何加速postgres sql中的批量记录删除

时间:2017-11-17 07:36:20

标签: sql postgresql

我有一个功能,删除超过1000000记录大约需要1分钟。

CREATE OR REPLACE FUNCTION DeleteRecord (cmpId integer)
RETURNS boolean AS
$BODY$
DECLARE cmpId INTEGER DEFAULT 0;
DECLARE counter INTEGER := 1 ; 
DECLARE batch    INTEGER := 0 ; 
DECLARE batchSize    INTEGER :=50000 ; 
BEGIN

     SELECT count(*)/batchSize INTO batch FROM department WHERE cmpId= cmpId;
     WHILE counter <= batch + 1 LOOP
          DELETE FROM department WHERE deptId IN ( SELECT deptId FROM
           department WHERE cmpId = cmpId limit batchSize); 
          RAISE NOTICE 'Counter: %', counter;
          counter := counter + 1 ; 
       END LOOP ; 

     counter := 1 ;
     SELECT count(*)/batchSize INTO batch FROM employee WHERE cmpId = cmpId;
     WHILE counter <= batch + 1 LOOP
          DELETE FROM empoyee WHERE empId IN ( SELECT empId FROM
           employee  WHERE cmpId = cmpId limit batchSize);  
          RAISE NOTICE 'Counter: %', counter;
          counter := counter + 1 ; 
       END LOOP ; 

END; 
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION DeleteRecord(integer)
  OWNER TO postgres;

部门表持有cmpId的500000记录

员工表为cmpId持有更多500000条记录

我尝试删除简单删除,但花了超过1分钟。

我找到了一个链接 How do I speed up deletes from a large database table?

告诉你批量删除操作。

但它确实有助于将更多时间缩短到2到3秒。

1 个答案:

答案 0 :(得分:1)

你可以做很多事情来加速大量删除。

如果经常发生检查点并且存储速度更快,则增加max_wal_size

如果您经常遇到这种问题,请考虑对您的表进行分区。删除分区比批量删除要快得多。