MySQL 降序索引简介(mysql降序关键字)
mhr18 2024-10-01 12:31 20 浏览 0 评论
使用过Oracle、SQLServer数据库的降序索引的同学,可能在使用MySQL8.0之前版本时有个疑惑,明明我已经创建了将需要索引,但是为何执行时走不了索引或者效果不理想?
1. 创建环境
分别在MySQL5.7 及MySQL8.0版本中创建如下表及数据
# 创建表
create table test1(
id int primary key auto_increment,
name varchar(100),
create_time datetime
);
# 插入部分测试数据,有条件的创建更多数据更佳
insert into test1(name,creatE_time) values('anniuadaOAIFAPUHIA','2020-07-01 12:00:00');
insert into test1(name,creatE_time) values('CWQSsar3qcssg','2020-07-01 15:00:00');
insert into test1(name,creatE_time) values('vxfqrt2adafz','2020-07-01 21:30:00');
insert into test1(name,creatE_time) values('etxzwrwbdhegqgaheqhag','2020-07-02 01:30:00');
insert into test1(name,creatE_time) values('awrs433fsgvsfwtwg','2020-07-02 03:30:00');
insert into test1(name,creatE_time) values('awrs433fsgvsfwtwg','2020-07-02 07:32:00');
insert into test1(name,creatE_time) values('awrs433fsgvsfwtwg','2020-07-02 10:32:00');
insert into test1(name,creatE_time) values('tuilklmdadq','2020-07-02 15:32:00');
insert into test1(name,creatE_time) values('wesv2wqdshehq','2020-07-02 20:32:00');
insert into test1(name,creatE_time) values('89yoijnlkwr1','2020-07-03 02:56:00');
insert into test1(name,creatE_time) values('olj;nsaaq','2020-07-03 08:41:00');
insert into test1(name,creatE_time) values('ygo;jkdsaq','2020-07-03 16:20:00');
2. MySQL5.7中创建索引并查看执行计划
2.1 MySQL5.7中创建升序索引
在MySQL5.7中创建升序索引,并执行SQL查看执行计划
# 升序索引
alter table test1 add key idx_nameAsc_createtimeAsc( name,create_time);
执行语句查看执行计划
mysql> explain select * from test1 order by name desc ,create_time ;
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
| 1 | SIMPLE | test1 | NULL | index | NULL | idx_nameAsc_createtimeAsc | 309 | NULL | 12 | 100.00 | Using index; Using filesort |
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
2.2 MySQL5.7中创建降序索引
在MySQL5.7中创建降序索引,并执行SQL查看执行计划
# 创建降序索引
alter table test1 add key idx_nameDesc_createtimeAsc( name desc ,create_time);
执行SQL并查看执行计划
mysql> explain select * from test1 order by name desc ,create_time;
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
| 1 | SIMPLE | test1 | NULL | index | NULL | idx_nameAsc_createtimeAsc | 309 | NULL | 12 | 100.00 | Using index; Using filesort |
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
发现使用的仍是升序索引,且用到了filesort
2.3 MySQL5.7中查看索引情况
查看索引情况会发现,MySQL5.7中,即使创建了降序索引,但是,排序方式依旧是升序(A[sc])
mysql> show index from test1;
+-------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test1 | 0 | PRIMARY | 1 | id | A | 12 | NULL | NULL | | BTREE | | |
| test1 | 1 | idx_nameAsc_createtimeAsc | 1 | name | A | 10 | NULL | NULL | YES | BTREE | | |
| test1 | 1 | idx_nameAsc_createtimeAsc | 2 | create_time | A | 12 | NULL | NULL | YES | BTREE | | |
| test1 | 1 | idx_nameDesc_createtimeAsc | 1 | name | A | 10 | NULL | NULL | YES | BTREE | | |
| test1 | 1 | idx_nameDesc_createtimeAsc | 2 | create_time | A | 12 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3. MySQL8.0中创建索引并查看执行计划
3.1 MySQL5.7中创建升序索引
在MySQL8.0中创建升序索引,并执行SQL查看执行计划
# 升序索引
alter table test1 add key idx_nameAsc_createtimeAsc( name,create_time);
执行语句查看执行计划
mysql> explain select * from test1 order by name desc ,create_time ;
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
|1| SIMPLE | test1 | NULL | index | NULL | idx_nameAsc_createtimeAsc | 309 | NULL | 12 |100.00| Using index; Using filesort |
+----+-------------+-------+------------+-------+---------------+---------------------------+---------+------+------+----------+-----------------------------+
结果和MySQL5.7 一致,也是需要进行filesort
3.2 MySQL8.0中创建降序索引
在MySQL8.0中创建降序索引,并执行SQL查看执行计划
# 创建降序索引
alter table test1 add key idx_nameDesc_createtimeAsc( name desc ,create_time);
执行SQL并查看执行计划
mysql> explain select * from test1 order by name desc ,create_time ;
+----+-------------+-------+------------+-------+---------------+----------------------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------------------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | test1 | NULL | index | NULL | idx_nameDesc_createtimeAsc | 409 | NULL | 12 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+----------------------------+---------+------+------+----------+-------------+
可见,MySQL8.0中的降序索引被使用到了,且排序无需进行filesort
3.3 MySQL8.0中查看索引情况
查看索引情况会发现,MySQL8.0中,升序索引及降序索引的排序方式出现了区分了
+-------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test1 | 0 | PRIMARY | 1 | id | A | 12 | NULL | NULL | | BTREE | | | YES | NULL |
| test1 | 1 | idx_nameAsc_createtimeAsc | 1 | name | A | 10 | NULL | NULL | YES | BTREE | | | YES | NULL |
| test1 | 1 | idx_nameAsc_createtimeAsc | 2 | create_time | A | 12 | NULL | NULL | YES | BTREE | | | YES | NULL |
| test1 | 1 | idx_nameDesc_createtimeAsc | 1 | name | D | 10 | NULL | NULL | YES | BTREE | | | YES | NULL |
| test1 | 1 | idx_nameDesc_createtimeAsc | 2 | create_time | A | 12 | NULL | NULL | YES | BTREE | | | YES | NULL |
+-------+------------+----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------
4. 小结
- MySQL5.7中,可以创建降序索引,但只是停留在语法层面上,到MySQL8.0才能使用上降序索引
- 另外,如果在MySQL5.7及之前版本,order by 多个字段时,建议排序方式一致(可以均升序或均降序),这样方可无需filesort
相关推荐
- 一文带您了解数据库的行列之争:行式与列式存储的异同
-
数据库存储格式是数据库管理系统中一个至关重要的方面,它直接影响到数据的组织和检索效率。在数据库中,有两种主要的存储格式,即行式存储和列式存储。这两者采用截然不同的方法来组织和存储数据,各自具有一系列优...
- NL2SQL(三)开源项目怎么选:talk is cheap, show me the code!
-
老规矩,先看效果下面的demo来自试用的SuperSonic,将会在下面详细介绍:大模型时代Text-to-SQL特点随着基于LLM技术的发展,RAG/AIAgent/Fine...
- JDK25长期支持版九月降临:18项王炸功能全解析
-
Java要放大招啦!9月份推出的JDK25长期支持版已经锁定18个超能力,从稳定值到结构化并发,还有Linux系统下的"预知未来"性能分析!下面我用打游戏的术语给你们掰扯明白:1、飞...
- OceanBase 推出单机版 高度兼容MySQL和Oracle
-
【环球网科技综合报道】3月27日,独立数据库厂商OceanBase正式发布单机版产品。据悉,这一产品基于自主研发的单机分布式一体化架构设计,具备极简数据库架构和高度兼容性,为中小规模业务提供兼具性能与...
- 黄远邦:应对7月1日闰秒对Oracle数据库影响
-
由于今年7月1日全世界会多出一秒,这可能对时间敏感的IT系统造成较大影响。中亦科技数据库团队对此问题做了深入的研究,并对用户系统提出了相应的解决方法及建议。中亦科技数据库产品总监黄远邦认为,闰秒调整会...
- MySQL数据库密码忘记了,怎么办?(mysql 数据库密码)
-
#头条创作挑战赛#MySQL数据库密码忘记了且没有其他可以修改账号密码的账户时怎么办呢?登录MySQL,密码输入错误/*密码错误,报如下错误*/[root@TESTDB~]#mysql-u...
- Chinese AI Talent in Spotlight as Nvidia and Meta Escalate Talent War
-
OntherightisBanghuaZhu,ChiefResearchScientistatNVIDIATMTPOST--SiliconValley’stoptech...
- 用Cursor开启JAVA+AI生涯(javascirpt怎么开启)
-
Cursor是基于VSCode开发的一款编辑器,支持多种语言的开发编辑。与传统的开发工具相比,它有多种优势:与AI无缝集成,响应速度快,占用内存小。但很多同学在"起步"过程中遇到了...
- 毕业十年了,自从做了开发用了很多软件,但距离写开发工具还很远
-
办公系统类:办公软件Word、Excel、PowerPoint三大必备技能+腾讯/金山在线文档解压缩操作:7-zip/winrar文件文本处理:Notepad++(文本编辑器正则表达式超级好...
- 盘点Java中最没用的知识⑤:这3个老古董你还在代码里“考古”?
-
一、Stack类:“继承Vector”的历史bug,为何成了性能拖油瓶?你是不是在学Java集合时,老师说过“栈结构用Stack类”?是不是在老代码里见过"newStack<>(...
- Gemini 2.5 Pro 0506发布,编程最强大模型, 碾压 Claude3.7 sonnent
-
一、Gemini2.5Pro(I/Oedition)发布1、为何叫I/Oedition?谷歌史上最强编程模型Gemini2.5Pro(I/Oedition)发布,具体型号是Gemin...
- 如何让无聊变得有趣(附本人大量美图)
-
文/图:金冬成在这条长300公里的公路上,我已经来回往返了无数次。3小时车程,一个人,想想都是多么无聊的一件事。其实,人生道路上,类似这种无聊的事情有很多很多。无聊的事情、枯燥的工作,往往让我们容易失...
- Oracle 推出 Java 24,增强 AI 支持和后量子加密
-
导读:Oracle宣布正式发布Java24,该语言增加了几个新功能,例如StreamGatherersAPI和Class-FileAPI的可用性,以及专门为AI推理和量子安全设计...
- 公司ERP突然变慢?“索引重建”这颗“药”可不能随便吃!
-
各位老板、IT小哥、财务小姐姐,有没有遇到过公司ERP系统突然卡顿得像“老爷车”,点个按钮半天没反应,急得直跺脚?这时候,可能有人会跳出来说:“我知道,重建一下数据库索引就好了!”听起来像个“神操作”...
- 基于Java实现,支持在线发布API接口读取数据库,有哪些工具?
-
基于java实现,不需要编辑就能发布api接口的,有哪些工具、平台?还能一键发布、快速授权和开放提供给第三方请求调用接口的解决方案。架构方案设计:以下是一些基于Java实现的无需编辑或只需少量编辑...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 一文带您了解数据库的行列之争:行式与列式存储的异同
- NL2SQL(三)开源项目怎么选:talk is cheap, show me the code!
- JDK25长期支持版九月降临:18项王炸功能全解析
- OceanBase 推出单机版 高度兼容MySQL和Oracle
- 黄远邦:应对7月1日闰秒对Oracle数据库影响
- MySQL数据库密码忘记了,怎么办?(mysql 数据库密码)
- Chinese AI Talent in Spotlight as Nvidia and Meta Escalate Talent War
- 用Cursor开启JAVA+AI生涯(javascirpt怎么开启)
- 毕业十年了,自从做了开发用了很多软件,但距离写开发工具还很远
- 盘点Java中最没用的知识⑤:这3个老古董你还在代码里“考古”?
- 标签列表
-
- oracle位图索引 (74)
- oracle批量插入数据 (65)
- oracle事务隔离级别 (59)
- oracle 空为0 (51)
- oracle主从同步 (55)
- oracle 乐观锁 (51)
- redis 命令 (78)
- php redis (88)
- redis 存储 (66)
- redis 锁 (69)
- 启动 redis (66)
- redis 时间 (56)
- redis 删除 (67)
- redis内存 (57)
- redis并发 (52)
- redis 主从 (69)
- redis 订阅 (51)
- redis 登录 (54)
- redis 面试 (58)
- 阿里 redis (59)
- redis 搭建 (53)
- redis的缓存 (55)
- lua redis (58)
- redis 连接池 (61)
- redis 限流 (51)