是否可以优化以下查询?
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;
抱歉,这是我的第一个查询方法的解释。平等主义的答案已经很好,但也许它还可以更快?谢谢!
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)"