MySQL8.0索引新特性:颠覆了我对索引的认知
欢迎关注方志朋的博客,回复”666“获面试宝典
来源:http://dbapub.cn/
函数索引
在MySQL8.0之前对条件字段做函数操作、或者做运算都将不会使用字段上的索引,例如下面的例子
root@employees 14:09: show index from employees;
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| employees | 0 | PRIMARY | 1 | emp_no | A | 299232 | NULL | NULL | | BTREE | | |
| employees | 1 | inx_date | 1 | birth_date | A | 4739 | NULL | NULL | | BTREE | | |
+-----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)root@employees 14:10: explain select * from employees where month(birth_date)=9;
+----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| 1 | SIMPLE | employees | NULL | ALL | NULL | NULL | NULL | NULL | 299232 | 100.00 | Using where |
+----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
可以看到SQL执行计划的type为ALL,并没有利用索引。在MySQL8.0中推出了函数索引的特性,其是通过虚拟列来实现的,接着就来通过函数索引实现相同的需求,看看有什么不同
root@employees 14:35: alter table employees add index idx_birth_date((month(birth_date)));
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0root@employees 14:36: explain select * from employees where month(birth_date)=9;
+----+-------------+-----------+------------+------+----------------+----------------+---------+-------+-------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+------+----------------+----------------+---------+-------+-------+----------+-------+
| 1 | SIMPLE | employees | NULL | ref | idx_birth_date | idx_birth_date | 5 | const | 47370 | 100.00 | NULL |
+----+-------------+-----------+------------+------+----------------+----------------+---------+-------+-------+----------+-------+
需要注意的是函数索引写法是固定时,在使用函数索引时必须依照定义时的语法进行使用,否则优化器无法识别
Index Skip Scan
在存在一个联合索引的情况下,如果查询条件中不包含联合索引的最左字段,则无法使用联合索引。例如存在Index(a,b),现在执行select * from tab where b=1,这时需要针对b字段建立一个单独的索引。MySQL8.0中引入了Index Skip Scan就是用于优化这种场景。
root@employees 15:16: select * from t1;
+------+--------+
| id | score |
+------+--------+
| 0 | 100 |
| 0 | 100 |
| 0 | 200 |
| 0 | 300 |
| 0 | 400 |
| 0 | 500 |
| 0 | 600 |
| 0 | 700 |
| 0 | 800 |
| 1 | 900 |
| 1 | 1000 |
| 1 | 1100 |
| 1 | 1200 |
| 1 | 1300 |
| 1 | 1400 |
| 1 | 1500 |
| 1 | 1600 |
| 2 | 1700 |
| 2 | 1800 |
| 2 | 1900 |
+------+--------+root@employees 15:16: select * from t1 where score>500;
Index Skip Scan会将查询转换为
seect * from tab where a=0 and b>500
union
select * from tab where a=1 and b>500
union
select * from tab where a=2 and b>500
可以看出实际上它是将id字段做了distinct然后作为条件再union拼接起来,这种优化只适用于左边字段唯一性较差的情况,例如性别,状态之类的值,否则优化器则不会使用Index Skip Scan来进行优化
倒序索引
MySQL8.0之前创建索引只支持ASC正向索引,对于一些desc排序的查询并不是很友好,执行计划通常会出现using filesort。
root@employees 15:40: explain select salary from salaries group by salary order by salary desc;
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+-----------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+-----------------------------------------------------------+
| 1 | SIMPLE | salaries | NULL | range | idx_salary | idx_salary | 4 | NULL | 81274 | 100.00 | Using index for group-by; Using temporary; Using filesort |
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+-----------------------------------------------------------+
1 row in set, 1 warning (0.02 sec)root@employees 15:41: explain select salary from salaries group by salary order by salary asc;
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+--------------------------+
| 1 | SIMPLE | salaries | NULL | range | idx_salary | idx_salary | 4 | NULL | 81274 | 100.00 | Using index for group-by |
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+--------------------------+
可以看到倒序排序时,执行计划extra中相对正序多了Using temporary; Using filesort,现在看看8.0中的倒序索引
root@employees 15:43: create index idx_salary on salaries(salary desc);
Query OK, 0 rows affected (7.39 sec)
Records: 0 Duplicates: 0 Warnings: 0root@employees 15:47: explain select salary from salaries group by salary order by salary desc;
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+--------------------------+
| 1 | SIMPLE | salaries | NULL | range | idx_salary | idx_salary | 4 | NULL | 72950 | 100.00 | Using index for group-by |
+----+-------------+----------+------------+-------+---------------+------------+---------+------+-------+----------+--------------------------+
不可见索引
MySQL8.0中引入了隐藏索引,即该索引对优化器不可见,优化器也不会选择该索引,即使使用force index也无法使用。当我们在做优化时需要评估索引的影响,就可以通过隐藏索引来进行。
root@employees 15:47: create index idx_emp on salaries(emp_no) invisible;
Query OK, 0 rows affected (4.12 sec)
Records: 0 Duplicates: 0 Warnings: 0root@employees 15:58: explain select * from salaries force index(idx_emp) where emp_no=10001;
ERROR 1176 (42000): Key 'idx_emp' doesn't exist in table 'salaries'
开启use_invisible_indexes优化器选项后,就可以使用隐藏索引
root@employees 15:58: set @@optimizer_switch='use_invisible_indexes=on';
热门内容:居然有人提问“国家何时整治程序员的高薪现象”?Spring官宣新家族成员:Spring Authorization Server!21 款 yyds 的 IDEA插件Lombok!代码简洁神器还是代码“亚健康”元凶?最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。
明天见(。・ω・。)ノ♡
MySQL8.0索引新特性:颠覆了我对索引的认知相关推荐
- MySQL-8.0.x 新特性之索引页合并
[背景] 索引的重要是在些不表.在这里我想说的另一个问题:索引和数据一样在innodb中都是以page的形式来组织的,那么问题就来了. 比如果说索引 ix_person_name 的内容只要8个页面就 ...
- mysql8.0 的新特性_What's New In MySQL 8.0(MySQL 8.0 新特性)
由于8.0内有很多C++11特性.需要gcc4.8版本以上.Rhel6系列默认gcc是4.7.在安装gcc6.1之后仍然检查不过. 原因可能是6.1版本不一定高于4.7,暂不讨论.鉴于升级gcc耗时较 ...
- MySQL8.0.14 - 新特性 - InnoDB Parallel Read简述
最近的MySQL8.0.14版本增加了其第一个并行查询特性,可以支持在聚集索引上做SELECT COUNT()和check table操作.本文简单的介绍下这个特性. 用法 增加了一个session级 ...
- MySQL8.0.14 - 新特性 - InnoDB Parallel Read简述 1
最近的MySQL8.0.14版本增加了其第一个并行查询特性,可以支持在聚集索引上做SELECT COUNT()和check table操作.本文简单的介绍下这个特性. 用法 增加了一个session级 ...
- mysql8.0 的新特性_MySQL8.0-新特性汇总
大量使用 class 重构代码 明面上: 提高了代码安全性.可维护性,优化继承关系,提高可读性 小算盘: 第三方的开源分支受重创,难以跟上8.0的代码结构 DDL重做原子性秒加列 官方文档: http ...
- .NET Framework 4.0的新特性
本文将揭示.NET 4.0中的3个新特性:图表控件.SEO支持以及ASP.NET 4可扩展的输出缓存. 图表控件 微软向开发者提供了大量可免费下载的图表控件,可以在.NET 3.5 ASP.NET或W ...
- 盘点Greenplum 6.0六大新特性及展望
导读:本文介绍Greenplum 6.0的新特性. 作者:王春波 来源:大数据DT(ID:hzdashuju) Greenplum 6.0于2019年9月4日正式发布,内核版本从PostgreSQL ...
- 高性能数据库引擎 CoolHash 产品宣言 Fourinone4.0版新特性(转)
Fourinone4.0版新特性:一个高性能的数据库引擎CoolHash(酷哈嘻) 一.前言:如何写一个数据库 如果将操作系统和业务应用之间的软件都统称中间件的话,那么最重要的软件无疑是数据库,它比w ...
- Spark 3.2.0 版本新特性 push-based shuffle 论文详解(二)背景和动机
前言 本文隶属于专栏<大数据技术体系>,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢! 本专栏目录结构和参考文献请见大数据技术体系 目录 Spark 3.2.0 ...
- .NET 4.0 Interop新特性ICustomQueryInterface (转载)
.NET 4.0 Interop新特性ICustomQueryInterface 在.NET Framework v4.0发布的新功能中,在名字空间System.Runtime.InteropServ ...
最新文章
- TableStore: 海量结构化数据分层存储方案
- python的模块的导入和包的导入
- Python常用语句及流程控制
- 微信小程序-控制文本只显示若干行多余隐藏
- 学习笔记(10):Python网络编程并发编程-粘包现象
- 餐饮外卖平台小程序 v1.0.1
- SharePoint REST API - 一个请求批量操作
- 服务器中anaconda如何修改环境变量,手动添加anaconda3到环境变量中的方法
- 拓端tecdat|R语言对混合分布中的不可观测与可观测异质性因子分析
- easyui 扩展loading
- java计算机毕业设计大数据在线考试系统在线阅卷系统及大数据统计分析源码+mysql数据库+系统+lw文档+部署
- 蓝桥杯 ALGO-45算法训练 调和数列问题
- 零知识证明在区块链中的应用
- 数字图像处理入门-邻域、连通性、通路和距离
- SpringBoot控制配置类加载顺序
- iOS开发-iOS10新特性及开发者主要注意点
- EJB 3会话bean与Spring的区别
- smbms超市管理系统项目总结
- mysql批量导入json_微信小程序云开发---数据库批量导入json文件
- Python 学习笔记 D1(基础)