MySQL 联合索引测试
搭建测试环境
1:创建表
CREATE TABLE tab_index
(id int(5),
age int(3),
dte datetime);
2:插入测试数据
INSERT INTO tab_index
VALUES(1,'2012-05-13',23);
INSERT INTO tab_index
VALUES(2,'2012-05-13',23);
INSERT INTO tab_index
VALUES(3,'2012-05-13',31);
INSERT INTO tab_index
VALUES(4,'2012-05-13',32);
INSERT INTO tab_index
VALUES(5,'2012-05-13',33);
INSERT INTO tab_index
VALUES(6,'2012-06-13',34);
INSERT INTO tab_index
VALUES(7,'2012-07-13',35);
INSERT INTO tab_index
VALUES(8,'2012-08-13',36);
INSERT INTO tab_index
VALUES(9,'2012-09-13',37);
INSERT INTO tab_index
VALUES(10,'2012-05-17',38);
INSERT INTO tab_index
VALUES(11,'2012-05-19',39);
INSERT INTO tab_index
VALUES(1,'2012-05-13',23);
INSERT INTO tab_index
VALUES(2,'2012-05-13',23);
INSERT INTO tab_index
VALUES(3,'2012-05-13',31);
INSERT INTO tab_index
VALUES(4,'2012-05-13',32);
INSERT INTO tab_index
VALUES(5,'2012-05-13',33);
INSERT INTO tab_index
VALUES(6,'2012-06-13',34);
INSERT INTO tab_index
VALUES(7,'2012-07-13',35);
INSERT INTO tab_index
VALUES(8,'2012-08-13',36);
INSERT INTO tab_index
VALUES(9,'2012-09-13',37);
INSERT INTO tab_index
VALUES(10,'2012-05-17',38);
INSERT INTO tab_index
VALUES(11,'2012-05-19',39);
INSERT INTO tab_index
VALUES(1,'2012-05-13',23);
INSERT INTO tab_index
VALUES(2,'2015-05-13',23);
INSERT INTO tab_index
VALUES(3,'2012-05-13',31);
INSERT INTO tab_index
VALUES(4,'2012-05-13',32);
INSERT INTO tab_index
VALUES(5,'2012-05-13',33);
INSERT INTO tab_index
VALUES(6,'2012-06-13',34);
INSERT INTO tab_index
VALUES(7,'2013-07-13',35);
INSERT INTO tab_index
VALUES(8,'2012-08-13',36);
INSERT INTO tab_index
VALUES(9,'2012-09-13',37);
INSERT INTO tab_index
VALUES(10,'2012-05-17',38);
INSERT INTO tab_index
VALUES(11,'2011-05-19',39);
INSERT INTO tab_index
VALUES(1,'2012-05-13',23);
INSERT INTO tab_index
VALUES(2,'2012-05-13',23);
INSERT INTO tab_index
VALUES(3,'2010-05-13',31);
INSERT INTO tab_index
VALUES(4,'2012-05-13',32);
INSERT INTO tab_index
VALUES(5,'2010-05-13',33);
INSERT INTO tab_index
VALUES(6,'2010-06-13',34);
INSERT INTO tab_index
VALUES(7,'2012-07-13',35);
INSERT INTO tab_index
VALUES(8,'2012-08-13',36);
INSERT INTO tab_index
VALUES(9,'2011-09-13',37);
INSERT INTO tab_index
VALUES(10,'2012-05-17',38);
INSERT INTO tab_index
VALUES(11,'2012-05-19',39);
INSERT INTO tab_index
VALUES(1,'2012-05-13',23);
INSERT INTO tab_index
VALUES(2,'2012-05-13',23);
INSERT INTO tab_index
VALUES(3,'2012-05-13',31);
INSERT INTO tab_index
VALUES(4,'2012-05-13',32);
INSERT INTO tab_index
VALUES(5,'2012-05-13',33);
INSERT INTO tab_index
VALUES(6,'2012-06-13',34);
INSERT INTO tab_index
VALUES(7,'2014-07-13',35);
INSERT INTO tab_index
VALUES(8,'2012-08-13',36);
INSERT INTO tab_index
VALUES(9,'2011-09-13',37);
INSERT INTO tab_index
VALUES(10,'2012-05-17',38);
INSERT INTO tab_index
VALUES(11,'2012-05-19',39);
3:创建id和age上的联合索引
CREATE INDEX idx1
ON tab_index(id,age);
4:开始测试
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE id=3;
+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 5 | const | 5 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+
1 row in set (0.00 sec)
id=3走索引,age=31不走索引。很容易理解
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE age=31;
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | tab_index | ALL | NULL | NULL | NULL | NULL | 55 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE id=3 AND age=31;
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
id=3 AND age=31 和 age=31 AND id=3都走索引了,但是索引长度跟之前不同
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE age=31 AND id=3 ;
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE id=3 AND age=31 AND dte BETWEEN '2011-05-13 00:00:00' AND '2013-05-13 00:00:00';
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE id=3 AND dte BETWEEN '2011-05-13 00:00:00' AND '2013-05-13 00:00:00' AND age=31;
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE dte BETWEEN '2011-05-13 00:00:00' AND '2013-05-13 00:00:00' AND id=3 AND age=31;
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE age=31 AND id=3 AND dte BETWEEN '2011-05-13 00:00:00' AND '2013-05-13 00:00:00';
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE age=31 AND dte BETWEEN '2011-05-13 00:00:00' AND '2013-05-13 00:00:00' AND id=3 ;
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN SELECT id,dte,age
-> FROM tab_index
-> WHERE dte BETWEEN '2011-05-13 00:00:00' AND '2013-05-13 00:00:00' AND age=31 AND id=3 ;
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| 1 | SIMPLE | tab_index | ref | idx1 | idx1 | 10 | const,const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.00 sec)
另外其他几种情况,同学们自己尝试下以加深印象。
总结如下:
(id)走索引,且索引长度最短
(id,dte)走索引,且索引长度最短
(id,age)走索引,且索引长度最长
(id,age,dte)走索引,且索引长度最长
(id,dte,age)走索引,且索引长度最长
(dte,id,age)走索引,且索引长度最长
(dte,age,id)走索引,且索引长度最长
(age,dte,id)走索引,且索引长度最长
(age,id,dte)走索引,且索引长度最长
MySQL 联合索引测试的更多相关文章
- MySQL 联合索引测试2
接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430300.html 1:首先删掉上一篇建立的索引,重新建立一个. mysql> DROP INDEX idx ...
- MySQL 联合索引测试3
接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430387.html 有时候会出现某字段建立一个索引,但是查看执行计划的时候发现还是全扫了表? 可以强制走下索引看看 ...
- SQL Server中的联合主键、聚集索引、非聚集索引、mysql 联合索引
我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...
- 三道MySQL联合索引面试题,淘汰80%的面试者,你能答对几道
众所周知MySQL联合索引遵循最左前缀匹配原则,在少数情况下也会不遵循(有兴趣,可以翻一下上篇文章). 创建联合索引的时候,建议优先把区分度高的字段放在第一列. 至于怎么统计区分度,可以按照下面这种方 ...
- MySQL 联合索引详解
MySQL 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c ...
- mysql 联合索引(转)
http://blog.csdn.net/lmh12506/article/details/8879916 mysql 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中 ...
- MySQL联合索引VS单列索引
MySQL联合索引VS单列索引 以一个一千万数据量的表格为例 1. 建表建索引 USE foo; DROP TABLE IF EXISTS tmp; CREATE TABLE tmp ( id INT ...
- MySQL联合索引最左匹配范例
MySQL联合索引最左匹配范例 参考文章:http://blog.jobbole.com/24006/ 创建示例表. 示例表来自MySQL官方文档: https://dev.mysql.com/doc ...
- [转]mysql联合索引
mysql联合索引 命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果where条件中是OR关系,加索引不起作用4.符合最左原则 https:/ ...
随机推荐
- WordPress 客户端软件列表
Windows: BlogDesk BlogJet Blog Writer Chrysanth WebStory Deepest Sender (Firefox或SeaMonkey扩展,跨平台- De ...
- Go 收藏
Golang 定位解决分布式系统,服务器应用开发,主要竞争对手是 Java.Python 之类:Rust 定位解决单机安全问题,高性能场景偏系统底层开发,主要竞争对手就是 C 和 C++. Golan ...
- 03把IL编译成可执行文件
1.在记事本中编写IL代码如下: .assembly HelloWorld{} .assembly extern mscorlib{} .method public static void Mai ...
- datagrid在MVC中的运用04-同时添加搜索和操作区域
本文介绍在datagrid上同时添加搜索和操作区域. 仅仅是增加操作区域 □ 方法1 $('#dg').datagrid({ toolbar: '#tb' }); <div id="t ...
- Windows 8 Metro 应用开发入门(一):开发环境介绍
摘 要 Windows8已经发布,随之而来的基于WinRT的Metro应用也正向我们走来,正像它所宣传的:光滑.快.现代.看习惯了玻璃.立体风格的应用,或许Metro的简洁能给你留下不一样的体验.Vi ...
- 修改Eclipse/MyEclipse项目的默认编码
应该是中文操作系统的原因,eclipse默认的新项目的编码是GBK,出于对编码支持的考虑,项目组中最好统一要求是UTF-8编码进行开发. 修改eclipse的配置,可以使得eclipse的新建项目的默 ...
- 利用nginx加速web访问
起因---------- 在最近新系统的设计中对于首页打开速度的问题做了一些考虑,由于页面上还是以动态数据为主,所以简单的静态化数据可能并不行,所以需要从业务和技术结合的角度去考虑这个问题.由于有些数 ...
- Linux下安装Oracle的过程和涉及的知识点-系列6
16.一路安装后.会提示下面界面.此时须要用root登录下面文件夹,然后运行这两个脚本. 至此,Oracle软件的安装就已经完毕了,接下来就能够创建数据库了. 17.选择自己定义数据库: 输入数据库名 ...
- 《逆袭大学:传给IT学子的正能量》
<逆袭大学:传给IT学子的正能量> 基本信息 作者: 贺利坚 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115347473 上架时间:2014-3-3 出版日期:201 ...
- C语言条件编译
使用与平台有关的C语言函数,可能会使得程序不具有可移植性.比如Socket编程.多线程编程等是与平台有关的. 若想将程序做成平台无关的就需要用到与平台相关的条件编译. 下面转自:http://blog ...