首页 > 数据库 > MySQL > MySQL8.0:倒序索引数据的数据排列方式
这里简单记录用到了我的一个工具详细见如下:
innblock和bcview前者用于窥视innodb块的物理结构后者用于查看二进制文件免得肉眼撸。
innblock | InnoDB page观察利器
https://www.jianshu.com/p/c5ef92b0c769
bcview
http://pan.baidu.com/s/1num76RJ
我们知道普通索引数据的排列方式是从小到大的,而倒序索引应该是从大到小的那么如何证明呢?
下面我们就来一窥物理文件的组织方式,我们用一个小索引就在一个块里面来证明。
mysql> create table tab_desc -> (id1 int, -> id2 int, -> key(id1), -> key(id2 desc)); Query OK, 0 rows affected (1.29 sec) mysql> select * from tab_desc; +------+------+| id1 | id2 | +------+------+ | 1 | 1 || 2 | 2 | | 3 | 3 || 4 | 4 | | 5 | 5 || 6 | 6 | | 7 | 7 |+------+------+
这个比较简单我们使用using index type index 来访问索引发现他们确实是相反
mysql> desc select id2 from tab_desc; +----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | tab_desc | NULL | index | NULL | id2 | 5 | NULL | 7 | 100.00 | Using index |+----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.11 sec) mysql> select id2 from tab_desc; +------+| id2 |+------+| 7 || 6 || 5 || 4 || 3 || 2 || 1 |+------+7 rows in set (0.00 sec) mysql> desc select id1 from tab_desc; +----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | tab_desc | NULL | index | NULL | id1 | 5 | NULL | 7 | 100.00 | Using index |+----+-------------+----------+------------+-------+---------------+------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.00 sec) mysql> select id1 from tab_desc; +------+| id1 |+------+| 1 || 2 || 3 || 4 || 5 || 6 || 7 |+------+7 rows in set (0.00 sec)
===INDEX_ID:136level0 total block is (1) block_no: 4,level: 0|*| ===INDEX_ID:137level0 total block is (1) block_no: 5,level: 0|*| ===INDEX_ID:138level0 total block is (1) block_no: 6,level: 0|*|
| 136 | GEN_CLUST_INDEX | 1059 | 1 | 5 | 4 | 2 | 50 | | 137 | id1 | 1059 | 0 | 2 | 5 | 2 | 50 || 138 | id2 | 1059 | 0 | 2 | 6 | 2 | 50 |
id1 ==== Block list info ==== -----Total used rows:9 used rows list(logic): (1) INFIMUM record offset:99 heapno:0 n_owned 1,delflag:N minflag:0 rectype:2(2) normal record offset:126 heapno:2 n_owned 0,delflag:N minflag:0 rectype:0 (3) normal record offset:142 heapno:3 n_owned 0,delflag:N minflag:0 rectype:0(4) normal record offset:158 heapno:4 n_owned 0,delflag:N minflag:0 rectype:0(5) normal record offset:174 heapno:5 n_owned 0,delflag:N minflag:0 rectype:0(6) normal record offset:190 heapno:6 n_owned 0,delflag:N minflag:0 rectype:0(7) normal record offset:206 heapno:7 n_owned 0,delflag:N minflag:0 rectype:0(8) normal record offset:222 heapno:8 n_owned 0,delflag:N minflag:0 rectype:0 (9) SUPREMUM record offset:112 heapno:1 n_owned 8,delflag:N minflag:0 rectype:3id2 ==== Block list info ==== -----Total used rows:9 used rows list(logic): (1) INFIMUM record offset:99 heapno:0 n_owned 1,delflag:N minflag:0 rectype:2(2) normal record offset:222 heapno:8 n_owned 0,delflag:N minflag:0 rectype:0 (3) normal record offset:206 heapno:7 n_owned 0,delflag:N minflag:0 rectype:0(4) normal record offset:190 heapno:6 n_owned 0,delflag:N minflag:0 rectype:0(5) normal record offset:174 heapno:5 n_owned 0,delflag:N minflag:0 rectype:0(6) normal record offset:158 heapno:4 n_owned 0,delflag:N minflag:0 rectype:0(7) normal record offset:142 heapno:3 n_owned 0,delflag:N minflag:0 rectype:0(8) normal record offset:126 heapno:2 n_owned 0,delflag:N minflag:0 rectype:0 (9) SUPREMUM record offset:112 heapno:1 n_owned 8,delflag:N minflag:0 rectype:3
我们可以看到ID1普通索引逻辑链表信息为:
INFIMUM ->126 ->142 ->158 .....->SUPREMUM
而我们的反向索引逻辑链表信息为:
INFIMUM ->222->206 ->190 .....->SUPREMUM
那么我们分别来解读下数据因为普通索引的数据域排列方式就是:数据+主键 而int代表的是4字节那么
id1的数据就是 (这里用到了一个自己的工具bcview方便观察,当然非要肉眼撸也是也可以的用hexdump):
后面的我就不查询了可以看到是从小到大的。
接下来我们分解下倒序索引的数据:
因此我们得到验证,对于倒序索引而言其数据是在INFIMUM和SUPREMUM降序排列的。
作者微信:gaopp_22389860
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7728585/viewspace-2286703/ ,如需转载,请注明出处,否则将追究法律责任。