范围查询中未使用postgresql索引
我有一个postgresql表:范围查询中未使用postgresql索引,sql,postgresql,Sql,Postgresql,我有一个postgresql表: CREATE TABLE new_table ( fecha_dato date NULL, codpers numeric NULL, ... CREATE INDEX ix_fecha_dato ON new_table USING btree (fecha_dato); CREATE INDEX ix_codpers ON new_table USING hash (codpers); 以及范围查询的问题: explain SEL
CREATE TABLE new_table (
fecha_dato date NULL,
codpers numeric NULL,
...
CREATE INDEX ix_fecha_dato ON new_table USING btree (fecha_dato);
CREATE INDEX ix_codpers ON new_table USING hash (codpers);
以及范围查询的问题:
explain SELECT * FROM new_table WHERE codpers>=10000000 and codpers<10000003;
QUERY PLAN |
----------------------------------------------------------------------------|
Seq Scan on tablon_base_pf_202006 (cost=0.00..5705226.14 rows=2 width=1404)|
Filter: ((codpers >= 10000000::numeric) AND (codpers < 10000002::numeric))|
explain SELECT * FROM tablones.tablon_base_pf_202006 where codpers in (10000000,10000001, 10000002);
QUERY PLAN
-----------------------------------------------------------------------------------
Bitmap Heap Scan on tablon_base_pf_202006 (cost=18.81..30.84 rows=3 width=1404)
Recheck Cond: (codpers = ANY ('{10000000,10000001,10000002}'::numeric[]))
-> Bitmap Index Scan on ix_hashtablon_base_pf_202006 (cost=0.00..18.81 rows=3 w
Index Cond: (codpers = ANY ('{10000000,10000001,10000002}'::numeric[]))
两个查询返回相同的3条记录,第一次花费超过5分钟,解释显示一次连续扫描,未使用密码子索引。
第二个花费不到1秒,使用codpers索引没有问题
有什么线索吗?将ix_密码更改为:
哈希索引比btree索引快,但不能用于范围查询,只能用于直接键搜索
CREATE INDEX ix_codpers ON new_table USING btree (codpers);