MySQL 联合索引测试2
接上一篇文章:
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的更多相关文章
- MySQL 联合索引测试
搭建测试环境 1:创建表 CREATE TABLE tab_index (id int(5), age int(3), dte datetime); 2:插入测试数据 INSERT INTO tab_ ...
- MySQL 联合索引测试3
接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430387.html 有时候会出现某字段建立一个索引,但是查看执行计划的时候发现还是全扫了表? 可以强制走下索引看看 ...
- SQL Server中的联合主键、聚集索引、非聚集索引、mysql 联合索引
我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...
- 三道MySQL联合索引面试题,淘汰80%的面试者,你能答对几道
众所周知MySQL联合索引遵循最左前缀匹配原则,在少数情况下也会不遵循(有兴趣,可以翻一下上篇文章). 创建联合索引的时候,建议优先把区分度高的字段放在第一列. 至于怎么统计区分度,可以按照下面这种方 ...
- MySQL 联合索引详解
MySQL 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c ...
- mysql 联合索引(转)
http://blog.csdn.net/lmh12506/article/details/8879916 mysql 联合索引详解 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中 ...
- MySQL联合索引VS单列索引
MySQL联合索引VS单列索引 以一个一千万数据量的表格为例 1. 建表建索引 USE foo; DROP TABLE IF EXISTS tmp; CREATE TABLE tmp ( id INT ...
- MySQL联合索引最左匹配范例
MySQL联合索引最左匹配范例 参考文章:http://blog.jobbole.com/24006/ 创建示例表. 示例表来自MySQL官方文档: https://dev.mysql.com/doc ...
- [转]mysql联合索引
mysql联合索引 命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果where条件中是OR关系,加索引不起作用4.符合最左原则 https:/ ...
随机推荐
- VS2010发布网站
- 动态创建html元素的几种方法
可以通过以下几种方式动态创建html元素: 1.使用jQuery创建元素的语法 2.把动态内容存放到数组中,再遍历数组动态创建html元素 3.使用模版 □ 使用jQuery动态创建元素追加到jQ ...
- joel 相关
1. stack overflow , http://blog.jobbole.com/102810/, stack overflow 提问也是一门学问. 2. tello 类似于 leangoo,一 ...
- dwr3实现消息精确推送详细步骤
最近项目中需要用到推送消息,找了很久终于找到一篇不错的文章,方便以后查看就转载了,也分享给大家,希望能帮到有需要的人. 第一.在项目中引入dwr.jar,然后在web.xml中进行配置,配置如下: & ...
- Python已成为网络攻击的首选编程语言
Python已成为网络攻击的首选编程语言 最新的调查数据表明,Python已经变成了世界上最热门的编程语言了,而Python的热门风也刮到了信息安全领域中.Python,摇身一变,也变成了黑客开发网络 ...
- HDU1874畅通project续 dijkstra&&floyd
畅通project续 http://acm.hdu.edu.cn/showproblem.php?pid=1874 Time Limit: 3000/1000 MS (Java/Others) ...
- C语言:将结构体数组的成绩按照从小到大进行排序。
#include<stdio.h> typedef struct student { char *name; int sno; int age; float score; }Student ...
- Informatica 常用组件Source Qualifier之七 使用排序端口
使用已排序端口时,PowerCenter 将添加端口至默认查询中的 ORDER BY 子句.PowerCenter Server 将添加配置的端口号,从源限定符转换的顶部开始.在映射中包括以下任何转换 ...
- cesiumjs学习笔记之三——cesium-navigation插件 【转】
http://blog.csdn.net/Prepared/article/details/68940997?locationNum=10&fps=1 插件源码地址:https://githu ...
- EventBus 事件总线 原理 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...