表+ LIMIT +卦:說明有關索引+ ORDER BY在PostgreSQL的9.1
CREATE TABLE msp_adm_munic_complet_g_01
(
nom_tri character varying(64),
ogc_fid serial NOT NULL
)
指數:
CREATE INDEX idx_gist_msp_adm_munic_complet_g_nom_tri
ON msp_adm_munic_complet_g_01
USING gist
(nom_tri COLLATE pg_catalog."default" gist_trgm_ops);
查詢:
select * from msp_adm_munic_complet_g_01
ORDER BY 'potato'<->nom_tri
LIMIT 25;
問題:
爲什麼它通過梳子通過指數ORDER BY + LIMIT的初始化,而不是當查詢只包含ORDER BY時?
當然,指數也增加了查詢的速度
我發現的唯一的解釋是在這裏: http://www.postgresql.org/docs/9.1/static/indexes-ordering.html
但缺乏細節
編輯#1:
帶限制的查詢計劃:
Limit (cost=0.00..19.27 rows=25 width=590)
-> Index Scan using idx_gist_msp_adm_munic_complet_g_nom_tri on
msp_adm_munic_complet_g_01 (cost=0.00..2784.49 rows=3612 width=590)
Order By: ((nom_tri)::text <-> 'potato'::text)
查詢計劃沒有限制:
Sort (cost=1847.59..1856.62 rows=3612 width=590)
Sort Key: (('potato'::text <-> (nom_tri)::text))
-> Seq Scan on msp_adm_munic_complet_g_01 (cost=0.00..682.15 rows=3612 width=590)
請向我們展示您的查詢的執行計劃('explain analyze')(理想情況下上傳到http://explain.depesz.com) –
另外,請解釋*爲什麼*您認爲它應該使用索引。你認爲執行者應該採取哪些精確的步驟?嘗試並向他們提出一些估計的成本 - 如果您不知道某些步驟的相對成本,請不要擔心。即使數字與現實不符,仔細思考它也是有用的。 –
當我閱讀您所引用的PG文檔中的解釋時,爲什麼在這種情況下不使用索引對我有意義。你可能必須解釋爲什麼你認爲這個解釋缺乏細節。 – harmic