暂无图片
pg中or条件为啥走不上索引?
我来答
暂无图片 匿名用户
pg中or条件为啥走不上索引?


osdba=# create table test(id int primary key,col1 varchar(100),col2 varchar(50),u_id int,i_id int);
CREATE TABLE
osdba=#
osdba=#
osdba=# insert into test select 1,’test1’,’test1’,1,1;
INSERT 0 1
osdba=# insert into test select 2,’test2’,’test2’,2,2;
INSERT 0 1
osdba=# insert into test select 3,’test3’,’test3’,3,3;
INSERT 0 1
osdba=# insert into test select 4,’test4’,’test4’,4,4;
INSERT 0 1
osdba=#
osdba=#
osdba=# insert into test select 5,’test5’,’test5’,5,5;
INSERT 0 1
osdba=# insert into test select 6,’test6’,’test6’,6,6;
INSERT 0 1
osdba=# insert into test select 7,’test7’,’test7’,7,7;
INSERT 0 1
osdba=# insert into test select 8,’test8’,’test8’,8,8;
INSERT 0 1
osdba=# insert into test select 9,’test9’,’test9’,9,9;
INSERT 0 1
osdba=# insert into test select 10,’test10’,’test10’,10,10;
INSERT 0 1
osdba=# insert into test select 11,’test11’,’test11’,11,11;
INSERT 0 1

osdba=# create index idx_u_id on test(u_id);
CREATE INDEX
osdba=# create index idx_i_id on test(i_id);

explain select * from test t where t.u_id=1 or t.u_id=5; ###走全表扫描,改成union all 也走不上索引,请问这个怎么解释?
同样的语句在mysql 中可以走索引

我来答
添加附件
收藏
问题补充
1条回答
默认
最新
漫步者

不走是因为你插入的记录太少了,数据库通过简单顺序扫描就能获得结果
contractfee_testdb=> \d+ test;
数据表 “public.test”
栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述
------±-----------------------±---------±---------±-----±---------±-----±---------±-----
id | integer | | not null | | plain | | |
col1 | character varying(100) | | | | extended | | |
col2 | character varying(50) | | | | extended | | |
u_id | integer | | | | plain | | |
i_id | integer | | | | plain | | |
索引:
“test_pkey” PRIMARY KEY, btree (id)
“idx_u_id” btree (u_id)
访问方法 heap

contractfee_testdb=> explain select * from test where u_id=2 or u_id=3;
QUERY PLAN

Seq Scan on test (cost=0.00…13.15 rows=2 width=348)
Filter: ((u_id = 2) OR (u_id = 3))
(2 行记录)

contractfee_testdb=> explain select * from test where u_id=2 or u_id=3 or u_id =4;
QUERY PLAN

Seq Scan on test (cost=0.00…13.68 rows=3 width=348)
Filter: ((u_id = 2) OR (u_id = 3) OR (u_id = 4))
(2 行记录)


插入一万条记录后

contractfee_testdb=> \d+ test
数据表 “public.test”
栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述
------±-----------------------±---------±---------±-----±---------±-----±---------±-----
id | integer | | not null | | plain | | |
col1 | character varying(100) | | | | extended | | |
col2 | character varying(50) | | | | extended | | |
u_id | integer | | | | plain | | |
i_id | integer | | | | plain | | |
索引:
“test_pkey” PRIMARY KEY, btree (id)
“idx_i_id” btree (i_id)
“idx_u_id” btree (u_id)
访问方法 heap

contractfee_testdb=> explain select * from test where u_id=2;
QUERY PLAN

Index Scan using idx_u_id on test (cost=0.29…8.30 rows=1 width=30)
Index Cond: (u_id = 2)
(2 行记录)

contractfee_testdb=> explain select * from test where u_id=2 or u_id=3;
QUERY PLAN

Bitmap Heap Scan on test (cost=8.59…15.59 rows=2 width=30)
Recheck Cond: ((u_id = 2) OR (u_id = 3))
-> BitmapOr (cost=8.59…8.59 rows=2 width=0)
-> Bitmap Index Scan on idx_u_id (cost=0.00…4.29 rows=1 width=0)
Index Cond: (u_id = 2)
-> Bitmap Index Scan on idx_u_id (cost=0.00…4.29 rows=1 width=0)
Index Cond: (u_id = 3)
(7 行记录)

暂无图片 评论
暂无图片 有用 0
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
暂无图片墨值悬赏