MySQL8.0中的降序索引

前言

相信大家都知道,索引是有序的;不过,在MySQL之前版本中,只支持升序索引,不支持降序索引,这会带来一些问题;在最新的MySQL 8.0版本中,终于引入了降序索引,接下来我们就来看一看。

降序索引

单列索引

(1)查看测试表结构

mysql> show create table sbtest1\G
*************************** 1. row ***************************
    Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
 `id` int unsigned NOT NULL AUTO_INCREMENT,
 `k` int unsigned NOT NULL DEFAULT '0',
 `c` char(120) NOT NULL DEFAULT '',
 `pad` char(60) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`),
 KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci MAX_ROWS=1000000
1 row in set (0.00 sec)

(2)执行SQL语句order by ... limit n,默认是升序,可以使用到索引

mysql> explain select * from sbtest1 order by k limit 10;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | k_1 | 4    | NULL |  10 |  100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

(3)执行SQL语句order by ... desc limit n,如果是降序的话,无法使用索引,虽然可以相反顺序扫描,但性能会受到影响

mysql> explain select * from sbtest1 order by k desc limit 10;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+---------------------+
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra        |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+---------------------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | k_1 | 4    | NULL |  10 |  100.00 | Backward index scan |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+---------------------+
1 row in set, 1 warning (0.00 sec)

(4)创建降序索引

mysql> alter table sbtest1 add index k_2(k desc);
Query OK, 0 rows affected (6.45 sec)
Records: 0 Duplicates: 0 Warnings: 0

(5)再次执行SQL语句order by ... desc limit n,可以使用到降序索引

mysql> explain select * from sbtest1 order by k desc limit 10;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | k_2 | 4    | NULL |  10 |  100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

多列索引

(1)查看测试表结构

mysql> show create table sbtest1\G
*************************** 1. row ***************************
    Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
 `id` int unsigned NOT NULL AUTO_INCREMENT,
 `k` int unsigned NOT NULL DEFAULT '0',
 `c` char(120) NOT NULL DEFAULT '',
 `pad` char(60) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`),
 KEY `k_1` (`k`),
 KEY `idx_c_pad_1` (`c`,`pad`)
) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci MAX_ROWS=1000000
1 row in set (0.00 sec)

(2)对于多列索引来说,如果没有降序索引的话,那么只有SQL 1才能用到索引,SQL 4能用相反顺序扫描,其他两条SQL语句只能走全表扫描,效率非常低

SQL 1:select * from sbtest1 order by c,pad limit 10;

SQL 2:select * from sbtest1 order by c,pad desc limit 10;

SQL 3:select * from sbtest1 order by c desc,pad limit 10;

SQL 4:explain select * from sbtest1 order by c desc,pad desc limit 10;

mysql> explain select * from sbtest1 order by c,pad limit 10;
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | idx_c_pad_1 | 720   | NULL |  10 |  100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from sbtest1 order by c,pad desc limit 10;
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows  | filtered | Extra     |
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| 1 | SIMPLE   | sbtest1 | NULL    | ALL | NULL     | NULL | NULL  | NULL | 950738 |  100.00 | Using filesort |
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from sbtest1 order by c desc,pad limit 10;
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| id | select_type | table  | partitions | type | possible_keys | key | key_len | ref | rows  | filtered | Extra     |
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| 1 | SIMPLE   | sbtest1 | NULL    | ALL | NULL     | NULL | NULL  | NULL | 950738 |  100.00 | Using filesort |
+----+-------------+---------+------------+------+---------------+------+---------+------+--------+----------+----------------+
1 row in set, 1 warning (0.01 sec)

mysql> explain select * from sbtest1 order by c desc,pad desc limit 10;
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+---------------------+
| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref | rows | filtered | Extra        |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+---------------------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | idx_c_pad_1 | 720   | NULL |  10 |  100.00 | Backward index scan |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+---------------------+
1 row in set, 1 warning (0.00 sec)

(3)创建相应的降序索引

