postgresql 使用explain 进行SQL分析

扫描节点

  1. Seq Scan:顺序扫描(全表扫描)
  2. Index Scan:索引扫描(不只是返回索引列的值)
  3. IndexOnly Scan:索引扫描(只返回索引列的值)
  4. BitmapIndex Scan:利用Bitmap 结构扫描
  5. BitmapHeap Scan:把BitmapIndex Scan 返回的Bitmap 结构转换为元组结构

Bitmap 结构扫描:处理多索引列的组合查询

案例

下面这是一段执行explain ANALYZE 打印输出的结果

Append  (cost=4.32..18016.95 rows=4507 width=54) (actual time=0.037..20.751 rows=6046 loops=1)
  ->  Bitmap Heap Scan on dwa_d_dg_staff_directsale_dev_report_p20231114 a  (cost=4.32..19.11 rows=4 width=49) (actual time=0.036..0.045 rows=4 loops=1)
        Recheck Cond: ((branch_id)::text = '837010040280000'::text)
        Heap Blocks: exact=4
        ->  Bitmap Index Scan on dwa_d_dg_staff_directsale_dev_report_p20231114_branch_id_idx  (cost=0.00..4.32 rows=4 width=0) (actual time=0.025..0.025 rows=4 loops=1)
              Index Cond: ((branch_id)::text = '837010040280000'::text)
  ->  Bitmap Heap Scan on dwa_d_dg_staff_directsale_dev_report_p20231115 a_1  (cost=4.32..19.11 rows=4 width=49) (actual time=0.019..0.026 rows=4 loops=1)
        Recheck Cond: ((branch_id)::text = '837010040280000'::text)
        Heap Blocks: exact=4
        ->  Bitmap Index Scan on dwa_d_dg_staff_directsale_dev_report_p20231115_branch_id_idx  (cost=0.00..4.32 rows=4 width=0) (actual time=0.013..0.013 rows=4 loops=1)
              Index Cond: ((branch_id)::text = '837010040280000'::text)
  ->  Bitmap Heap Scan on dwa_d_dg_staff_directsale_dev_report_p20231116 a_2  (cost=4.54..64.13 rows=16 width=54) (actual time=0.023..0.073 rows=18 loops=1)
        Recheck Cond: ((branch_id)::text = '837010040280000'::text)
        Heap Blocks: exact=18

从上面打印的结果 我们由此可以分析出branch_id是where里面的其中一个条件,在执行这个where条件时,branch_id进行了索引扫描,具体应该怎么看呢,下面来详细的分析一下:

  1. Bitmap Heap Scan on dwa_d_dg_staff_directsale_dev_report_p20231114:表示对dwa_d_dg_staff_directsale_dev_report表进行Bitmap Heap 扫描
  2. Recheck Cond: ((branch_id)::text = ‘837010040280000’::text)表明Bitmap Heap Scan 的Recheck操作 的条件是branch_id = ‘837010040280000’
  3. Heap Blocks: exact=4表明准确扫描到数据块的个数是4
  4. Bitmap Index Scan on dwa_d_dg_staff_directsale_dev_report_p20231114_branch_id_idx:表示使用dwa_d_dg_staff_directsale_dev_report_p20231114_branch_id_idx 索引进行位图索引扫描
  5. actual time:执行时间,格式为xxx…xxx,在… 之前的是该节点实际的启动时间,即找到符合该节点条件的第一个结果实际需要的时间,在…之后的是该节点实际的执行时间
  6. Filter:条件过滤
  7. rows:指的是该节点实际的返回行数
  8. loops:指的是该节点实际的重启次数。如果一个计划节点在运行过程中,它的相关参数值(如绑定变量)发生了变化,就需要重新运行这个计划节点。
  9. width:每行平均宽度为74字节
  10. cost:第一个数字表示启动的成本,也就是返回第一行需要多少 cost 值;第二个数字表示返回所有的数据的成本

注意

如果获取的结果集的占比比较小,但是元组数很多时,可能Bitmap Index Scan 的性能要比Index Scan 好

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值