mysql聚合索引失效
索引是可以高效的获取数据的数据结构, 对查询有很大作用. 索引对于数据库, 就像偏旁部首, 或者26个英文字母对于字典一样, 能很快的提高查询速度.但是索引也有它的缺点:首先就是占用磁盘空间其次是虽然...
2024.11.15使用过Oracle、SQLServer数据库的降序索引的同学,可能在使用MySQL8.0之前版本时有个疑惑,明明我已经创建了将需要索引,但是为何执行时走不了索引或者效果不理想?
1. 创建环境
分别在MySQL5.7 及MySQL8.0版本中创建如下表及数据
# 创建表 create tabletest1( id int primary key auto_increment, namevarchar(100), create_time datetime );# 插入部分测试数据,有条件的创建更多数据更佳 insert intotest1(name,creatE_time) values(‘anniuadaOAIFAPUHIA‘,‘2020-07-01 12:00:00‘); insert intotest1(name,creatE_time) values(‘CWQSsar3qcssg‘,‘2020-07-01 15:00:00‘); insert intotest1(name,creatE_time) values(‘vxfqrt2adafz‘,‘2020-07-01 21:30:00‘); insert intotest1(name,creatE_time) values(‘etxzwrwbdhegqgaheqhag‘,‘2020-07-02 01:30:00‘); insert intotest1(name,creatE_time) values(‘awrs433fsgvsfwtwg‘,‘2020-07-02 03:30:00‘); insert intotest1(name,creatE_time) values(‘awrs433fsgvsfwtwg‘,‘2020-07-02 07:32:00‘); insert intotest1(name,creatE_time) values(‘awrs433fsgvsfwtwg‘,‘2020-07-02 10:32:00‘); insert intotest1(name,creatE_time) values(‘tuilklmdadq‘,‘2020-07-02 15:32:00‘); insert intotest1(name,creatE_time) values(‘wesv2wqdshehq‘,‘2020-07-02 20:32:00‘); insert intotest1(name,creatE_time) values(‘89yoijnlkwr1‘,‘2020-07-03 02:56:00‘); insert intotest1(name,creatE_time) values(‘olj;nsaaq‘,‘2020-07-03 08:41:00‘); insert intotest1(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 keyidx_nameAsc_createtimeAsc( name,create_time);执行语句查看执行计划
mysql> explainselect * fromtest1 order byname 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 keyidx_nameDesc_createtimeAsc( name desc ,create_time);执行SQL并查看执行计划
mysql> explainselect * fromtest1 order byname 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 keyidx_nameAsc_createtimeAsc( name,create_time);执行语句查看执行计划
mysql> explainselect * fromtest1 order byname 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 keyidx_nameDesc_createtimeAsc( name desc ,create_time);执行SQL并查看执行计划
mysql> explainselect * fromtest1 order byname 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索引是可以高效的获取数据的数据结构, 对查询有很大作用. 索引对于数据库, 就像偏旁部首, 或者26个英文字母对于字典一样, 能很快的提高查询速度.但是索引也有它的缺点:首先就是占用磁盘空间其次是虽然...
2024.11.15看了很多关于索引的博客,讲的大同小异。但是始终没有让我明白关于索引的一些概念,如B-Tree索引,Hash索引,唯一索引….或许有很多人和我一样,没搞清楚概念就开始研究B-Tree,B+Tree等结构...
2024.11.15四种mysql存储引擎前言数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建、查询、更新和删除数据。不同的存储引擎提供不同的存储机制、索引技巧、锁定水平等功能,使用不同...
2024.11.13作为一名java程序员,对于mysql的索引相信都不陌生。当我们在数据库表中查询数据时,若数据表没有索引,会逐个遍历表格中的所有数据,要是遇到表的数据很大时,查询就会很耗时。建立索引就像创建目录一样,...
2024.11.15在编写SQL 语句时常常会用到 order by 进行排序,那么排序过程是什么样的?为什么有些排序执行比较快,有些排序执行很慢?又该如何去优化?索引排序索引排序指的是在通过索引查询时就完成了排序,从而...
2024.11.14