查询优化

时间:2011-05-11 06:57:41

标签: postgresql

我在表格中有50964218条记录。我将从此表中获取数据并插入到同一个表中。这需要更多的时间来操纵。如何优化此查询。

查询

INSERT INTO contacts_lists (contact_id, list_id, is_excluded, added_by_search)
SELECT contact_id, 68114 , TRUE, added_by_search
FROM contacts_lists cl1
WHERE list_id = 67579
AND is_excluded = TRUE
AND NOT EXISTS 
    (SELECT 1 FROM contacts_lists cl2 
     WHERE cl1.contact_id = cl2.contact_id AND cl2.list_id = 68114 )

index:list_id,contact_id

1 个答案:

答案 0 :(得分:1)

使用左连接可能会获得更好的结果:

select t1.[field], ...
from t1
left join t2
on [conditions]
where t2.[any pkey field] is null;