postgres返回索引树高度

这篇博客探讨了PostgreSQL中索引扫描的启动成本,涉及Hindex的概念,它是指数树的高度。通过一个例子展示了如何根据公式计算出启动成本,并提到了使用`bt_metap`函数可以直接获取索引信息。文章引用了一个SQL优化案例,强调了理解这些概念对于数据库性能优化的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一下,今天看书的时候看到这里的时候一直不明白Hindex是怎么算出来的。

3.2.2.1. Start-Up Cost

The start-up cost of the index scan is the cost to read the index pages to access to the first tuple in the target table, and it is defined by the following equation:

\{​{ceil(log_{2}(N_{index,tuple}))+(H_{index}+1)*50}\}*{cpu\_operator\_cost}

where HindexHindex is the height of the index tree.

In this case, according to (3), Nindex,tupleNindex,tuple is 10000; HindexHindex is 1; cpu_operator_costcpu_operator_cost is 0.0025 (by default). Thus,

‘start-up cost’={ceil(log2(10000))+(1+1)×50}×0.0025=0.285.

结果发现不需要去估算直接bt_metap返回就可以啦

taria=# select * from  bt_metap('idx_t1_id');
 magic  | version | root | level | fastroot | fastlevel | oldest_xact | last_cleanup_num_tuples | allequalimage 
--------+---------+------+-------+----------+-----------+-------------+-------------------------+---------------
 340322 |       4 |    1 |     0 |        1 |         0 |           0 |                      -1 | t
(1 row)

Reference

一个有趣的SQL优化案例__一个有趣的SQL优化案例__PostgreSQL中文社区: 世界上功能最强大的开源数据库...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值