我有一個關於(language_id,state)索引的單詞表。下面是解釋結果分析一下:在Postgres中使用LIMIT時不使用索引
無限制
explain analyze SELECT "words".* FROM "words" WHERE (words.language_id = 27) AND (state IS NULL);
Bitmap Heap Scan on words (cost=10800.38..134324.10 rows=441257 width=96) (actual time=233.257..416.026 rows=540556 loops=1)
Recheck Cond: ((language_id = 27) AND (state IS NULL))
-> Bitmap Index Scan on ls (cost=0.00..10690.07 rows=441257 width=0) (actual time=230.849..230.849 rows=540556 loops=1)
Index Cond: ((language_id = 27) AND (state IS NULL))
Total runtime: 460.277 ms
(5 rows)
限額100
explain analyze SELECT "words".* FROM "words" WHERE (words.language_id = 27) AND (state IS NULL) LIMIT 100;
Limit (cost=0.00..51.66 rows=100 width=96) (actual time=0.081..0.184 rows=100 loops=1)
-> Seq Scan on words (cost=0.00..227935.59 rows=441257 width=96) (actual time=0.080..0.160 rows=100 loops=1)
Filter: ((state IS NULL) AND (language_id = 27))
Total runtime: 0.240 ms
(4 rows)
這究竟是爲什麼?我怎樣才能在所有情況下使用索引?
謝謝。
沒有ORDER BY的LIMIT似乎是有限的(沒有雙關語意)值。您希望返回哪100行? – 2011-12-19 20:15:24
BTW:language_id = 17 AND status IS NULL子句的選擇性是什麼?單詞表的總大小是多少? – wildplasser 2011-12-19 20:28:39
true ...訂單發生在id DESC上。這可能會減慢它?該列需要索引嗎? @wildplasser總大小爲1,000萬行,選擇性大約爲500,000行 – alste 2011-12-19 20:32:32