搭建测试环境

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. 使用NFS启动Tiny4412开发板根文件系统

      1.Ubuntu14.04上搭建NFS服务 1.1.安装NFS服务 $ sudo apt-get install nfs-kernel-server    //安装NFS服务 1.2 创建Tiny ...

  2. git pull的时候出错: Git Couldn't reserve space for cygwin's heap

    具体: 1. 运行CMD,以管理员身份打开 2. 运行:rebase.exe -b 0x50000000 msys-1.0.dll 再次git pull的时候,不再报错 转自:http://doc.o ...

  3. PowerDesigner设置唯一约束/唯一索引/唯一键

    注意:还需要设置unique约束,也是在这个界面. 参考: https://blog.csdn.net/cnham/article/details/6676650 https://blog.csdn. ...

  4. PowerDesigner导出图片

    但是通过上面导出的图片会比较模糊不清晰,但是可以通过这样解决: 1.放大设计,然后全选复制全部 2.打开画图工具 3.粘贴到画图工具

  5. NHibernate的调试技巧和Log4Net配置

    1.查看nhibernate写在控制台里的sql语句 在配置文件中有这么个选项,假如把它设置为true,nhibernate会把执行的sql显示在控制台上. <property name=&qu ...

  6. Arcgis Runtime for andriod 100 Simple marker symbol

    GraphicsOverlay graphicsOverlay = new GraphicsOverlay(); 58 mMapView.getGraphicsOverlays().add(graph ...

  7. .net上开发winform

    c++用WinForm做界面的实现 因为笔者是以前是做C#的,对Winform情有独钟,最近想转C++,想把以前的一些Delphi转成c++,MFC我不熟而且用起来相当烦效果又丑,GTK图形库用起来太 ...

  8. tomcat怎么运行servlet程序

    新建一个web project 取名 myproject 在myproject 新建一个继承了HttpServlet 的类 MyServlet 重写HttpServlet 的 dopost doget ...

  9. 数学图形之Boy surface

    这是一个姓Boy的人发现的,所以取名为Boy surface.该图形与罗马图形有点相似,都是三分的图形.它甚至可以说是由罗马曲面变化而成的. 本文将展示几种Boy曲面的生成算法和切图,使用自己定义语法 ...

  10. Android数据适配-ExpandableListView

    Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableList ...