MySQL 联合索引测试

搭建测试环境

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)走索引,且索引长度最长

时间: 2024-10-01 19:19:10

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

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);

MySQL 联合索引测试3

接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430387.html 有时候会出现某字段建立一个索引,但是查看执行计划的时候发现还是全扫了表? 可以强制走下索引看看扫描了多少数据量: EXPLAIN SELECT * FROM tab_index force index(idx2) WHERE age = 32; 然后比较 此次需要扫描的记录数与之前全表扫描的记录数,看百分比是多少??? 如果走索引扫描的数据很多的话,可能优化器会认为效率没有全扫快.索引就没有

mysql 联合索引详解

mysql 联合索引详解   联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c 3种组合进行查找,但不支持 b,c进行查找 .当最左侧字段是常量引用时,索引就十分有效. 两个或更多个列上的索引被称作复合索引.利用索引中的附加列,您可以缩小搜索的范围,但使用一个具有两列的索引 不同于使用两个单独的索引.复合索引的结构与电话簿类似,人名由

MySQL联合索引VS单列索引

MySQL联合索引VS单列索引 以一个一千万数据量的表格为例 1. 建表建索引 USE foo; DROP TABLE IF EXISTS tmp; CREATE TABLE tmp ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, school_id INT UNSIGNED NOT NULL, student_id INT UNSIGNED NOT NULL, INDEX school_id(school_id), INDEX student_id(

对mysql联合索引中的字段进行合理排序

在MySQL的where条件中,有时会用到很多的条件,通常为了加快速度会把这些字段放到联合索引中,可以更快的提高搜索速度: 但是对联合索引中字段顺序的合理排序,便更能提高速度 例子:select * from table where (groupid=1000) and (userid=500) and (time=140012345) 建立的索引也通常很随性的就按照where条件中字段的顺序建立 ALTER  TABLE  table  ADD  INDEX g_u_time_index(gr

mysql联合索引详解

所有的MySQL列类型能被索引.在相关的列上的使用索引是改进SELECT操作性能的最好方法. 一.前缀索引 对于CHAR和VARCHAR列,你可以索引列的前缀.这更快并且比索引整个列需要较少的磁盘空间.在CREATE TABLE语句中索引列前缀的语法看起来像这样: KEY index_name (col_name(length))下面的例子为name列的头10个字符创建一个索引: mysql> CREATE TABLE test (name CHAR(200) NOT NULL,KEY inde

mysql 联合索引和唯一索引

一般来说.如果有where a=? and b=? and c=? 的语句. 如果表也有DML, 我一般只在a 上建索引.  这也是代价平衡的结果. 一方面 只在a 上建索引那么是 index range scan, 不像联合索引那样可以index  unique scan , 我觉得速度差的不多(数据量不大的情况). 另一方面,DML也会更新index,更新三个column肯定比更新一个column慢些. 所以我一般采取折中.只建单列或2列索引. 联合索引对于查询 where a=? and

mysql:联合索引及优化

命名规则:表名_字段名1.需要加索引的字段,要在where条件中2.数据量少的字段不需要加索引3.如果where条件中是OR关系,加索引不起作用4.符合最左原则 https://segmentfault.com/q/1010000003984016/a-1020000003984281 联合索引又叫复合索引.对于复合索引:Mysql从左到右的使用索引中的字段,一个查询可以只使用索引中的一部份,但只能是最左侧部分.例如索引是key index (a,b,c). 可以支持a | a,b| a,b,c

mysql联合索引 sql索引使用

注意:Index(Name,Age)表示在Name,Age两列上建立联合索引 由于索引对数据库的查询性能有着至关重要的影响,下面是我的一些总结和体会: 一个查询一次只能使用一个索引:select name from user where name='plantegg' and age>35 , 如果Index(name); Index(age)的话,MySQL查询优化器会自动选择一个索引来使用: MySQL选择哪个索引,可以这样来看:mysql> show index from photo;