MySQL InnoDB Engine--主键索引非叶子节点数据存储
测试表信息
1、测试表结构
CREATE TABLE `schedule` ( `id` int(11) NOT NULL AUTO_INCREMENT, `task_id` int(11) NOT NULL, `first_run_time` datetime NOT NULL, `end_run_time` datetime DEFAULT NULL, `next_run_time` datetime DEFAULT NULL, `run_interval` int(11) DEFAULT NULL, `time_unit` tinyint(4) NOT NULL, `status` tinyint(4) NOT NULL, `schedule_config` text NOT NULL, `create_time` datetime NOT NULL, `update_time` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `task_id` (`task_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1214 DEFAULT CHARSET=utf8
2、测试表数据
当前测试表中有681条数据,每个数据页16KB,该表ibd文件为176KB(11个数据页)
3、查看索引的根节点数据页
SELECT T1.* FROM information_schema.INNODB_SYS_INDEXES AS T1 INNER JOIN information_schema.INNODB_SYS_TABLES AS T2 ON T1.TABLE_ID = T2.TABLE_ID WHERE T2.NAME LIKE 'test/schedule'; +----------+---------+----------+------+----------+---------+-------+-----------------+ | INDEX_ID | NAME | TABLE_ID | TYPE | N_FIELDS | PAGE_NO | SPACE | MERGE_THRESHOLD | +----------+---------+----------+------+----------+---------+-------+-----------------+ | 1559 | PRIMARY | 669 | 3 | 1 | 3 | 658 | 50 | | 1560 | task_id | 669 | 2 | 1 | 4 | 658 | 50 | +----------+---------+----------+------+----------+---------+-------+-----------------+
根节点数据分析
根据主键根节点数据页编号,通过Hex Editor查看ibd文件,可以找到该页数据为:
00 10 00 11 00 0e 80 00 00 46 00 00 00 05 00 00 00 19 00 0e 80 00 00 e3 00 00 00 06 00 00 00 21 00 0e 80 00 02 9a 00 00 00 07 00 00 00 29 00 0e 80 00 03 a5 00 00 00 08 00 00 00 31 ff ba 80 00 04 79 00 00 00 09
整理后信息为:
每条记录使用4字节空间存放子节点数据页上存放的主键最小值
80 00 00 46 = 70
80 00 00 e3 = 227
80 00 02 9a = 666
80 00 03 a5 = 933
80 00 04 79 = 1145
主键ID在[70,227)的记录88条,在[227,666)的记录181条,在[666,933)的记录182条,在[933,1145)的记录182条,在[1145,N)的记录为48行。
每条记录的最后4各字节存放对应数据页的页编号,主键索引的叶子节点数据存放于05/06/07/08/09五个数据页上,主键索引根节点页编号为03,task_id索引的所有数据存放于页编号04的数据页。