To the next article:
Http://www.cnblogs.com/xiaoit/p/4430300.html
1: First delete the previous established index and re-establish one.
Mysql> DROP INDEX idx1 on Tab_index; Query OK, rows affected (0.01 sec) records:55 duplicates:0 warnings:0
Mysql> ALTER TABLE tab_index, ADD index idx2 (age,dte); Query OK, rows affected (0.01 sec) records:55 duplicates:0 warnings:0
The test results according to the previous article should be:
(age) Walk the index with the shortest index length
(age,id) walk index with shortest index length
(age,dte) Go index, and the index length is the longest
(id,age,dte) Go index, and the index length is the longest
(id,dte,age) Go index, and the index length is the longest
(dte,id,age) Go index, and the index length is the longest
(dte,age,id) Go index, and the index length is the longest
(age,dte,id) Go index, and the index length is the longest
(age,id,dte) Go index, and the index length is the longest
Re-test look under:
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 | 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 ' an D ' 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 | 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 0 0: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 | 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-0 5-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 | 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-0 5-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 | 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 ' an D ' 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 | 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 ' an D ' 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 | 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 0 0: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 | 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 | NULL | 1 | Using where |+----+-------------+-----------+-------+---------------+------+---------+------+------+------------ -+1 row in Set (0.00 sec)
The conclusion is correct!
Because questioning today's interviewer's conclusion, go home and do a test, the decisive interviewer is not careful enough. Tomorrow if you call to confirm the number of the call to think again ...
MySQL Federated Index Test 2