是否可以優化以下查詢? webdte.docto
a是具有數百萬個條目的非常大的表,並在所有查詢的列上運行索引。最後的排序順序非常重要。使用嵌套選擇優化查詢
SELECT
id_doc,
id_tip_doc,
id_est_doc,
folios.nro_fol,
seleccionable
FROM
(
SELECT distinct(nro_fol)
FROM webdte.docto
WHERE
id_tip_doc IN
(
SELECT distinct(id_tip_doc)
FROM webdte.docto
WHERE id_doc IN
(
SELECT id_doc
FROM webdte.lib_doc
WHERE id_lib = 37
)
) AND
id_doc IN
(
SELECT id_doc
FROM webdte.lib_doc
WHERE id_lib = 37
)
) AS folios JOIN webdte.docto AS docs ON docs.nro_fol = folios.nro_fol
ORDER BY id_tip_doc, folios.nro_fol, id_est_doc;
對不起,這裏是我的拳頭查詢方法的解釋。來自Egalitarian的答案已經很好,但也許它可以更快?謝謝!
Sort (cost=13745.13..13805.42 rows=24115 width=22)"
Sort Key: docs.id_tip_doc, docto.nro_fol, docs.id_est_doc"
-> Hash Join (cost=9240.19..11492.84 rows=24115 width=22)"
Hash Cond: (docto.nro_fol = docs.nro_fol)"
-> HashAggregate (cost=4424.81..4665.91 rows=24110 width=6)"
-> Hash Semi Join (cost=733.75..4364.54 rows=24110 width=6)"
Hash Cond: (docto.id_doc = lib_doc.id_doc)"
-> Seq Scan on docto (cost=0.00..2885.28 rows=105128 width=10)"
-> Hash (cost=432.38..432.38 rows=24110 width=4)"
-> Seq Scan on lib_doc (cost=0.00..432.38 rows=24110 width=4)"
Filter: (id_lib = 37)"
-> Hash (cost=2885.28..2885.28 rows=105128 width=22)"
-> Seq Scan on docto docs (cost=0.00..2885.28 rows=105128 width=22)"
您能向我們展示EXPLAIN和EXPLAIN ANALYZE的結果嗎?如果沒有這些信息,幾乎不可能優化查詢,因爲您無法看到實際問題出在哪裏。只有猜... – 2012-07-23 09:51:29