mysql联合索引的选择性解析

本篇内容介绍了“MySQL联合索引的选择性解析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

十载的克州网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整克州建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“克州网站设计”,“克州网站推广”以来,每个客户项目都认真落实执行。

通过下面的实验来探讨下联合索引的使用选择性:

版本:percona 5.6.27

create table test(

a int,

b int,

c int,

name varchar(32),

PRIMARY key(a),

key index_a_b_c(a,b,c)) ENGINE=INNODB

insert into test values(1,1,1,3,'leo');

insert into test values(2,1,2,1,'mike' );

insert into test values(3,1,3,1,'exo' );

insert into test values(4,1,2,3,'jhon' );

insert into test values(5,1,1,3,'lucy' );

insert into test values(6,2,2,3,'leo' );

insert into test values(7,3,1,2,'dv' );

insert into test values(8,2,1,3,'men' );

一:where条件对联合索引的选择性

mysql> explain select * from test where a=2;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | NULL  |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------+

mysql> explain select * from test where a=2 and b=1;

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+

| id | select_type | table | type | possible_keys | key         | key_len | ref         | rows | Extra |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 10      | const,const |    1 | NULL  |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------+

mysql> explain select * from test where a=2 and b=2 and c=3;

+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

| id | select_type | table | type | possible_keys | key         | key_len | ref               | rows | Extra |

+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 15      | const,const,const |    1 | NULL  |

+----+-------------+-------+------+---------------+-------------+---------+-------------------+------+-------+

这三个是正常的使用方法,都走了索引

mysql> explain select * from test where a=2 and c=3;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra                 |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using index condition |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------+

1 row in set (0.00 sec)

如果把b漏掉,同样走了索引

mysql> explain select * from test where b=2 and c=3;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

1 row in set (0.00 sec)

mysql> explain select * from test where c=3;

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    8 | Using where |

+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

如果把a漏掉,则不会走索引

结论:必须有联合索引的第一个字段作为wehre条件

二:联合索引排序选择性

联合索引的排序会按照(a,b,c)的顺序进行排序

测试数据在联合索引的排序会是(1,1,3), (1,2,1), (1,2,3), (1,3,1), (2,1,3), (2,2,3), (3,1,2)顺序存储

mysql> explain select * from test where a=2 order by b;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra       |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using where |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-------------+

mysql> explain select * from test where a=2 order by c;

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref   | rows | Extra                       |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 5       | const |    2 | Using where; Using filesort |

+----+-------------+-------+------+---------------+-------------+---------+-------+------+-----------------------------+

可以看出第二个Using filesort使用了临时表排序了,效率低。从联合索引的排序就可以知道当指定a的值的时候,这些值会按b的值排序,不是按c的值排序,故order by b不用再filesort排序,反之order by b需要重新排序。

所以select * from test where a=2 and b=2 order by c;不会 filesort排序

mysql> explain select * from test where a=2 and b=2 order by c;

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+

| id | select_type | table | type | possible_keys | key         | key_len | ref         | rows | Extra       |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+

|  1 | SIMPLE      | test  | ref  | index_a_b_c   | index_a_b_c | 10      | const,const |    1 | Using where |

+----+-------------+-------+------+---------------+-------------+---------+-------------+------+-------------+

结论:当针对联合索引中的某个字段进行排序的时候,最优的方法是有联合索引排序字段之前的字段过滤条件

“mysql联合索引的选择性解析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网页标题:mysql联合索引的选择性解析
文章位置:http://bzwzjz.com/article/pegosj.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆网站建设 外贸网站设计方案 响应式网站设计方案 网站制作公司 重庆电商网站建设 网站制作 成都企业网站设计 成都商城网站建设 温江网站设计 成都定制网站建设 成都企业网站建设公司 成都网站建设公司 高端网站建设 网站制作 攀枝花网站设计 响应式网站建设 营销型网站建设 成都网站建设公司 成都网站制作 成都网站设计制作公司 高端网站设计 成都企业网站建设