千家信息网

mysql联合索引的选择性解析

发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,本篇内容介绍了"mysql联合索引的选择性解析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!通过下面
千家信息网最后更新 2025年11月07日mysql联合索引的选择性解析

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

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

版本: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联合索引的选择性解析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

索引 排序 联合 选择性 选择 字段 条件 内容 方法 时候 更多 知识 结论 顺序 实用 学有所成 接下来 三个 下联 不用 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 ark服务器管理器 成都新篇章网络技术有限公司 阿里巴巴网络安全部门 浙江省计算机三级网络技术 旅游网站数据库如何设计 服务器做棋牌数据库 国外有5g网络技术么 拷贝过来的数据库怎么打开 pubmed数据库网页 宣传首都网络安全工作 考研网络安全还是计算机专业好 沈阳商城软件开发放心省心 打电话服务器连接错误是什么原因 信封设计软件开发 网安网络安全保卫工作重点 中华人民网络安全法第二十一条 数据库实例和数据库可以分开么 翻墙后登录服务器安全吗 西宁机架式服务器 网络安全实验设计方向 网络技术部干什么 基于校园网网络安全的设计 数据库实现安全分析 网络安全学历最高的公司 语言处理系统与数据库管理 论文防火墙在网络安全的应用 苦逼的软件开发工程师 oracle进入数据库 用友系统一部分不能连接服务器 mc基岩版无规则服务器
0