我有一个更新查询,用于更新表中100万条记录中的大约30,000条记录。但是执行不断进行而没有给出任何回应。有人可以指导我如何优化查询以加快执行速度。 查询如下:
UPDATE ccdb.consumer_index_details ci
SET dtr_id = dsm.survey_dtr_code, fdr_id = SUBSTRING(dsm.survey_dtr_code,1,7),ss_id = SUBSTRING(dsm.survey_dtr_code,1,5), r_apdrp_town_flag = 1
FROM ccdb.dtr_stg ds, ccdb.dtr_survey_map dsm, ccdb.consumers c
WHERE ds.dtr_name = dsm.display_name
AND ci.dtr_id = ds.dtr_code
AND dsm.dtr_code = ds.dtr_code
AND ds.sec_code = '6533'
AND ci.cin = c.cin
AND c.org_unit_id = 6533
AND ci.dtr_id IN (SELECT dtr_code FROM ccdb.dtr_survey_map);
代码适用于PostgreSQL 9.2。
更新:添加了查询计划
"Update on consumer_index_details ci (cost=267.79..227219.89 rows=27759 width=628)"
" -> Nested Loop (cost=267.79..227219.89 rows=27759 width=628)"
" -> Nested Loop Semi Join (cost=267.79..65652.98 rows=1 width=622)"
" Join Filter: ((dsm.dtr_code)::text = (dtr_survey_map.dtr_code)::text)"
" -> Hash Join (cost=267.79..65625.25 rows=1 width=652)"
" Hash Cond: ((ci.dtr_id)::text = (dsm.dtr_code)::text)"
" -> Seq Scan on consumer_index_details ci (cost=0.00..57920.36 rows=1982436 width=605)"
" -> Hash (cost=267.78..267.78 rows=1 width=47)"
" -> Hash Join (cost=243.98..267.78 rows=1 width=47)"
" Hash Cond: (((dsm.display_name)::text = (ds.dtr_name)::text) AND ((dsm.dtr_code)::text = (ds.dtr_code)::text))"
" -> Seq Scan on dtr_survey_map dsm (cost=0.00..17.88 rows=788 width=41)"
" -> Hash (cost=241.41..241.41 rows=171 width=31)"
" -> Seq Scan on dtr_stg ds (cost=0.00..241.41 rows=171 width=31)"
" Filter: ((sec_code)::text = '6533'::text)"
" -> Seq Scan on dtr_survey_map (cost=0.00..17.88 rows=788 width=18)"
" -> Seq Scan on consumers c (cost=0.00..161011.73 rows=27759 width=6)"
" Filter: (org_unit_id = 6533)"