加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL优化中index_merge有什么用途

发布时间:2021-12-21 12:12:06 所属栏目:MySql教程 来源:互联网
导读:这篇文章主要介绍MySQL优化中index_merge有什么作用,在日常操作中,相信很多人在MySQL优化中index_merge有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答MySQL优化中index_merge有什么作用的疑惑有所帮助!接下来
这篇文章主要介绍“MySQL优化中index_merge有什么作用”,在日常操作中,相信很多人在MySQL优化中index_merge有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL优化中index_merge有什么作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
 
1. 为什么会有index merge
 
我们的 where 中可能有多个条件(或者join)涉及到多个字段,它们之间进行 AND 或者 OR,那么此时就有可能会使用到 index merge 技术。index merge 技术如果简单的说,其实就是:对多个索引分别进行条件扫描,然后将它们各自的结果进行合并(intersect/union)。
 
MySQL5.0之前,一个表一次只能使用一个索引,无法同时使用多个索引分别进行条件扫描。但是从5.1开始,引入了 index merge 优化技术,对同一个表可以使用多个索引分别进行条件扫描。
 
相关文档:http://dev.mysql.com/doc/refman/5.6/en/index-merge-optimization.html (注意该文档中说的有几处错误)
 
The Index Merge method is used to retrieve rows with several range scans and to merge their results into one. The merge can produce unions, intersections, or unions-of-intersections of its underlying scans. This access method merges index scans from a single table; it does not merge scans across multiple tables.
 
2. index merge 之 intersect
 
简单而言,index intersect merge就是多个索引条件扫描得到的结果进行交集运算。显然在多个索引提交之间是 AND 运算时,才会出现 index intersect merge. 下面两种where条件或者它们的组合时会进行 index intersect merge:
 
3. index merge 之 union
 
简单而言,index uion merge就是多个索引条件扫描,对得到的结果进行并集运算,显然是多个条件之间进行的是 OR 运算。
 
下面几种类型的 where 条件,以及他们的组合可能会使用到 index union merge算法:
 
1) 条件使用到复合索引中的所有字段或者左前缀字段(对单字段索引也适用)
 
2) 主键上的任何范围条件
 
3) 任何符合 index intersect merge 的where条件;
 
上面三种 where 条件进行 OR 运算时,可能会使用 index union merge算法。
 
4. index merge 之 sort_union
 
This access algorithm is employed when the WHERE clause was converted to several range conditions combined by OR, but for which the Index Merge method union algorithm is not applicable.(多个条件扫描进行 OR 运算,但是不符合 index union merge算法的,此时可能会使用 sort_union算法)
 
5. index merge的局限
 
1)If your query has a complex WHERE clause with deep AND/OR nesting and MySQL does not choose the optimal plan, try distributing terms using the following identity laws:
 
6. 对 index merge 的进一步优化
 
index merge使得我们可以使用到多个索引同时进行扫描,然后将结果进行合并。听起来好像是很好的功能,但是如果出现了 index intersect merge,那么一般同时也意味着我们的索引建立得不太合理,因为 index intersect merge 是可以通过建立 复合索引进行更一步优化的。
 
7. 复合索引的最左前缀原则
 
上面我们说到,对复合索引的非最左前缀字段进行 OR 运算,是无法使用到复合索引的
 
SQL如下:
select cd.coupon_id, count(1) total from AAA cd
where  cd.coupon_act_id = 100476 and cd.deleted=0 and cd.pick_time is not null
group by cd.coupon_id ;
在AAA表中,coupon_act_id 和 deleted 都是独立的索引
select count(*) from AAA  where coupon_act_id = 100476;   结果为12360行
select count(*) from AAA where deleted=0;  结果为1300W行
从上面的解释我们可以看出来,index merge其实就是分别通过对两个独立的index进行过滤之后,将过滤之后的结果聚合在一起,然后在返回结果集。
在我们的这个例子中,由于deleted字段的过滤性不好,故返回的rows依然很多,所以造成的很多的磁盘read,导致了cpu的负载非常的高,直接就出现了延迟。
ps:其实在这个case中,并不需要加2个条件的index,只需要将deleted这个index干掉,直接使用coupon_act_id这个index即可,毕竟这个index的过滤的结果集已经很小了。
或者通过关闭index intersect功能也可以。
 
到此,关于“MySQL优化中index_merge有什么作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读