我正在閱讀這article,並決定嘗試自己的例子。指數cond沒有出現在postgres查詢計劃
加入索引之前:
我有表BookHibernate
,被hibernate
生成SQL:
CREATE TABLE bookhibernate
(
book_id bigint NOT NULL,
bought boolean,
genre character varying(255),
name character varying(255) NOT NULL,
price integer NOT NULL,
author_id bigint,
CONSTRAINT bookhibernate_pkey PRIMARY KEY (book_id),
CONSTRAINT fk_hlepqn9vy6biuo6vn47jo5ewx FOREIGN KEY (book_id)
REFERENCES authorhibernate (author_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_r9o6704wcbcawmruyqojj4nab FOREIGN KEY (author_id)
REFERENCES authorhibernate (author_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
於是我就打電話explain analyze
:
explain analyze select * from bookhibernate where bookhibernate.price > 10
它給我:
"Seq Scan on bookhibernate (cost=0.00..1.02 rows=1 width=1053) (actual time=0.007..0.008 rows=2 loops=1)"
" Filter: (price > 10)"
"Planning time: 0.054 ms"
"Execution time: 0.021 ms"
加入指數後:
create index my_index on bookhibernate(price);
添加索引和執行相同explain analyze
我看到execution plan
沒有改變,我看到Filter: (price > 10)
寄存器,但我希望看到Index cond: (price > 10)
後。
爲什麼我的期望失敗?
更新:
我被告知,這是因爲小表的大小。這聽起來很合理,而且這是真的。但我試着對1000行表進行查詢。
- 爲什麼
Postgres
決定使用過濾?爲什麼更好? Filter: (price > 10)
現場背後是什麼?這個算法的複雜性是什麼?
表中是否有任何數據?你在桌子上運行「分析」嗎? – jmelesky
@jmelesky,是,兩行/是 –
對於兩行,沒有數據庫會使用索引。 –