標籤:
搭建測試環境
1:建立表
CREATE TABLE tab_index(id int(5),age int(3),dte datetime);
2:插入測試資料
INSERT INTO tab_indexVALUES(1,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(2,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(3,‘2012-05-13‘,31);INSERT INTO tab_indexVALUES(4,‘2012-05-13‘,32);INSERT INTO tab_indexVALUES(5,‘2012-05-13‘,33);INSERT INTO tab_indexVALUES(6,‘2012-06-13‘,34);INSERT INTO tab_indexVALUES(7,‘2012-07-13‘,35);INSERT INTO tab_indexVALUES(8,‘2012-08-13‘,36);INSERT INTO tab_indexVALUES(9,‘2012-09-13‘,37);INSERT INTO tab_indexVALUES(10,‘2012-05-17‘,38);INSERT INTO tab_indexVALUES(11,‘2012-05-19‘,39);INSERT INTO tab_indexVALUES(1,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(2,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(3,‘2012-05-13‘,31);INSERT INTO tab_indexVALUES(4,‘2012-05-13‘,32);INSERT INTO tab_indexVALUES(5,‘2012-05-13‘,33);INSERT INTO tab_indexVALUES(6,‘2012-06-13‘,34);INSERT INTO tab_indexVALUES(7,‘2012-07-13‘,35);INSERT INTO tab_indexVALUES(8,‘2012-08-13‘,36);INSERT INTO tab_indexVALUES(9,‘2012-09-13‘,37);INSERT INTO tab_indexVALUES(10,‘2012-05-17‘,38);INSERT INTO tab_indexVALUES(11,‘2012-05-19‘,39);INSERT INTO tab_indexVALUES(1,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(2,‘2015-05-13‘,23);INSERT INTO tab_indexVALUES(3,‘2012-05-13‘,31);INSERT INTO tab_indexVALUES(4,‘2012-05-13‘,32);INSERT INTO tab_indexVALUES(5,‘2012-05-13‘,33);INSERT INTO tab_indexVALUES(6,‘2012-06-13‘,34);INSERT INTO tab_indexVALUES(7,‘2013-07-13‘,35);INSERT INTO tab_indexVALUES(8,‘2012-08-13‘,36);INSERT INTO tab_indexVALUES(9,‘2012-09-13‘,37);INSERT INTO tab_indexVALUES(10,‘2012-05-17‘,38);INSERT INTO tab_indexVALUES(11,‘2011-05-19‘,39);INSERT INTO tab_indexVALUES(1,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(2,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(3,‘2010-05-13‘,31);INSERT INTO tab_indexVALUES(4,‘2012-05-13‘,32);INSERT INTO tab_indexVALUES(5,‘2010-05-13‘,33);INSERT INTO tab_indexVALUES(6,‘2010-06-13‘,34);INSERT INTO tab_indexVALUES(7,‘2012-07-13‘,35);INSERT INTO tab_indexVALUES(8,‘2012-08-13‘,36);INSERT INTO tab_indexVALUES(9,‘2011-09-13‘,37);INSERT INTO tab_indexVALUES(10,‘2012-05-17‘,38);INSERT INTO tab_indexVALUES(11,‘2012-05-19‘,39);INSERT INTO tab_indexVALUES(1,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(2,‘2012-05-13‘,23);INSERT INTO tab_indexVALUES(3,‘2012-05-13‘,31);INSERT INTO tab_indexVALUES(4,‘2012-05-13‘,32);INSERT INTO tab_indexVALUES(5,‘2012-05-13‘,33);INSERT INTO tab_indexVALUES(6,‘2012-06-13‘,34);INSERT INTO tab_indexVALUES(7,‘2014-07-13‘,35);INSERT INTO tab_indexVALUES(8,‘2012-08-13‘,36);INSERT INTO tab_indexVALUES(9,‘2011-09-13‘,37);INSERT INTO tab_indexVALUES(10,‘2012-05-17‘,38);INSERT INTO tab_indexVALUES(11,‘2012-05-19‘,39);
3:建立id和age上的聯合索引
CREATE INDEX idx1ON 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 聯合索引測試