我們在同一個表上有2個相同的(雙精度)列,其中2個相同的索引運行2個相同的查詢。但其中一個比另一個快近10倍。這是什麼原因造成的?PostgreSQL索引查詢速度不一致
1) SELECT MIN("reports"."longitude") AS min_id FROM "reports" WHERE (area2 = 18)
2) SELECT MIN("reports"."latitude") AS min_id FROM "reports" WHERE (area2 = 18)
1 28ms運行,並在> 300毫秒
這裏2個運行是 '解釋':
1)
Result (cost=6.07..6.08 rows=1 width=0)"
InitPlan 1 (returns $0)"
-> Limit (cost=0.00..6.07 rows=1 width=8)"
-> Index Scan using longitude on reports (cost=0.00..139617.49 rows=22983 width=8)"
Index Cond: (longitude IS NOT NULL)"
Filter: (area2 = 18)"
2)
Result (cost=5.95..5.96 rows=1 width=0)"
InitPlan 1 (returns $0)"
-> Limit (cost=0.00..5.95 rows=1 width=8)"
-> Index Scan using latitude on reports (cost=0.00..136754.07 rows=22983 width=8)"
Index Cond: (latitude IS NOT NULL)"
Filter: (area2 = 18)"
作爲要求這裏ED是在解釋分析輸出...
1)
Result (cost=6.07..6.08 rows=1 width=0) (actual time=10.992..10.993 rows=1 loops=1)"
InitPlan 1 (returns $0)"
-> Limit (cost=0.00..6.07 rows=1 width=8) (actual time=10.985..10.986 rows=1 loops=1)"
-> Index Scan using longitude on reports (cost=0.00..139617.49 rows=22983 width=8) (actual time=10.983..10.983 rows=1 loops=1)"
Index Cond: (longitude IS NOT NULL)"
Filter: (area2 = 18)"
Total runtime: 11.033 ms"
2)
Result (cost=5.95..5.96 rows=1 width=0) (actual time=259.749..259.749 rows=1 loops=1)"
InitPlan 1 (returns $0)"
-> Limit (cost=0.00..5.95 rows=1 width=8) (actual time=259.740..259.740 rows=1 loops=1)"
-> Index Scan using latitude on reports (cost=0.00..136754.07 rows=22983 width=8) (actual time=259.739..259.739 rows=1 loops=1)"
Index Cond: (latitude IS NOT NULL)"
Filter: (area2 = 18)"
Total runtime: 259.789 ms"
---------------------
這是怎麼回事?我怎樣才能讓第二個查詢正常運行並快速運行?據我所知,兩種設置都是相同的。
你以前做過'VACUUM FULL ANALYZE'嗎?你做了一個'EXPLAIN ANALYSE'還是一個'EXPLAIN'? – ckruse 2012-07-11 16:27:05
是否存在複合索引?請向我們展示表格定義和'explain analyze'的輸出 – wildplasser 2012-07-11 16:37:21
您應該在這樣的問題中包含確切的表格和索引定義。哪些列是NOT NULL?索引大小? VACUUM FULL ANALYZE會改變什麼嗎? 'CLUSTER報告使用idx1'和'CLUSTER報告使用idx2'會改變什麼? (我期望它!) – 2012-07-11 18:45:36