【发布时间】:2020-03-02 09:00:26
【问题描述】:
我在 postgresql 9.5 上有一个 sql 查询,但它需要的时间太长。然后我运行解释查询:
DELETE FROM source v1
WHERE id < (SELECT MAX(id)
FROM source v2
WHERE v2.ent_id = v1.ent_id
AND v2.name = v1.name
);
很明显是
Delete on source v1 (cost=0.00..1764410287608.21 rows=2891175 width=6)');
-> Seq Scan on source v1 (cost=0.00..1764410287608.21 rows=2891175 width=6)');
Filter: (id < (SubPlan 2))');
SubPlan 2');
-> Result (cost=203424.76..203424.77 rows=1 width=0)');
InitPlan 1 (returns $2)');
-> Limit (cost=0.43..203424.76 rows=1 width=8)');
-> Index Scan Backward using source_id_ix on source v2 (cost=0.43..813697.74 rows=4 width=8)');
Index Cond: (id IS NOT NULL)');
Filter: (((ent_id)::text = (v1.ent_id)::text) AND ((name)::text = (v1.name)::text))');
我的表有大约 8.000.000 条记录。我好几天都没有得到结果。我无法计算需要多少次?有什么新的解决办法吗?
【问题讨论】:
标签: postgresql sql-delete postgresql-9.5 postgresql-performance