接上一篇文章:

http://www.cnblogs.com/xiaoit/p/4430300.html

1:首先删掉上一篇建立的索引,重新建立一个。

mysql> DROP INDEX idx1 ON tab_index;
Query OK, 55 rows affected (0.01 sec)
Records: 55 Duplicates: 0 Warnings: 0

  

mysql> ALTER TABLE tab_index
-> ADD INDEX idx2(age,dte);
Query OK, 55 rows affected (0.01 sec)
Records: 55 Duplicates: 0 Warnings: 0

  

按照上一篇的测试结论应该是:

(age) 走索引,且索引长度最短
(age,id)走索引,且索引长度最短

(age,dte)走索引,且索引长度最长
(id,age,dte)走索引,且索引长度最长
(id,dte,age)走索引,且索引长度最长
(dte,id,age)走索引,且索引长度最长
(dte,age,id)走索引,且索引长度最长
(age,dte,id)走索引,且索引长度最长
(age,id,dte)走索引,且索引长度最长

重新测试看下:

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 | ref | idx2 | idx2 | 5 | const | 1 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------+------+-------------+
1 row in set (0.01 sec)

  

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 | idx2 | idx2 | 5 | 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' ;
+----+-------------+-----------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+-------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | tab_index | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 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 | range | idx2 | idx2 | 14 | NULL | 1 | Using where |
+----+-------------+-----------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

  

结论正确!

MySQL 联合索引测试2的更多相关文章

  1. MySQL 联合索引测试

    搭建测试环境 1:创建表 CREATE TABLE tab_index (id int(5), age int(3), dte datetime); 2:插入测试数据 INSERT INTO tab_ ...

  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. Spring Boot项目的Logback配置文件使用yaml格式

    1.普通的Spring项目使用logback默认用properties文件做为配置变量. 2.如果非要用yaml文件,那么可以转成Spring Boot项目,天生无缝结合 3.没办法,如果项目配置文件 ...

  2. 移动平台的WebApp之Meta标签

    对于桌面平台web布局中大家对meta标签再熟悉不过了,它永远位于 head 元素内部,对做SEO的朋友一定对meta有种特殊的感情吧,今天我们就来说说移动平台的meta标签,在移动平台meta标签究 ...

  3. OBD-II Protocol -- SAE J1850 VPW PWM

    http://www.auto-diagnostics.info/j1850 j1850 The SAE J1850 bus bus is used for diagnostics and data ...

  4. c# 四舍五入、上取整、下取整

    在处理一些数据时,我们希望能用“四舍五入”法实现,但是C#采用的是“四舍六入五成双”的方法,如下面的例子,就是用“四舍六入五成双”得到的结果: double d1 = Math.Round(1.25, ...

  5. winform打开进程与关闭进程

    #region 判断某进程名是否运行 /// <summary> /// 关闭指定名称的进程 /// </summary> /// <param name="p ...

  6. Java实现归并排序(转)

    Java实现归并排序  本文转自https://www.cnblogs.com/of-fanruice/p/7678801.html 归并排序 (merge sort) 是一类与插入排序.交换排序.选 ...

  7. 如何开启解决android studio的模拟器的问题

    来自:http://jingyan.baidu.com/article/03b2f78c0a19e75ea237ae24.html 有的时候因为电脑系统或者是安装的一些问题我们可能需要对症下药的解决模 ...

  8. Windows Phone 7 开发环境的搭建

    本节开始进行Windows Phone 开发环境的搭建,包括所需要的操作系统及硬件的介绍,开发工具的下载与安装,以及开发工具的介绍等.由于Jake Lin老师的视频中讲解的是早期的Windows Ph ...

  9. [翻译] AsyncImageView 异步下载图片

    AsyncImageView  https://github.com/nicklockwood/AsyncImageView AsyncImageView is a simple extension ...

  10. Java IO的简单示例

    File类示例 public class FileUsageTest { private static void usage() { System.err.println("Usage: F ...