MySQL indexes causes of failure

Posted on 2018-11-30 19:35:07 Original 92.4 451 Favorite Reads
expanded
view index structure
MySQL> Show index from Staffs;
+ -------- + ----------- - + ------------------ + -------------- + ------------- + ------------- + ----------- + ---------- + -------- + ---- - + ------------ + --------- + --------------- +
| the Table | NON_UNIQUE | the Key_name | Seq_in_index | Column_name | Collation | the Cardinality | Sub_part | Packed | Null | index_type | the Comment | Index_comment |
+ -------- + -------- + ------------ ---------- + -------------- + ------------- + ---------- - + ------------- + ---------- + -------- + ------ + ------- + --------- + --------------- + -----
| Staffs | 0 | a PRIMARY |. 1 | ID | A |. 3 | NULL | NULL | | BTREE | | |
| staffs |          1 | idx_name_age_pos |            1 | NAME        | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| staffs |          1 | idx_name_age_pos |            2 | age         | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| staffs |          1 | idx_name_age_pos |            3 | pos         | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
+--------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1
2
3
4
5
6
7
8
9
I love the whole value matches
the query field or query fields and the index fields established correspondence

mysql> explain select * from staffs where NAME = 'July' and age = 23 and pos = 'dev';
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref               | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 140     | const,const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
1 row in set (0.00 sec)

# Type for the ref, and used the index
1
2
3
4
5
6
7
8
9
best left-prefix rule
means: If more than the value of the index, the most left-prefix to comply with the law. It refers to a query from the leftmost column index of the beginning and not skip column in the index.
Explanation: The query field or field conditions may not all be consistent with the index field, but must be consistent beginning and the middle can not be cut off.
Take the lead in Big Brother can not die
among the brothers can not be broken
# Only the first two
MySQL> EXPLAIN the SELECT * Staffs from the WHERE NAME = 'July' and Age = 23;
+ ---- + ------------- + -------- + ------ + ------ + -------------- ---- + --------- + ------ + -------------- + ------------- + ---------
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 78      | const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
1 row in set (0.00 sec)

# 没有‘大哥’
mysql> explain select * from staffs where age = 23 and pos = 'dev';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

# ‘兄弟’间断
mysql> explain select * from staffs where NAME = 'July' and pos = 'dev';
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref   | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 74      | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
analyzed: no 'big brother' the same as the first layer without stairs. 'Brother' interrupted the stairs like a middle layer of less.
Do anything (calculation function (automatic or manual) type conversion) in the index column, leads to failure while the index steering full table scan
# does not do operations on the index column
mysql> explain select * from staffs where name = 'July ';
+ ---- + ------------- ------------ + -------- + ------ + ------ + ------ + --------- + ------- + ------ + ----------------------- +
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 74      | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
1 row in set (0.01 sec)

# 在索引列上做了操作
mysql> explain select * from staffs where left(name,4)='July';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12 is
13 is
14
15
16
. 17
storage engine can not use the index range condition rightmost column
analysis following code can know when the range condition index occurs, which index columns behind will not be used (occurrence of this field range condition itself may also be use)
MySQL> Staffs from EXPLAIN SELECT * WHERE name = 'July' and Age = 22 is and POS = 'dev';
+ ---- + ---- + ------------- ---- + ------ + ------ + ------ + ------------------- + --------- + ------ + ------------- + ----------
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+ ---- + ------------ - + -------- + ------ + ------ + ------------- ----- + --------- + ------- + ------ + ------- ---------------- +
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 140     | const,const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------------+------+-----------------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name='July' and age>15 and pos='dev';
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys    | key              | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | staffs | range | idx_name_age_pos | idx_name_age_pos | 78      | NULL |    1 | Using index condition |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name='July' and age=15;
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref         | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 78      | const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
1 row in set (0.00 sec)
1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
to make use of a covering index (only access the index query (query column and the index column consistency)), reduction SELECT *
MySQL> EXPLAIN SELECT * WHERE name = Staffs from 'July' and Age = 15;
+ ---- + ------ + + -------- + ------------- - ------------------ + ----------------- + --------- + --- + ------ + ----------------------- + ----------
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 78      | const,const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+-----------------------+
1 row in set (0.00 sec)

