下面是一個postgres查詢,似乎比我預期的要長得多。 field_instances表在form_instance_id和field_id上都被索引,而form_instances表則在workflow_state上被索引。所以我認爲這將是一個快速查詢,但它需要永遠。任何人都可以幫助我解釋查詢計劃以及添加哪些索引來加速它?謝謝。如何優化這個postgresql查詢?
explain analyze
select form_id,form_instance_id,answer,field_id
from form_instances,field_instances
where workflow_state = 'DRqueued'
and form_instance_id = form_instances.id
and field_id = 'Book_EstimatedDueDate';
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hash Join (cost=8733.85..95692.90 rows=9277 width=29) (actual time=2550.000..15430.000 rows=11431 loops=1)
Hash Cond: (field_instances.form_instance_id = form_instances.id)
-> Bitmap Heap Scan on field_instances (cost=2681.11..89071.72 rows=47567 width=25) (actual time=850.000..13690.000 rows=51726 loops=1)
Recheck Cond: ((field_id)::text = 'Book_EstimatedDueDate'::text)
-> Bitmap Index Scan on index_field_instances_on_field_id (cost=0.00..2669.22 rows=47567 width=0) (actual time=830.000..830.000 rows=51729 loops=1)
Index Cond: ((field_id)::text = 'Book_EstimatedDueDate'::text)
-> Hash (cost=5911.34..5911.34 rows=11312 width=8) (actual time=1590.000..1590.000 rows=11431 loops=1)
-> Bitmap Heap Scan on form_instances (cost=511.94..5911.34 rows=11312 width=8) (actual time=720.000..1570.000 rows=11431 loops=1)
Recheck Cond: ((workflow_state)::text = 'DRqueued'::text)
-> Bitmap Index Scan on index_form_instances_on_workflow_state (cost=0.00..509.11 rows=11312 width=0) (actual time=650.000..650.000 rows=11509 loops=1)
Index Cond: ((workflow_state)::text = 'DRqueued'::text)
Total runtime: 15430.000 ms
(12 rows)
你可以嘗試像'設置ENABLE_HASHJOIN = 0;',看看是否有爲您提供更快的計劃。如果確實如此,那麼我們將繼續檢查爲什麼該計劃沒有被首先使用。 – sayap
一些事情。這是什麼版本的pg,並且您是否嘗試了一下work_mem(比如說16MB左右)?哦,我們可以得到表格的模式,還是完全合格的列名稱,當我不知道哪些列來自哪個表時,有點令人困惑。另外,你有沒有嘗試過使用顯式連接語法? (即從一個加入b(ax =) –