我想出了:
drop table if exists idtemp;
create temp table idtemp as
select documentid from taskflag where taskid='coref' and state = 2
order by statechanged asc limit howmany;
create unique index on idtemp(documentid);
-- trim taskflag to the first N docs ordered by coref.
delete from taskflag where documentid not in (select documentid from idtemp) ;
Run Code Online (Sandbox Code Playgroud)
当任务标志中有 120k 条记录而我保留 10k 条记录时,速度非常慢。
任务标志看起来像:
\d taskflag
Table "public.taskflag"
Column | Type | Modifiers
--------------+-----------------------------+-----------
documentid | character varying(64) | not null
taskid | character varying(64) | not null
state | smallint |
statechanged | timestamp without time zone |
Indexes:
"taskflag_pkey" PRIMARY KEY, btree (documentid, taskid)
"task_index2" btree (documentid)
"task_index4" btree (taskid, state, statechanged)
Run Code Online (Sandbox Code Playgroud)
解释说:
QUERY PLAN
----------------------------------------------------------------------------------
Delete on taskflag (cost=0.00..105811822.25 rows=223210 width=6)
-> Seq Scan on taskflag (cost=0.00..105811822.25 rows=223210 width=6)
Filter: (NOT (SubPlan 1))
SubPlan 1
-> Materialize (cost=0.00..449.00 rows=10000 width=146)
-> Seq Scan on idtemp (cost=0.00..184.00 rows=10000 width=146)
(6 rows)
Run Code Online (Sandbox Code Playgroud)
我应该安排临时表来包含我保留的表吗?
最简单的优化可能是让规划器使用哈希反连接,将查询重写为:
delete from taskflag where not exists
(select 1 from idtemp where documentid=taskflag.documentid);
Run Code Online (Sandbox Code Playgroud)
此外,您可能需要在填充临时表后立即分析它。
归档时间: |
|
查看次数: |
1169 次 |
最近记录: |