MySQL 聯合索引測試

來源:互聯網
上載者:User

標籤:

搭建測試環境

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 聯合索引測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.