搭建测试环境

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. 关于List Map Set的线程安全的问题

    常见的ArrayList  LinkedList  HashMap TreeMap LinkedHashMap HashSet TreeSet LinkedHashSet 都是线程不安全的.如果要使用 ...

  2. ubuntu 13.04 编译 安装 升级 gcc 4.9.0 address sanitizer

    @ 前记: 最近查一个线上项目的crash,review代码无果,crash几率低,不可在本地环境重现.之后在线上好几个服务器跑valgrind就不crash了.个人猜测可能是跑valgrind后性能 ...

  3. C#软件license管理(简单软件注册机制)

    最近做了一个绿色免安装软件,领导临时要求加个注册机制,不能让现场工程师随意复制.事出突然,只能在现场开发(离开现场软件就不受我们控了).花了不到两个小时实现了简单的注册机制,稍作整理.        ...

  4. [Asp.net web api]缓存

    摘要 为了提高接口的性能,我们常做的优化就包括缓存,对经常访问但变化不大的数据进行缓存.或者使用http的缓存,减少请求的次数. web api缓存 在提供的api,我们也可以实现缓存,来减少访问的次 ...

  5. iOS开发者帐号申请指南

    iOS开发者的申请流程如果你是一个开发团队,在你打算掏腰包购买iOS开发者授权之前,最好先问一下你的同事,是否已经有人获得了开发许可,因为一个开发许可一年内最多可以授权给111个设备来开发测试.如果你 ...

  6. [转载] 关于matlab GUI的一点心得

    转载自 落落轻尘 [Fig文件方式,即使用菜单File->New->GUI来设计界面] 首先值得注意的是,在低版本matlab上制作的含GUI的m文件一般不能在高版本的matlab上面运行 ...

  7. Entityframework:“System.Data.Entity.Internal.AppConfig”的类型初始值设定项引发异常。

    <configSections> <!-- For more information on Entity Framework configuration, visit http:// ...

  8. [翻译] Fast Image Cache

    https://github.com/path/FastImageCache Fast Image Cache is an efficient, persistent, and—above all—f ...

  9. libjson 编译和使用 - 2. 配置使用lib文件

    以下转自:http://blog.csdn.net/laogong5i0/article/details/8223448 1. 在之前的libjson所在的解决方案里新建一个控制台应用程序,叫Test ...

  10. 金蝶KIS下载地址

    升级方法: 您好,建议您先升级到标准版7.5,再升级到标准版8.1,直接用7.5的软件打开金蝶2000的账套,会提示升级,再用8.1的软件打开7.5的账套,升级前,需先备份账套. 金蝶KIS标准版和业 ...