使用Postgresql分页查询遇到的问题??

1,假设有一个表
结构和索引如下:
create table test1(ID BIGINT,name varchar(10),age int,addr varchar(128));
插入测试数据:
insert into test1 select i,'name'||i,100,substr(md5(random()::text),1,30) from generate_series(1,1000000) as t(i);
-- 创建组合索引,ID + NAME
create index idx_test_id_name on test1(id ,name);
analyze test1;

2,分页查询语句
-- sql语句:
select id,name from test1 where id between 1000 and 10000 and name in ('name1','name2','name3')
order by id desc limit 24;

-- 执行计划显示
explain (analyze,buffers) select id,name from test1 where id between 1000 and 10000 and name in ('name1','name2','name3')
order by id desc limit 24;

Limit (cost=0.42..10879 rows=1 width=51) (actual time=35.174..35.174 rows=0 loops=1)
Buffers:shared hit = 39 read=93
--> Index Scan using idx_test1_id_name on test1(cost=0.42..10879.37 row=1 width=51)(actual time=35.169..35.169 rows=0 loops=1)
Index Cond:((id >=1000 ) and (id <=10000))
Filter:((name)::text = ANY('{name1,name2}'::text[]))
Rows Removed by Filter:9001
Buffers:shared hit=39 read = 93
Planning time : 2.183ms
Execution time : 35.311ms

-- 2个疑问:
第一个问题:
Filter:((name)::text = ANY('{name1,name2}'::text[]))
这步为什么不能在索引的块中过滤,Pg走的是回表过滤,这样会造成效率低下。
仅仅是根据idx_test1_id_name的前导列范围扫描id,直接回表读取heap page过滤
而且我只选中了id和name两列都在索引中的,理论上不需要回表嘛,

这点在oracle数据库中确认没有回表过程

第二个问题:
postgresql的执行计划很难看出是否真正的回表了。上面的回表过滤我是通过查看 pg_buffercache插件确认的。

请大牛帮忙看看,是否有好的思路和方法。

posted on 2018-06-29 15:59  qzren1982  阅读(1448)  评论(0编辑  收藏  举报

导航