MySQL8.0:倒序索引資料的資料排列方式

gaopengtttt發表於2018-12-27

MySQL8.0:倒序索引資料的資料排列方式

96  

這裡簡單記錄用到了我的一個工具詳細見如下:
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)

三、通過工具證明

執行 ./innblock tab_desc.ibd scan 16得到結果
===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|*|
通過INNODB_INDEXES可以看到這兩個索引對應的ID確實是137/138
|      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 |
通過命令 ./innblock tab_desc.ibd 5 16和 ./innblock tab_desc.ibd 6 16可以獲得他們的邏輯連結串列資訊如下:
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):

  • 第一行 126位元組後的4位元組為:80000001
    current block:00000005--Offset:00126--cnt bytes:04--data is:80000001
  • 第二行 142位元組後的4個位元組:80000002
    current block:00000005--Offset:00142--cnt bytes:04--data is:80000002
  • 第三行 158位元組後的4個位元組:80000003
    current block:00000005--Offset:00158--cnt bytes:04--data is:80000003
  • 第四行 174位元組後的4個位元組:80000004
    current block:00000005--Offset:00174--cnt bytes:04--data is:80000004

後面的我就不查詢了可以看到是從小到大的。

接下來我們分解下倒序索引的資料:

  • 第一行 222位元組後的4位元組為: 80000007
    current block:00000006--Offset:00222--cnt bytes:04--data is:80000007
  • 第二行 206位元組後的4個位元組: 80000006
    current block:00000006--Offset:00206--cnt bytes:04--data is:80000006
  • 第三行 190位元組後的4個位元組: 80000005
    current block:00000006--Offset:00190--cnt bytes:04--data is:80000005
  • 第四行 174位元組後的4個位元組: 80000004
    current block:00000006--Offset:00174--cnt bytes:04--data is:80000004

因此我們得到驗證,對於倒序索引而言其資料是在INFIMUM和SUPREMUM降序排列的。

作者微信:gaopp_22389860


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2286703/,如需轉載,請註明出處,否則將追究法律責任。

相關文章