PostgreSQL index scan,bitmap index scan区别

1. 建表
postgres=# create table tb_index_test(id serial primary key,name character varying);
CREATE TABLE 
postgres=# 
postgres=# \d tb_index_test;
                              Table "public.tb_index_test"
 Column |       Type        |                         Modifiers                          
--------+-------------------+------------------------------------------------------------
 id     | integer           | not null default nextval('tb_index_test_id_seq'::regclass)
 name   | character varying | 
Indexes:
    "tb_index_test_pkey" PRIMARY KEY, btree (id)
 2. 插入测试数据:
 postgres=# insert into tb_index_test values(generate_series(1,10000),'john');
INSERT 0 10000
3. 区别一、启动成本
> index sacn: 因为需要扫描索引块,节点才能输出,索引启动成本大于0
postgres=# explain(analyze,verbose,buffers)select count(0) from tb_index_test where id<400;
                                                                     QUERY PLAN                                                                     
----------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=22.29..22.30 rows=1 width=0) (actual time=0.108..0.108 rows=1 loops=1)
   Output: count(0)
   Buffers: shared hit=6
   ->  Index Scan using tb_index_test_pkey on public.tb_index_test  (<span style="color:#ff0000;">cost=0.29</span>..21.29 rows=400 width=0) (actual time=0.016..0.074 rows=399 loops=1)
         Output: id, name
         Index Cond: (tb_index_test.id < 400)
         Buffers: shared hit=6
 Total runtime: 0.140 ms
(8 rows)


Time: 0.620 ms
> bitmap index scan:  因为需要扫描索引块,节点才能放输出,索引启动成本也大于0,但是bitmap index scan的启动成本却是0,可以解释为bitmap Index Scan节点和Bitmap Heap Scan是一对的, 启动成本算在Bitmap Heap Scan上.
postgres=# explain(analyze,verbose,buffers)select count(0) from tb_index_test where id<400;
                                                              QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=72.39..72.40 rows=1 width=0) (actual time=0.178..0.178 rows=1 loops=1)
   Output: count(0)
   Buffers: shared hit=6
   ->  Bitmap Heap Scan on public.tb_index_test  (cost=11.38..71.39 rows=400 width=0) (actual time=0.062..0.120 rows=399 loops=1)
         Output: id, name
         Recheck Cond: (tb_index_test.id < 400)
         Buffers: shared hit=6
         ->  Bitmap Index Scan on tb_index_test_pkey  (c<span style="color:#ff0000;">ost=0.00</span>..11.29 rows=400 width=0) (actual time=0.047..0.047 rows=399 loops=1)
               Index Cond: (tb_index_test.id < 400)
               Buffers: shared hit=3
 Total runtime: 0.217 ms
(11 rows)


Time: 0.741 ms
4. 区别二、节点返回的类型
   index scan: 输出的是tuple,它先扫描索引块,然后得到rowid扫描数据块得到目标记录。
   bitmap index scan; 输出的是索引条目,并不是行的数据,输出索引条目后,交给上一个几点 bitmap heap scan(之间可能将索引条目根据物理排列顺序进行排序)。

 以上参考自 德哥 的教程

5. 区别三、扫描索引块和数据块的区别。
 index scan: 一次只读一条索引项,那么一个 PAGE面有可能被多次访问;
 bitmap index scan : 一次性将满足条件的索引项全部取出,然后交给bitmap heap scan节点,并在内存中进行排序, 根据取出的索引项访问表数据。
参考自: http://francs3.blog.163.com/blog/static/405767272011665227181/
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值