MySQL InnoDB Engine--倒序索引的存储

倒序索引存储

引用"阿里云数据库开源"里说的:MySQL倒序索引的改动主要在server层的优化器和执行器,在InnoDB存储引擎层变化不大。

MySQL倒序索引限制:

  • 由于涉及到数据的存储,目前只支持InnoDB
  • Descending index 无法使用change buffer
  • Descneding index不支持fulltext或spatial index, 选择desc关键字会报错
  • GROUP BY不在隐式的保证顺序性,只有明确的指定asc/desc,才去确保顺序

测试表结构

CREATE TABLE `tb1002` (
  `id` int NOT NULL AUTO_INCREMENT,
  `c1` varchar(100) COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_c1` (`c1` DESC)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

SQL数据

 insert into tb1002(c1)select 'AAAAA';
 insert into tb1002(c1)select 'BBBBB';
 insert into tb1002(c1)select 'CCCCC';
 insert into tb1002(c1)select 'DDDDD';
 
 mysql> select * from tb1002 order by id;
+----+-------+
| id | c1    |
+----+-------+
|  2 | AAAAA |
|  4 | BBBBB |
|  6 | CCCCC |
|  8 | DDDDD |
+----+-------+
4 rows in set (0.00 sec)

二级制数据

69 6e 66 69 6d 75 6d 00 05 00 0b 00 00
# Hex 69 6e 66 69 6d 75 6d ==> Infimum
73 75 70 72 65 6d 75 6d
# Hex 73 75 70 72 65 6d 75 6d ==> supremun
05 00 00 00 10 ff f1 41 41 41 41 41 80 00 00 02
# Hex 10==>16(Next Record)    Hex 41 41 41 41 41==>AAAAA(c1)  Hex 02==>2(id)
05 00 00 00 18 ff f0 42 42 42 42 42 80 00 00 04
# Hex 18==>24(Next Record)    Hex 42 42 42 42 42==>BBBBB(c1)  Hex 04==>4(id)
05 00 00 00 20 ff f0 43 43 43 43 43 80 00 00 06
# Hex 20==>32(Next Record)    Hex 43 43 43 43 43==>CCCCC(c1)  Hex 06==>6(id)
05 00 00 00 28 ff f0 44 44 44 44 44 80 00 00 08
# Hex 28==>40(Next Record)    Hex 44 44 44 44 44==>DDDDD(c1)  Hex 06==>8(id)

从二级制数据来看,倒序索引idx_c1(c1 DESC)的数据依旧是从小到大存放,记录中Next Record指针指向的还是比当前记录更大的记录所在位置,与普通正序索引idx_c1(c1 ASC)的存储方式相同。

拓展阅读

posted @ 2023-04-27 21:44  TeyGao  阅读(113)  评论(0编辑  收藏  举报