【发布时间】:2014-01-22 06:37:38
【问题描述】:
我正在对数据库执行此查询(所有数字和列名都是组成的):
select * from t where a=1 and b=11 and c!=5 and d<8
t 有一个索引:
create index i on t (a,b,c,d)
当我运行“EXPLAIN ANALYZE”时,查询会执行顺序扫描,并且执行此操作大约需要 55 毫秒。如果我像这样修改查询:
select * from t where a=1 and b=11 and c=5 and d<8
^
它使用索引并在 0.5 毫秒内完成。所以它一定是不等于,对吧?不是这样,因为如果我做这个查询:
select * from t where a=1 and b=11 and c=5 and d!=8
^
查询仍然使用索引。但是如果我尝试这个,没有索引:
select * from t where a=1 and b=11 and c<5 and d<8
^
那么为什么 Postgres 会这样呢?这对我来说很奇怪。
【问题讨论】:
标签: sql postgresql indexing