我在Postgresql中有以下表格:
CREATE TABLE index_test ( id int PRIMARY KEY NOT NULL,text varchar(2048) NOT NULL,last_modified timestamp NOT NULL,value int,item_type varchar(2046) ); CREATE INDEX idx_index_type ON index_test ( item_type ); CREATE INDEX idx_index_value ON index_test ( value )
我做了以下选择:
explain select * from index_test r where r.item_type='B'; explain select r.value from index_test r where r.value=56;
执行计划的解释如下:
Seq Scan on index_test r (cost=0.00..1.04 rows=1 width=1576) Filter: ((item_type)::text = 'B'::text)'
据我了解,这是一个全表扫描.问题是:为什么我的索引没有被使用?
Maybe,the reason is that I have too few rows in my table?
是.对于表中的总共20行,seq扫描总是比索引扫描更快.有可能这些行无论如何都位于单个数据库块中,因此seq扫描只需要一个I / O操作.
如果你使用
explain (analyze true,verbose true,buffers true) select ....
你可以看到更多关于真实情况的细节.
顺便说一句:你不应该使用文本作为列名,因为它也是Postgres中的数据类型(因此也是保留字).