mysql where and 使用的索引顺序
来源:SegmentFault
时间:2023-01-27 15:31:21 290浏览 收藏
本篇文章向大家介绍《mysql where and 使用的索引顺序》,主要包括MySQL、索引,具有一定的参考价值,需要的朋友可以参考一下。
mysql> select * from test; +----+------+------+ | id | a | b | +----+------+------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 1 | 2 | | 4 | 2 | 2 | | 5 | 2 | 2 | | 6 | 3 | 3 | | 7 | 4 | 4 | | 8 | 4 | 4 | +----+------+------+ 69 rows in set (0.00 sec)
mysql> show index from test; +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | test | 0 | PRIMARY | 1 | id | A | 69 | NULL | NULL | | BTREE | | | | test | 1 | a | 1 | a | A | 2 | NULL | NULL | YES | BTREE | | | | test | 1 | b | 1 | b | A | 2 | NULL | NULL | YES | BTREE | | | +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 3 rows in set (0.00 sec)
EXPLAIN select * from test where b=31 and a=31;
mysql> EXPLAIN select * from test where b=31 and a=31; +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | test | NULL | ref | a,b | a | 5 | const | 1 | 5.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
结果说明:a与b同时不满足条件,使用了最左索引 a
mysql> EXPLAIN select * from test where b=11 and a=1; +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | test | NULL | ref | a,b | b | 5 | const | 1 | 5.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.01 sec)
结果说明:a满足条件,b不满足条件,使用了索引 b
mysql> EXPLAIN select * from test where b=1 and a=11; +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | test | NULL | ref | a,b | a | 5 | const | 1 | 5.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
结果说明:a不满足条件,b满足条件,使用了索引 a
mysql> EXPLAIN select * from test where b=1 and a=1; +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | test | NULL | ref | a,b | b | 5 | const | 1 | 5.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
结果说明:a满足条件,b满足条件,使用了索引符合该条件的行数少的索引b
explain select * from test where b=2 and a=1; +----+-------------+-------+------------+-------------+---------------+------+---------+------+------+----------+------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------------+---------------+------+---------+------+------+----------+------------------------------------------------+ | 1 | SIMPLE | test | NULL | index_merge | a,b | a,b | 5,5 | NULL | 2 | 100.00 | Using intersect(a,b); Using where; Using index | +----+-------------+-------+------------+-------------+---------------+------+---------+------+------+----------+------------------------------------------------+ 1 row in set, 1 warning (0.00 sec)
结果说明:a满足条件,b满足条件,最左索引a符合条件少于b,使用了索引 a,b
mysql> explain select * from test where b=3 and a=3; +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ | 1 | SIMPLE | test | NULL | ref | a,b | a | 5 | const | 1 | 16.67 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+-------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
结果说明:a满足条件,b满足条件,最左索引a符合条件等于于b,使用了最左索引 a
mysql> explain select * from test where b=4 and a=4; +----+-------------+-------+------------+-------------+---------------+------+---------+------+------+----------+------------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------------+---------------+------+---------+------+------+----------+------------------------------------------------+ | 1 | SIMPLE | test | NULL | index_merge | a,b | a,b | 5,5 | NULL | 1 | 100.00 | Using intersect(a,b); Using where; Using index | +----+-------------+-------+------------+-------------+---------------+------+---------+------+------+----------+------------------------------------------------+ 1 row in set, 1 warning (0.00 sec
结果说明:a满足条件,b满足条件,最左索引a符合条件等于b(大于2),使用了索引 a,b
总结:根据实验大概得知结果
- and查询会根据关联性高(符合该条件的行数少)选择具体走哪个索引,
- 如果关联性相等(符合该条件的行数相同),满足一条,会走最左索引,大于一条,会走两个索引
- and查询会根据关联性高(符合该条件的行数少),最左索引最小,会选择两个索引,
- 两个都不满足,走最左索引
以上测试结果仅供参考,如有错误,欢迎指正
今天带大家了解了MySQL、索引的相关知识,希望对你有所帮助;关于数据库的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
371 收藏
-
244 收藏
-
288 收藏
-
480 收藏
-
430 收藏
-
120 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习