搭建测试环境

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. 在一台服务器上搭建多个项目的SVN

    需求:一台机子,多个项目,项目之间用户独立不可以相互访问文件 思路:在机子上设置多个代码仓库,用不同的端口号加一区分 实现: 首先安装SVN,我这里使用的是TortoiseSVN 首先创建多个SVN代 ...

  2. 11gR2 RAC重新启动后仅仅能起单节点

    11gR2 RAC重新启动后仅仅能起单节点 问题背景: 将11gR2 RAC正常部署完毕之后运行两节点重新启动操作发现当中有一个节点的集群资源无法启动,遂再次重新启动该无法启动集群资源的节点,还是不可 ...

  3. 提交改动到 github 远程服务器,怎么跳过要求输入密码的步骤

    新机器上将工程改动提交到 github 服务器时,发现每次都要输入密码,这个有点儿小烦人,怎么解决这个问题呢? 首先,切换到工程根目录的 .git 隐藏目录,用 TextEdit 打开 config ...

  4. [Node.js]Path模块

    摘要 path模块提供了一些处理文件路径问题的工具. path模块 引入模块 var path=require("path"); 方法 1 path.normalize(p)规范化 ...

  5. Redis的主从复制设置与安装

    1 验证redis的主从复制,将过程抓图 实验步骤 A.  安装Redis B.  启动Master Redis C.  创建redis-slave.conf配置文件 改动Redisport和打开sl ...

  6. linux驱动移植的重要数据结构

    转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201304/14-2538.html 对于嵌入式 Linux 系统来说,有各种体系结构的处理器和 ...

  7. 使用Bootstrap 3开发响应式网站实践05,使用Tab、Modal、Form展示内容,使用Popover、Tooltip展示提示信息

    本篇体验用Tab插件显示内容.Html部分为: <div class="row" id="moreInfo"> <div class=&quo ...

  8. 扩展redisTemplate实现分布式锁

    原文:https://blog.csdn.net/qq1010267837/article/details/79697572 依赖jar包 compile group: 'redis.clients' ...

  9. python笔记25-mock-server之moco

    前言 mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口 此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自 ...

  10. [翻译] Haneke(处理图片缓存问题)

    Haneke https://github.com/hpique/Haneke A lightweight zero-config image cache for iOS. 轻量级0配置图片缓存. H ...