我想要删除超过3年的记录,但我们正在谈论超过500万条记录。 所以我正在考虑分批运行删除10000,之后提交并循环直到没有剩余的记录要删除。 关于我目前如何接近这个的示例函数如下。不幸的是,对于大型结果集来说并不理想,并且回滚的时间成本会很高。建议将不胜感激。
CREATE OR REPLACE FUNCTION delrecords()
RETURNS VOID AS $$
DECLARE
p_pid del_cursor_result.p_id%TYPE;
p_cid del_cursor_result.c_id%TYPE;
pcCursor CURSOR FOR
SELECT
P.PID, C.CID FROM mytab1 P
INNER JOIN
mytab2 AS C ON
P.PID=C.PID
WHERE
(now()::date - c.my_date::date)/365 > 3
BEGIN
OPEN pcCursor;
LOOP
FETCH pcCursor INTO p_pid,p_cid;
EXIT WHEN NOT FOUND;
DELETE FROM mytab3 WHERE pid = p_pid and cid = p_cid;
DELETE FROM mytab4 WHERE pid = p_pid and cid = p_cid;
END LOOP;
CLOSE pcCursor;
END;
$$ LANGUAGE plpgsql;