mysql> explain select name,age from staffs where name='July' and age=15;
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+--------------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref         | rows | Extra                    |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+--------------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 78      | const,const |    1 | Using where; Using index |
+----+-------------+--------+------+------------------+------------------+---------+-------------+------+--------------------------+
1 row in set (0.00 sec)
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
not used MySQL using the not equal (<> or! =) Index, cause a full table scan
mysql> explain select * from staffs where name! = 'July ';
+ ---- + ------------- ------------ + -------- + ------ + ------ + ------ + ------ + ------ + --------- + ------------ - +
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+ ---- + ------- + ------------- - + ------ + ------ + ------ + --------- + ----- - + ------ + ------------- +
| 1 | SIMPLE | Staffs | ALL | idx_name_age_pos | NULL | NULL | NULL | 3 | the Using the WHERE |
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name='July';
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
| id | select_type | table  | type | possible_keys    | key              | key_len | ref   | rows | Extra                 |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
|  1 | SIMPLE      | staffs | ref  | idx_name_age_pos | idx_name_age_pos | 74      | const |    1 | Using index condition |
+----+-------------+--------+------+------------------+------------------+---------+-------+------+-----------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
is null和is not null也无法使用索引
mysql> explain select * from staffs where name is not null;
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys    | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | idx_name_age_pos | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name is null;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra            |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
|  1 | SIMPLE      | NULL  | NULL | NULL          | NULL | NULL    | NULL | NULL | Impossible WHERE |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12 is
13 is
14
15
like in the beginning with wildcards (% abc), MySQL index can not be used, resulting in a full table scan
MySQL> Staffs from EXPLAIN SELECT * WHERE name like '% July%';
+ ----- + ---- -------- + -------- + ----- + ------ + ------ + - + ------ + ------ + ------- ------------- +
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+ ------------- + ---- + -------- + ------ + ------- -------- + ------ + ------ + ------ + --------- + ---------- + ---
| 1 | SIMPLE | Staffs | ALL | NULL | NULL | NULL | NULL | 3 | the Using the WHERE |
+ ---- + ---- + ------------- ---- + ------ + ------ + ----- + ----- + --------- - + ------ + ------------- +
1 Row in the SET (0.00 sec)

mysql> explain select * from staffs where name like'%July';
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | NULL          | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select * from staffs where name like'July%';
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
| id | select_type | table  | type  | possible_keys    | key              | key_len | ref  | rows | Extra                 |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
|  1 | SIMPLE      | staffs | range | idx_name_age_pos | idx_name_age_pos | 74      | NULL |    1 | Using index condition |
+----+-------------+--------+-------+------------------+------------------+---------+------+------+-----------------------+
1 row in set (0.00 sec)
1
2
3
4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
resolved% can not be left
to use a covering index

String without single quotation marks Failure Index
MySQL> Staffs from EXPLAIN SELECT * WHERE name = '2000';
+ ---- + + ------- ------------- - + ------ + ------ + ------ + --- + ------- + ------ + ------ + -----------------------
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+ ---- + ---- + -------- + ------------- - + ------------------ + ------------------ + --------- ----------------------- + ------- + ------ + +
| 1 | SIMPLE | Staffs | ref | idx_name_age_pos | idx_name_age_pos | 74 | const | 1 | the Using index for condition Condition |
+ ------------- + ---- + -------- + ------ + - ------------------ + ----------------- + --------- + --- + ----------------------- + ------ + ----
1 Row in the SET (0.00 sec)

mysql> explain select * from staffs where name = 2000;
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
| id | select_type | table  | type | possible_keys    | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | staffs | ALL  | idx_name_age_pos | NULL | NULL    | NULL |    3 | Using where |
+----+-------------+--------+------+------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13 is
14
15
Note that
in the sql varchar single quotation marks must
be avoided explicit or implicit type conversion occurs
or can cause failure of the index
MySQL> Staffs from EXPLAIN SELECT * WHERE name = 'July' 20 is or = Age;
+ - --- + ------ + -------- + ------------- + ---------------- - + ------ + ------ + ------ + --------- + ------------- +
| the above mentioned id | SELECT_TYPE | the Table | of the type | possible_keys | Key | key_len | ref | rows | Extra |
+ ---- + -------- + ------------- + - ---- + ------ + ------ + ------ + --------- + - + ------------- + ----
| 1 | SIMPLE | Staffs | ALL | idx_name_age_pos | NULL | NULL | NULL | 3 | the Using the WHERE |
+ ---- + --- ---------- + -------- + ------ + ------ + ---- - + --------- + ------ + ------ + ------------- +
1 Row in the SET (0.00 sec)
1
2
3
4
5
6
7
[optimization summary formulas]
the full value of my favorite matches, the most left-prefix to follow;
take the lead in Big Brother can not die, the middle brother can not be broken;
less computationally index column, after the failure of the whole range;
the LIKE percentage wrote most Right, do not write cover elements lead star;
unequal null value there or, the index failed to use less;

Thumbs
----------------
Disclaimer: This article is CSDN blogger original article "92.4", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/zyp1376308302/article/details/84639977

Published 51 original articles · won praise 80 · views 930 000 +

Guess you like

Origin blog.csdn.net/xiyang_1990/article/details/103812841