博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL联合索引最左匹配范例
阅读量:6552 次
发布时间:2019-06-24

本文共 19484 字,大约阅读时间需要 64 分钟。

MySQL联合索引最左匹配范例参考文章:http://blog.jobbole.com/24006/创建示例表。示例表来自MySQL官方文档: https://dev.mysql.com/doc/employee/en/CREATE TABLE titles (emp_no      INT             NOT NULL,title       VARCHAR(50)     NOT NULL,from_date   DATE            NOT NULL,to_date     DATE,PRIMARY KEY (emp_no,title,from_date)) ;导入测试数据  load_titles.dump https://github.com/datacharmer/test_db索引情况mysql> show index from titles;+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comme+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------| titles |          0 | PRIMARY  |            1 | emp_no      | A         |           0 |     NULL | NULL   |      | BTREE      |      | titles |          0 | PRIMARY  |            2 | title       | A         |           0 |     NULL | NULL   |      | BTREE      |      | titles |          0 | PRIMARY  |            3 | from_date   | A         |           0 |     NULL | NULL   |      | BTREE      |      +--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+------3 rows in set (0.00 sec)情况一.WHERE条件中全部列都在索引中 不管WHERE的顺序如何执行计划都可以完整用到索引。mysql> desc SELECT * FROM titles WHERE emp_no='10001' AND title='Senior Engineer' AND from_date='1986-06-26';+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref               | rows | filtered | Extra |+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+|  1 | SIMPLE      | titles | NULL       | const | PRIMARY       | PRIMARY | 159     | const,const,const |    1 |   100.00 | NULL  |+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE title='Senior Engineer' AND emp_no='10001' AND from_date='1986-06-26';+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref               | rows | filtered | Extra |+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+|  1 | SIMPLE      | titles | NULL       | const | PRIMARY       | PRIMARY | 159     | const,const,const |    1 |   100.00 | NULL  |+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+1 row in set, 1 warning (0.01 sec)mysql> desc SELECT * FROM titles WHERE from_date='1986-06-26' AND title='Senior Engineer' AND emp_no='10001';+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref               | rows | filtered | Extra |+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+|  1 | SIMPLE      | titles | NULL       | const | PRIMARY       | PRIMARY | 159     | const,const,const |    1 |   100.00 | NULL  |+----+-------------+--------+------------+-------+---------------+---------+---------+-------------------+------+----------+-------+1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE emp_no='10001' AND title in('Senior Engineer','Staff','Assistant Engineer') AND from_date='1986-06-26';+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | range | PRIMARY       | PRIMARY | 159     | NULL |    3 |   100.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql>  desc SELECT * FROM titles WHERE emp_no in('10061','10062','10063','10064') AND title in('Senior Engineer','Staff','Assistant Engineer') AND from_date='1986-06-26';+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | range | PRIMARY       | PRIMARY | 159     | NULL |   12 |   100.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.00 sec) mysql> desc SELECT * FROM titles WHERE emp_no='10001' AND title='Senior Engineer' AND from_date>='1986-06-26';+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | range | PRIMARY       | PRIMARY | 159     | NULL |    1 |   100.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.00 sec) 情况二.WHERE条件和ORDER BY中全部列都在索引中,索引最左列在WHERE条件中, 不管ORDER BY是ASC还是DESC,执行计划都可以完整用到索引。 mysql> desc SELECT * FROM titles WHERE emp_no='10001' AND title='Senior Engineer' order by from_date;+----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref         | rows | filtered | Extra       |+----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | ref  | PRIMARY       | PRIMARY | 156     | const,const |    1 |   100.00 | Using where |+----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+1 row in set, 1 warning (0.00 sec)  mysql> desc SELECT * FROM titles WHERE emp_no='10001' AND title='Senior Engineer' order by from_date desc;+----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref         | rows | filtered | Extra       |+----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | ref  | PRIMARY       | PRIMARY | 156     | const,const |    1 |   100.00 | Using where |+----+-------------+--------+------------+------+---------------+---------+---------+-------------+------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE emp_no='10001' order by title,from_date;+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref   | rows | filtered | Extra       |+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | ref  | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | Using where |+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+1 row in set, 1 warning (0.01 sec)mysql> desc SELECT * FROM titles WHERE emp_no='10001' order by title,from_date desc;+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-----------------------------+| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref   | rows | filtered | Extra                       |+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-----------------------------+|  1 | SIMPLE      | titles | NULL       | ref  | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | Using where; Using filesort |+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-----------------------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE emp_no='10001' order by title desc,from_date desc; +----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+| id | select_type | table  | partitions | type | possible_keys | key     | key_len | ref   | rows | filtered | Extra       |+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | ref  | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | Using where |+----+-------------+--------+------------+------+---------------+---------+---------+-------+------+----------+-------------+1 row in set, 1 warning (0.00 sec)情况三.当WHERE条件和ORDER BY中全部列都在索引中,索引最左列不在WHERE条件中而是在ORDER BY中执行计划会走index。mysql> desc SELECT * FROM titles WHERE title='Senior Engineer' order by emp_no,from_date;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |    10.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE title='Senior Engineer' order by emp_no desc,from_date desc;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |    10.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE from_date='1986-06-26' order by emp_no,title;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |    10.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE from_date='1986-06-26' order by emp_no desc,title desc; +----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |    10.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE title='Senior Engineer' AND from_date='1986-06-26'  order by emp_no;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |     1.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE from_date='1986-06-26' AND title='Senior Engineer' order by emp_no;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |     1.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE title='Senior Engineer' AND from_date='1986-06-26'  order by emp_no desc;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |     1.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles WHERE from_date='1986-06-26' AND title='Senior Engineer' order by emp_no desc;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra       |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |     1.00 | Using where |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------------+1 row in set, 1 warning (0.00 sec)情况四.当WHERE条件无过滤,索引列全在ORDER BY中1.当order by 中ASC和DESC顺序和索引一致执行计划会走INDEX,如果不一致则会走全表扫描。mysql> desc SELECT * FROM titles order by emp_no,title,from_date;+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------+| id | select_type | table  | partitions | type  | possible_keys | key     | key_len | ref  | rows   | filtered | Extra |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------+|  1 | SIMPLE      | titles | NULL       | index | NULL          | PRIMARY | 159     | NULL | 441772 |   100.00 | NULL  |+----+-------------+--------+------------+-------+---------------+---------+---------+------+--------+----------+-------+1 row in set, 1 warning (0.01 sec) mysql> desc SELECT * FROM titles order by emp_no,title desc,from_date desc; +----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra          |+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+|  1 | SIMPLE      | titles | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 441772 |   100.00 | Using filesort |+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles order by emp_no desc,title,from_date;+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra          |+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+|  1 | SIMPLE      | titles | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 441772 |   100.00 | Using filesort |+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+1 row in set, 1 warning (0.00 sec)mysql> desc SELECT * FROM titles order by emp_no,title desc,from_date desc; +----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+| id | select_type | table  | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra          |+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+|  1 | SIMPLE      | titles | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 441772 |   100.00 | Using filesort |+----+-------------+--------+------------+------+---------------+------+---------+------+--------+----------+----------------+1 row in set, 1 warning (0.00 sec)

 

转载于:https://www.cnblogs.com/liang545621/p/9454573.html

你可能感兴趣的文章
Dom4j学习笔记
查看>>
C语言 HTTP上传文件-利用libcurl库上传文件
查看>>
[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
查看>>
Hibernate 的HQL语句,初级
查看>>
调试逆向分为动态分析技术和静态分析技术(转)
查看>>
Android webview使用详解
查看>>
业务对象和BAPI
查看>>
程序源系统与当前系统不一致:Carry out repairs in non-original systems only if urgent
查看>>
微软职位内部推荐-Senior Software Engineer
查看>>
程序中的魔鬼数字
查看>>
SVN高速新手教程
查看>>
session cookie
查看>>
如何在Vblock里配置Boot from SAN
查看>>
ZBar之ZBarReaderViewController
查看>>
Android学习笔记——Handler(一)
查看>>
Nuget~管理自己的包包~丢了的包包快速恢复
查看>>
Hadoop单机模式安装-(3)安装和配置Hadoop
查看>>
$.extend({},defaults, options) --(初体验三)
查看>>
自己主动瀑布流布局和实现代码加载
查看>>
maven的一些依赖
查看>>