当前位置:网站首页>MySQL索引的B+树到底有多高?
MySQL索引的B+树到底有多高?
2022-08-10 11:56:00 【InfoQ】
一、问题
- h:统称索引的高度;
- h1:聚簇索引的高度;
- h2:二级辅助索引的高度;
- k:中间结点的扇出系数。
二、分析
2.1 索引高度h与页面I/O数的关系
- 点查询: 聚族索引:h1 二级索引: 覆盖索引:h2 回表查询:h2+h1
- 范围查询:这种情况相对比较复杂,但跟点查询的原理类似,读者可自行分析;
- 全表查询:B+树的叶子结点是通过链表连接起来的,对于全表查询,需要从头到尾将所有的叶子结点访问一遍。
2.2 索引高度h的理论计算
三、查看索引真实高度的方法

SELECT b.name, a.name, index_id, type, a.space, a.PAGE_NO
FROM information_schema.INNODB_SYS_INDEXES a,
information_schema.INNODB_SYS_TABLES b
WHERE a.table_id = b.table_id
AND a.space <> 0;
$hexdump -C -s 49216 -n 10 user.ibd
0000c040 00 01 00 00 00 00 00 00 03 2b
- 通过information_schema.INNODB_SYS_INDEXES和information_schema.INNODB_SYS_TABLES找到对应索引Root页在ibd文件的页面号PAGE_NO(聚簇索引通常为3,其他索引往上累加);
- 通过hexdump -C -s 16384*PAGE_NO+64 -n 10 user.ibd,前两个字节+1即为索引的高度。
四、验证索引的真实高度
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`card_id` INT(11) NOT NULL DEFAULT '0' COMMENT '身份证号',
`name` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '英文名字',
`age` TINYINT(2) UNSIGNED NOT NULL DEFAULT '0' COMMENT '年龄',
`content` VARCHAR(1024) NOT NULL DEFAULT '' COMMENT '附属信息',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最近更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_card_id` (`card_id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
五、总结
- 一次查询的页面I/O数跟索引高度h呈正相关,主要分为以下几种情况:点查询: 聚族索引:h1 二级索引: 覆盖索引:h2 回表查询:h2+h1范围查询:这种情况相对比较复杂,但跟点查询的原理类似,读者可自行分析;全表查询:B+树的叶子结点是通过链表连接起来的,对于全表查询,需要从头到尾将所有的叶子结点访问一遍。
- 索引高度通常为2~4层。在高度h=3、主键为int类型、行记录大小为1KB时,可索引的总行数为1170^2*16=2190W。这也是很多大厂将2000W作为分库分表标准的原因;
- 查看索引真实高度的方法如下:
- 通过information_schema.INNODB_SYS_INDEXES和information_schema.INNODB_SYS_TABLES找到对应索引Root页在ibd文件的页面号PAGE_NO(聚簇索引通常为3,其他索引往上累加);
- 通过hexdump -C -s 16384*PAGE_NO+64 -n 10 user.ibd,前两个字节+1即为索引的高度。
- 索引高度h也跟索引字段的数据类型有关。如果是int或short,扇出系数大,索引效率更好,整个索引看起来属于“矮胖”型;而如果是varchar(32)等,那扇出系数就低了,整个索引看起来属于“瘦高”型,索引效率自然要低些。所以我们在字段选取类型时,其类型越简单效率越好。
#聚簇索引(第一个)
hexdump -C -s 49216 -n 10 user.ibd
#第二个索引
hexdump -C -s 65600 -n 10 user.ibd
#第三个索引
hexdump -C -s 81984 -n 10 user.ibd
边栏推荐
- 托米的咒语
- 48MySQL数据库基础
- Diary 16
- Deploy the project halfway through the follow-up
- LeetCode 369. Plus One Linked List
- Servlet---Solve the problem of Chinese garbled characters in post requests
- LeetCode 109. 有序链表转换二叉搜索树
- SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
- LeetCode 138. Copy a linked list with random pointers
- 面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官
猜你喜欢
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
How many constants and data types do you remember?
LT8911EXB MIPI CSI/DSI to EDP signal conversion
three.js模糊玻璃效果
国外媒体宣发怎样做才可以把握重点
Does face attendance choose face comparison 1:1 or face search 1:N?
The 6th "Blue Hat Cup" National College Student Network Security Skills Competition Semi-Final Part WriteUp
How to do foreign media publicity to grasp the key points
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
百度用户产品流批一体的实时数仓实践
随机推荐
LeetCode 109. Sorted Linked List Conversion Binary Search Tree
SMIC CIM localization project suspended?Rising software: not shut down, changed to remote development!
技术人必看!数据治理是什么?它对数据中台建设重要吗?
阿里架构师整理一份企业级SSM架构实战文档,让你熟悉底层原理
浮动及其特点
Hackbar 使用教程
基础 | batchnorm原理及代码详解
It is rumored that Samsung 3nm has won the second customer, and the current production capacity is in short supply
[Collection] HashSet and ArrayList lookup Contains() time complexity
配置swagger
CodeForces - 628D (digital dp)
LeetCode 24. 两两交换链表中的节点
48 the mysql database
Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
StarRocks on AWS Review | Data Everywhere Series Event Shenzhen Station ended successfully
The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
Overseas media publicity. What problems should domestic media pay attention to?
海外媒体宣发.国内媒体发稿要注意哪些问题?
Color map and depth map to point cloud
Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?