搭建测试环境

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 联合索引测试的更多相关文章

  1. MySQL 联合索引测试2

    接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430300.html 1:首先删掉上一篇建立的索引,重新建立一个. mysql> DROP INDEX idx ...

  2. MySQL 联合索引测试3

    接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430387.html 有时候会出现某字段建立一个索引,但是查看执行计划的时候发现还是全扫了表? 可以强制走下索引看看 ...

  3. SQL Server中的联合主键、聚集索引、非聚集索引、mysql 联合索引

    我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...

  4. 三道MySQL联合索引面试题,淘汰80%的面试者,你能答对几道

    众所周知MySQL联合索引遵循最左前缀匹配原则,在少数情况下也会不遵循(有兴趣,可以翻一下上篇文章). 创建联合索引的时候,建议优先把区分度高的字段放在第一列. 至于怎么统计区分度,可以按照下面这种方 ...

  5. MySQL 联合索引详解

    MySQL 联合索引详解   联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c ...

  6. mysql 联合索引(转)

    http://blog.csdn.net/lmh12506/article/details/8879916 mysql 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中 ...

  7. MySQL联合索引VS单列索引

    MySQL联合索引VS单列索引 以一个一千万数据量的表格为例 1. 建表建索引 USE foo; DROP TABLE IF EXISTS tmp; CREATE TABLE tmp ( id INT ...

  8. MySQL联合索引最左匹配范例

    MySQL联合索引最左匹配范例 参考文章:http://blog.jobbole.com/24006/ 创建示例表. 示例表来自MySQL官方文档: https://dev.mysql.com/doc ...

  9. [转]mysql联合索引

    mysql联合索引   命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果where条件中是OR关系,加索引不起作用4.符合最左原则 https:/ ...

随机推荐

  1. Backup your Android without root or custom recovery -- adb backup

    ecently discovered a neat new way to back up apps on my Android without having to use Titanium Backu ...

  2. codeforces 558B Amr and The Large Array-yy

    题意:有一个数组.如今要削减它的尺寸.数组中同样元素的个数的最大值为数组的魅力值,要求削减后魅力值不能降低,同一时候要尽可能的把尺寸减到最小 分析:水题,主要是不要想复杂了.还有就是沉下心来做 代码: ...

  3. 使用jQuery异步传递Model到控制器方法,并异步返回错误信息

    需要通过jquery传递到控制器方法的Model为: public class Person { public string Name { get; set; } public int Age { g ...

  4. js Date 函数方法

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  5. DocumentManager在标签位置显示气泡框 z

    关于DevExpress DockManager下的DocumentManager头部标签如何显示气泡框,类似Visual studio那样显示文件的路径,如下图所示,------- 方式很简单,从工 ...

  6. Android Gradle Plugin指南(五)——Build Variants(构建变种版本号)

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants 6. Build Vari ...

  7. AFNetworking 2.0使用(持续更新)

    本人视频教程系列 导入AFNetworking 2.0 文件夹,引入头文件AFNetworking.h --------------- *使用NSURLSessionDownloadTask来下载一张 ...

  8. Ubuntu 16.04 重置密码

    忘记了你的Ubuntu用户密码,登录不了系统:不要紧,在Ubuntu上重置密码是很简单的,即使你忘记了用户名. #1 进入Recovery Mode Recovery Mode即恢复模式:在Grub启 ...

  9. 四边形优化dp

    理解: http://blog.renren.com/share/263498909/1064362501 http://www.cnblogs.com/ronaflx/archive/2011/03 ...

  10. TPC-E在populate测试Database时需要注意的一些事项

    第一,  安装时不要使用named instance, 默认的instance就好. 否则会报连不上Database. 第二, TPC-E工具文件夹的完整路径中不可以有空格, 否则会在generate ...