加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么PostgreSQL没有在小表上使用我的索引?

发布时间:2020-12-13 16:20:41 所属栏目:百科 来源:网络整理
导读:我在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 );CREAT
我在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)'

据我了解,这是一个全表扫描.问题是:为什么我的索引没有被使用?

可能是,原因是我桌上的行数太少了?我只有20个.你能不能给我一个SQL语句,用随机数据轻松填充我的表来检查索引问题?

我发现这篇文章:http://it.toolbox.com/blogs/db2luw/how-to-easily-populate-a-table-with-random-data-7888,但它对我不起作用.声明的效率无关紧要,只有简单.

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中的数据类型(因此也是保留字).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读