mysql> alter table sbtest1 add index idx_c_pad_2(c,pad desc);
Query OK, 0 rows affected (1 min 11.27 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table sbtest1 add index idx_c_pad_3(c desc,pad);
Query OK, 0 rows affected (1 min 14.22 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table sbtest1 add index idx_c_pad_4(c desc,pad desc);
Query OK, 0 rows affected (1 min 8.70 sec)
Records: 0 Duplicates: 0 Warnings: 0

(4)再次执行SQL,均能使用到降序索引,效率大大提升

mysql> explain select * from sbtest1 order by c,pad desc limit 10;
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | idx_c_pad_2 | 720   | NULL |  10 |  100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from sbtest1 order by c desc,pad limit 10;
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | idx_c_pad_3 | 720   | NULL |  10 |  100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from sbtest1 order by c desc,pad desc limit 10;
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
| 1 | SIMPLE   | sbtest1 | NULL    | index | NULL     | idx_c_pad_4 | 720   | NULL |  10 |  100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+-------------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

总结

MySQL 8.0引入的降序索引,最重要的作用是,解决了多列排序可能无法使用索引的问题,从而可以覆盖更多的应用场景。

以上就是MySQL8.0中的降序索引的详细内容,更多关于MySQL 降序索引的资料请关注我们其它相关文章!

(0)

相关推荐

  • MySQL8新特性:降序索引详解

    前言 MySQL 8.0终于支持降序索引了.其实,从语法上,MySQL 4就支持了,但正如官方文档所言,"they are parsed but ignored",实际创建的还是升序索引. 无图无真相,同一个建表语句,看看MySQL 5.7和8.0的区别. create table slowtech.t1(c1 int,c2 int,index idx_c1_c2(c1,c2 desc)); MySQL 5.7 mysql> show create table slowtech.

  • mysql8.0.20配合binlog2sql的配置和简单备份恢复的步骤详解

    第一步 安装 1.安装MySQL 2.安装Python3 [root@localhost /]#yum install python3 3.下载binlog2sql文件到本地(文件在百度云盘) [root@localhost /]#mkdir tools [root@localhost /]#cd tools [root@localhost tools]# ll total 317440 -rw-r--r--. 1 root root 317440 Sep 21 23:55 binlog2sql

  • mysql8.0.21下载安装详细教程

    官网地址:https://www.mysql.com/ 安装建议:尽量不要用.exe进行安装,用压缩包安装,对日后的卸载更为方便 下载地址:https://dev.mysql.com/downloads/mysql/ 1.下载得到zip压缩包 2.解压到要安装的目录 我这里是:D:\Program File\MySQL\mysql-8.0.21-winx64,增加了data目录 3.添加环境变量 我的电脑–>属性–>高级–>环境变量 选择path添加:mysql安装目录下的bin文件夹地

  • Windows系统下MySQL8.0.21安装教程(图文详解)

    安装建议:尽量不要用.exe进行安装,用压缩包安装,对日后的卸载/版本升级更为方便 下载地址:https://dev.mysql.com/downloads/mysql/ 1.点击上面的下载地址得到zip压缩包 2.解压到要安装的目录 我这里是E:\database\mysql8\mysql-8.0.21-winx64\bin data 文件夹与 my.ini文件需手动创建出来 3.添加环境变量 我的电脑–>属性–>高级系统设置–>环境变量 选择path添加:mysql安装目录下的bin

  • mysql8.0.21安装教程图文详解

    1.下载 下载链接 点击download,这里可能需要登录甲骨文的账号,登录一下即可 2.解压 下载好会得到一个安装包 把它解压到一个能找到的目录下即可,我的是这样(my.ini文件你们应该没有) 3.新建my.ini 如图,新建一个文件,后缀名改成ini.文件名最好不要改 打开文件,将下面的代码粘贴复制进去 [mysqld] # 设置3306端口 port=3306 # 设置mysql的安装目录 basedir=D:\\mysql-8.0.21-winx64 # 设置mysql数据库的数据的存

  • MySQL8新特性之降序索引底层实现详解

    什么是降序索引 大家可能对索引比较熟悉,而对降序索引比较陌生,事实上降序索引是索引的子集. 我们通常使用下面的语句来创建一个索引: create index idx_t1_bcd on t1(b,c,d); 上面sql的意思是在t1表中,针对b,c,d三个字段创建一个联合索引. 但是大家不知道的是,上面这个sql实际上和下面的这个sql是等价的: create index idx_t1_bcd on t1(b asc,c asc,d asc); asc表示的是升序,使用这种语法创建出来的索引叫做

  • VS2019连接mysql8.0数据库的教程图文详解

    1.首先准备好VS2019以及mysql数据库,两者都可以去官网下载,我们直接描述连接过程. 2.连接: 第一步:打开mysql的安装目录,我本地的安装目录如下:(注意是否有include和lib文件夹) 第二步:打开VS2019,新建一个空工程. 第三步:右击工程名,打开属性页: 第四步:打开VC++目录,在包含目录中,将mysql安装文件中的include文件的路径添加到这里: 第五步:还是在属性页上,打开C/C++,选择常规,和上一步一样,在附加包含目录中将mysql文件中的include

  • MySQL8.0中binlog的深入讲解

    1 简介 二进制日志,记录对数据发生或潜在发生更改的SQL语句,并以二进制形式保存在磁盘. 2 Binlog 的作用 主要作用:复制.恢复和审计. 3 开启Binlog 3.1 查询当前 MySQL 是否支持 binlog 如下OFF代表不支持 3.2 修改 my.cnf 文件以支持 binlog 查看my.cnf路径 mysql --help --verbose | grep my.cnf 在/etc 新建文件my.cnf并添加如下内容 注意添加 mysqld 组 重启 MySQL 再次查看是

  • MySQL8.0.21.0社区版安装教程(图文详解)

    一.下载MySQL 登录MySQL官网下载MSI Installer: 点击"Dnownload" 点击"No thanks, just start my download." 二.安装MySQL Step1:选择安装类型 根据个人需求,选择其中一个安装类型: Developer Default 开发者默认安装 Server only 仅安装服务端(推荐) Client only 仅安装客户端 Full 安装所有内容 Custom 自定义安装(推荐) 点击"

  • MySQL8.0 如何快速加列

    前言: 很早就听说 MySQL8.0 支持快速加列,可以实现大表秒级加字段.笔者自己本地也有8.0环境,但一直未进行测试.本篇文章我们就一起来看下 MySQL8.0 快速加列到底要如何操作. 1.了解背景信息 表结构的变更是业务运行过程中比较常见的需求之一,在 MySQL 的环境中,可以使用 Alter 语句来完成这些操作,这些 Alter 语句对应的操作通常也称之为 DDL 操作.通常情况下大表的 DDL 操作都会对业务有很明显的影响,需要在业务空闲,或者是维护的时候做.MySQL 5.7 支

随机推荐