对mysql使用索引的误解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a454213722/article/details/55003033
 
 

之前我一直以为mysql一个查询中只能使用一个索引,网上的资料有这个说的,而且我也以为是这样的,结果今天发现错了

主要是在验证只使用一个索引的时候发现,key中显示了两个索引字段,type中显示index_merge
如图

mysql explain

如果按照之前的理解只使用个索引,那就有问题了,那为什么有人会说只使用一个索引

答案在这里版本问题

MySQL5.0之前,一个表一次只能使用一个索引,无法同时使用多个索引分别进行条件扫描,但是从5.1开始,引入了 index merge 优化技术,对同一个表可以使用多个索引分别进行条件扫描

不能在相信百度里的资料了,查官方文档
官方文档:  http://dev.mysql.com/doc/refman/5.7/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.

索引合并方法用于与几个范围扫描检索行和他们的结果合并为一个。合并分为union, intersection, 以及它们的组合(先内部intersect然后在外面union)。这种访问方式合并来自单个表索引扫描;它不合并跨多个表扫描。

In EXPLAIN output, the Index Merge method appears as index_merge in the type column. In this case, the key column contains a list of indexes used, and key_len contains a list of the longest key parts for those indexes.

在EXPLAIN输出,索引合并方法出现在类型列index_merge。在这种情况下,键列包含用于索引的列表,并且key_len包含这些索引的最长键部件的列表

说的够明白了
参考: http://www.cnblogs.com/digdeep/archive/2015/11/18/4975977.html


QQ交流群:136351212
查看原文:

猜你喜欢

转载自blog.csdn.net/a454213722/article/details/55003033