Performance optimization of MySQL Explain

       explain show how to use the index to handle mysql select statement and connection table. You can help choose a better index and write more optimized queries.

Use, plus explain before the select statement on it:

Such as:

  1. explain select surname,first_name form a,b where a.id=b.id 

EXPLAIN column explanation:

table: display data of this line is about which tables of

type: This is an important column that shows what type of connection used. From best to worst the connection type is const, eq_reg, ref, range, index and ALL

         type shows the type of access is a more important indicator, the resulting value is in turn from good to bad: system> const> eq_ref> ref > fulltext> ref_or_null> index_merge> unique_subquery> index_subquery> range> index> ALL
         In general, queries have to ensure that at least the range level best to achieve ref.

possible_keys: displays the index in this table may apply. If empty, the index is not possible. May be selected from a suitable statement WHERE clause for the associated domain

key: the index of actual use. If NULL, then do not use the index. In rare cases, MYSQL will choose less than the index optimization. In this case, you can be used in the SELECT statement USE INDEX (indexname) to force the use of an index or with IGNORE INDEX (indexname) to force the index ignored MYSQL

key_len: length of the index used. Without loss of accuracy, a length as short as possible

ref: display column index which is used, if possible, is a constant

rows: MYSQL must check that the number of rows to return the requested data

Extra: MYSQL additional information about how to resolve the query. Will be discussed in Table 4.3, but here you can see the bad example of Using temporary and are Using filesort, meaning MYSQL can not use the index, the result is retrieved will be very slow

extra column describes the return of meaning

 Distinct: Once MYSQL found from row to row with the United match, no longer searched

 Not exists: MYSQL optimized LEFT JOIN, once it finds a match LEFT JOIN standard line, no longer searched

 Range checked for each Record (index map: #): not found over the index, so for each combination of a row from the preceding table, the use of which index MYSQL Based check, and use it to return rows from the table. This is done using the index one of the slowest connection

 Using filesort: see this when you need to optimize the query. MYSQL need for additional steps to find out how to sort the rows returned. It is to sort all the lines according to the connection type and storing all the lines of the row pointer and its sort key matching condition

 Using index: column data is read and no real action from the only use of the information in the index table to return, which occurs when a request for all of the table columns are part of the same index

 Using temporary see this when the need to optimize the query. Here, MYSQL need to create a temporary table to store the results, which usually occurs in the ORDER BY be set for different columns, rather than on the GROUP BY

 Where used using a WHERE clause to limit the rows which match the next table or returned to the user. If you do not return all the rows in the table, and the connection type ALL or index, this will happen, there is a problem or query interpretation of different connection types (sorted in descending order of efficiency)

 system tables only one line: system table. This is a special case of type const connection

 const: maximum value of a table record can match the query (the index may be a primary key or unique index). Because only one line, this value is in fact a constant, because the first reading of this value MYSQL and then to treat it as a constant

 eq_ref: In connection, MYSQL Based upon the query, the joint for each record are read from the table a record from the preceding table, it uses the index query or all of the primary key using a unique key

 This only occurs when the connection type used in the query is not the only or the primary key of the key or part of these types (for example, using the left-most prefix): ref. For each row of the table before the joint, all records are read from the table. This type is heavily dependent on how many records match the index - the less the better

 range: This type of connection used in the index to return a range of rows, such as using> or <find what happens when

 index: The connection type for each record in the table in front of a full scan of the joint (more than ALL, because the index is generally less than the table data)

 ALL: This type of connection joint for a full scan preceding each record, which in general relatively poor and should be avoided

  

Look at an example:

  1. mysql> explain select * from t_order; 
  2. +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
  3. | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra | 
  4. +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
  5. |  1 | SIMPLE      | t_order | ALL  | NULL          | NULL | NULL    | NULL | 100453 |       | 
  6. +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
  7. 1 row in set (0.03 sec) 

After adding after extended:

  1. mysql> explain extended select * from t_order; 
  2. +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+ 
  3. | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra | 
  4. +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+ 
  5. |  1 | SIMPLE      | t_order | ALL  | NULL          | NULL | NULL    | NULL | 100453 |   100.00 |       | 
  6. +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+ 
  7. 1 row in set, 1 warning (0.00 sec) 

It is necessary to explain the meaning of the long table where each column:

id SELECT identifier. This is a SELECT query sequence number
select_type

SELECT type, which can be any of the following:

  • SIMPLE : Simple SELECT (not using UNION or subqueries)
  • PRIMARY : the outermost SELECT
  • UNION : The second or rear UNION SELECT statement
  • UNION DEPENDENT : UNION in the second SELECT statement or the back, depending on outside of inquiry
  • The RESULT UNION : UNION results
  • SUBQUERY : the first sub-query SELECT
  • SUBQUERY DEPENDENT : the first sub-query SELECT, depending on outside inquiry
  • DERIVED : export table SELECT (FROM clause subqueries)
table

Output line table referenced

type

Join type. Various types of coupling are given below, in accordance with the type ordered from best to worst type:

  • System : Only one line of table (= system table). This is a special case of the const join type.
  • const : Table at most one matching row, which will be read at the start of the query. Because only one row, column values in this row may be considered to optimize the remaining portion is constant. const table quickly because they are read only once!
  • eq_ref : For each combination of rows from the front of the table, reads a line from the table. This is probably the best connection type, except const type.
  • REF : For each combination of rows from the preceding table, all rows matching the index values read from this table.
  • ref_or_null : The connection type as ref, but added the line MySQL can search specifically contain NULL values.
  • index_merge : the coupling type indication index combined optimization method.
  • unique_subquery : The type of the alternative form of ref IN subqueries of the following: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery is an index lookup function, can completely replace the subquery, more efficient.
  • index_subquery : This type is similar to the coupling unique_subquery. IN subquery can be replaced, but only for the following sub-query in the form of a non-unique index: value IN (SELECT key_column FROM single_table WHERE some_expr)
  • Range : only retrieves a row given range, using an index to select the rows.
  • index : the coupling with ALL the same type, except that only the index tree is scanned. This is usually faster than ALL, because the index file is usually smaller than the data file.
  • ALL : For each combination of rows from the previous table, a full table scan.
possible_keys

It indicates which indexes MySQL could use to find the rows in the table

key Display key (index) MySQL actually decided to use. If you do not select an index, the key is NULL.
key_len Display MySQL decided to use key lengths. If the key is NULL, the length is NULL.
ref Shows which columns or constants are selected together with a key from a table using the row.
rows Display the number of rows MySQL believes it must examine the query execution. Of data between a plurality of lines can be estimated by multiplying the number of rows to be processed.
filtered It shows the percentage of the estimated value of the number of rows filtered through conditions.
Extra

This column contains the MySQL query to resolve details

  • DISTINCT : MySQL found after a matching line for the current row combination stop searching for more rows.
  • EXISTS Not : the MySQL LEFT JOIN able to query optimization, after a match was found LEFT JOIN standard line, no more rows in the check table of the foregoing combinations of rows.
  • Record the checked for each Range (Map index: #) : the MySQL found no good index may be used, but if the value found from the front of the column of the table is known, the index portion may be used.
  • Filesort the Using : MySQL requires additional one pass to find out how to retrieve the rows in sorted order.
  • Index the Using : from the use of only information in the index tree without having to search further reads the actual line to retrieve column information in the table.
  • The Temporary the Using : In order to solve the query, MySQL needs to create a temporary table to hold the result.
  • Using where:WHERE 子句用于限制哪一个行匹配下一个表或发送到客户。
  • Using sort_union(...), Using union(...), Using intersect(...):这些函数说明如何为index_merge联接类型合并索引扫描。
  • Using index for group-by:类似于访问表的Using index方式,Using index for group-by表示MySQL发现了一个索引,可以用来查 询GROUP BY或DISTINCT查询的所有列,而不要额外搜索硬盘访问实际的表。

 

一.select_type的说明

1.UNION:

当通过union来连接多个查询结果时,第二个之后的select其select_type为UNION。

  1. mysql> explain select * from t_order where order_id=100 union select * from t_order where order_id=200; 
  2. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+ 
  3. | id | select_type  | table      | type  | possible_keys | key     | key_len | ref   | rows | Extra | 
  4. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+ 
  5. |  1 | PRIMARY      | t_order    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       | 
  6. |  2 | UNION        | t_order    | const | PRIMARY       | PRIMARY | 4       | const |    1 |       | 
  7. | NULL | UNION RESULT | <union1,2> | ALL   | NULL          | NULL    | NULL    | NULL  | NULL |       | 
  8. +----+--------------+------------+-------+---------------+---------+---------+-------+------+-------+ 
  9. 3 rows in set (0.34 sec) 

2.DEPENDENT UNION与DEPENDENT SUBQUERY:

当union作为子查询时,其中第二个union的select_type就是DEPENDENT UNION。
第一个子查询的select_type则是DEPENDENT SUBQUERY。

  1. mysql> explain select * from t_order where order_id in (select order_id from t_order where order_id=100 union select order_id from t_order where order_id=200); 
  2. +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+ 
  3. | id | select_type        | table      | type  | possible_keys | key     | key_len | ref   | rows   | Extra       | 
  4. +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+ 
  5. |  1 | PRIMARY            | t_order    | ALL   | NULL          | NULL    | NULL    | NULL  | 100453 | Using where | 
  6. |  2 | DEPENDENT SUBQUERY | t_order    | const | PRIMARY       | PRIMARY | 4       | const |      1 | Using index | 
  7. |  3 | DEPENDENT UNION    | t_order    | const | PRIMARY       | PRIMARY | 4       | const |      1 | Using index | 
  8. | NULL | UNION RESULT       | <union2,3> | ALL   | NULL          | NULL    | NULL    | NULL  |   NULL |             | 
  9. +----+--------------------+------------+-------+---------------+---------+---------+-------+--------+-------------+ 
  10. 4 rows in set (0.03 sec) 

3.SUBQUERY:

子查询中的第一个select其select_type为SUBQUERY。

  1. mysql> explain select * from t_order where order_id=(select order_id from t_order where order_id=100); 
  2. +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+ 
  3. | id | select_type | table   | type  | possible_keys | key     | key_len | ref   | rows | Extra       | 
  4. +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+ 
  5. |  1 | PRIMARY     | t_order | const | PRIMARY       | PRIMARY | 4       | const |    1 |             | 
  6. |  2 | SUBQUERY    | t_order | const | PRIMARY       | PRIMARY | 4       |       |    1 | Using index | 
  7. +----+-------------+---------+-------+---------------+---------+---------+-------+------+-------------+ 
  8. 2 rows in set (0.03 sec) 

4.DERIVED:

当子查询是from子句时,其select_type为DERIVED。

  1. mysql> explain select * from (select order_id from t_order where order_id=100) a; 
  2. +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+ 
  3. | id | select_type | table      | type   | possible_keys | key     | key_len | ref  | rows | Extra       | 
  4. +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+ 
  5. |  1 | PRIMARY     | <derived2> | system | NULL          | NULL    | NULL    | NULL |    1 |             | 
  6. |  2 | DERIVED     | t_order    | const  | PRIMARY       | PRIMARY | 4       |      |    1 | Using index | 
  7. +----+-------------+------------+--------+---------------+---------+---------+------+------+-------------+ 
  8. 2 rows in set (0.03 sec) 

二.type的说明

1.system,const

见上面4.DERIVED的例子。其中第一行的type就是为system,第二行是const,这两种联接类型是最快的。

2.eq_ref

在t_order表中的order_id是主键,t_order_ext表中的order_id也是主键,该表可以认为是订单表的补充信息表,他们的关系是1对1,在下面的例子中可以看到b表的连接类型是eq_ref,这是极快的联接类型。

  1. mysql> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id; 
  2. +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+ 
  3. | id | select_type | table | type   | possible_keys | key     | key_len | ref             | rows | Extra       | 
  4. +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+ 
  5. |  1 | SIMPLE      | b     | ALL    | order_id      | NULL    | NULL    | NULL            |    1 |             | 
  6. |  1 | SIMPLE      | a     | eq_ref | PRIMARY       | PRIMARY | 4       | test.b.order_id |    1 | Using where | 
  7. +----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------------+ 
  8. 2 rows in set (0.00 sec) 

3.ref

下面的例子在上面的例子上略作了修改,加上了条件。此时b表的联接类型变成了ref。因为所有与a表中order_id=100的匹配记录都将会从b表获取。这是比较常见的联接类型。

  1. mysql> explain select * from t_order a,t_order_ext b where a.order_id=b.order_id and a.order_id=100; 
  2. +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+ 
  3. | id | select_type | table | type  | possible_keys | key      | key_len | ref   | rows | Extra | 
  4. +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+ 
  5. |  1 | SIMPLE      | a     | const | PRIMARY       | PRIMARY  | 4       | const |    1 |       | 
  6. |  1 | SIMPLE      | b     | ref   | order_id      | order_id | 4       | const |    1 |       | 
  7. +----+-------------+-------+-------+---------------+----------+---------+-------+------+-------+ 
  8. 2 rows in set (0.00 sec) 

4.ref_or_null

user_id字段是一个可以为空的字段,并对该字段创建了一个索引。在下面的查询中可以看到联接类型为ref_or_null,这是mysql为含有null的字段专门做的处理。在我们的表设计中应当尽量避免索引字段为NULL,因为这会额外的耗费mysql的处理时间来做优化。

  1. mysql> explain select * from t_order where user_id=100 or user_id is null; 
  2. +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+ 
  3. | id | select_type | table   | type        | possible_keys | key     | key_len | ref   | rows  | Extra       | 
  4. +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+ 
  5. |  1 | SIMPLE      | t_order | ref_or_null | user_id       | user_id | 5       | const | 50325 | Using where | 
  6. +----+-------------+---------+-------------+---------------+---------+---------+-------+-------+-------------+ 
  7. 1 row in set (0.00 sec) 

5.index_merge

经常出现在使用一张表中的多个索引时。mysql会将多个索引合并在一起,如下例:

  1. mysql> explain select * from t_order where order_id=100 or user_id=10; 
  2. +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+ 
  3. | id | select_type | table   | type        | possible_keys   | key             | key_len | ref  | rows | Extra                                     | 
  4. +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+ 
  5. |  1 | SIMPLE      | t_order | index_merge | PRIMARY,user_id | PRIMARY,user_id | 4,5     | NULL |    2 | Using union(PRIMARY,user_id); Using where | 
  6. +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+-------------------------------------------+ 
  7. 1 row in set (0.09 sec) 

6.unique_subquery

该联接类型用于替换value IN (SELECT primary_key FROM single_table WHERE some_expr)这样的子查询的ref。注意ref列,其中第二行显示的是func,表明unique_subquery是一个函数,而不是一个普通的ref。

  1. mysql> explain select * from t_order where order_id in (select order_id from t_order where user_id=10); 
  2. +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+ 
  3. | id | select_type        | table   | type            | possible_keys   | key     | key_len | ref  | rows   | Extra       | 
  4. +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+ 
  5. |  1 | PRIMARY            | t_order | ALL             | NULL            | NULL    | NULL    | NULL | 100649 | Using where | 
  6. |  2 | DEPENDENT SUBQUERY | t_order | unique_subquery | PRIMARY,user_id | PRIMARY | 4       | func |      1 | Using where | 
  7. +----+--------------------+---------+-----------------+-----------------+---------+---------+------+--------+-------------+ 
  8. 2 rows in set (0.00 sec) 

7.index_subquery

该联接类型与上面的太像了,唯一的差别就是子查询查的不是主键而是非唯一索引。

  1. mysql> explain select * from t_order where user_id in (select user_id from t_order where order_id>10); 
  2. +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+ 
  3. | id | select_type        | table   | type           | possible_keys   | key     | key_len | ref  | rows   | Extra                    | 
  4. +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+ 
  5. |  1 | PRIMARY            | t_order | ALL            | NULL            | NULL    | NULL    | NULL | 100649 | Using where              | 
  6. |  2 | DEPENDENT SUBQUERY | t_order | index_subquery | PRIMARY,user_id | user_id | 5       | func |  50324 | Using index; Using where | 
  7. +----+--------------------+---------+----------------+-----------------+---------+---------+------+--------+--------------------------+ 
  8. 2 rows in set (0.00 sec) 

8.range

按指定的范围进行检索,很常见。

  1. mysql> explain select * from t_order where user_id in (100,200,300); 
  2. +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+ 
  3. | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows | Extra       | 
  4. +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+ 
  5. |  1 | SIMPLE      | t_order | range | user_id       | user_id | 5       | NULL |    3 | Using where | 
  6. +----+-------------+---------+-------+---------------+---------+---------+------+------+-------------+ 
  7. 1 row in set (0.00 sec) 

9.index

在进行统计时非常常见,此联接类型实际上会扫描索引树,仅比ALL快些。

  1. mysql> explain select count(*) from t_order; 
  2. +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+ 
  3. | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows   | Extra       | 
  4. +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+ 
  5. |  1 | SIMPLE      | t_order | index | NULL          | user_id | 5       | NULL | 100649 | Using index | 
  6. +----+-------------+---------+-------+---------------+---------+---------+------+--------+-------------+ 
  7. 1 row in set (0.00 sec) 

10.ALL

完整的扫描全表,最慢的联接类型,尽可能的避免。

  1. mysql> explain select * from t_order; 
  2. +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
  3. | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra | 
  4. +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
  5. |  1 | SIMPLE      | t_order | ALL  | NULL          | NULL | NULL    | NULL | 100649 |       | 
  6. +----+-------------+---------+------+---------------+------+---------+------+--------+-------+ 
  7. 1 row in set (0.00 sec) 

三.extra的说明

1.Distinct

MySQL发现第1个匹配行后,停止为当前的行组合搜索更多的行。对于此项没有找到合适的例子,求指点。

2.Not exists

因为b表中的order_id是主键,不可能为NULL,所以mysql在用a表的order_id扫描t_order表,并查找b表的行时,如果在b表发现一个匹配的行就不再继续扫描b了,因为b表中的order_id字段不可能为NULL。这样避免了对b表的多次扫描。

  1. mysql> explain select count(1) from t_order a left join t_order_ext b on a.order_id=b.order_id where b.order_id is null;  
  2. +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+ 
  3. | id | select_type | table | type  | possible_keys | key          | key_len | ref             | rows   | Extra                                | 
  4. +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+ 
  5. |  1 | SIMPLE      | a     | index | NULL          | express_type | 1       | NULL            | 100395 | Using index                          | 
  6. |  1 | SIMPLE      | b     | ref   | order_id      | order_id     | 4       | test.a.order_id |      1 | Using where; Using index; Not exists | 
  7. +----+-------------+-------+-------+---------------+--------------+---------+-----------------+--------+--------------------------------------+ 
  8. 2 rows in set (0.01 sec) 

3.Range checked for each record

这种情况是mysql没有发现好的索引可用,速度比没有索引要快得多。

  1. mysql> explain select * from t_order t, t_order_ext s where s.order_id>=t.order_id and s.order_id<=t.order_id and t.express_type>5; 
  2. +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+ 
  3. | id | select_type | table | type  | possible_keys        | key          | key_len | ref  | rows | Extra                                          | 
  4. +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+ 
  5. |  1 | SIMPLE      | t     | range | PRIMARY,express_type | express_type | 1       | NULL |    1 | Using where                                    | 
  6. |  1 | SIMPLE      | s     | ALL   | order_id             | NULL         | NULL    | NULL |    1 | Range checked for each record (index map: 0x1) | 
  7. +----+-------------+-------+-------+----------------------+--------------+---------+------+------+------------------------------------------------+ 
  8. 2 rows in set (0.00 sec)

4.Using filesort

在有排序子句的情况下很常见的一种情况。此时mysql会根据联接类型浏览所有符合条件的记录,并保存排序关键字和行指针,然后排序关键字并按顺序检索行。

  1. mysql> explain select * from t_order order by express_type; 
  2. +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+ 
  3. | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows   | Extra          | 
  4. +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+ 
  5. |  1 | SIMPLE      | t_order | ALL  | NULL          | NULL | NULL    | NULL | 100395 | Using filesort | 
  6. +----+-------------+---------+------+---------------+------+---------+------+--------+----------------+ 
  7. 1 row in set (0.00 sec) 

5.Using index

这是性能很高的一种情况。当查询所需的数据可以直接从索引树中检索到时,就会出现。上面的例子中有很多这样的例子,不再多举例了。

6.Using temporary

发生这种情况一般都是需要进行优化的。mysql需要创建一张临时表用来处理此类查询。

  1. mysql> explain select * from t_order a left join t_order_ext b on a.order_id=b.order_id group by b.order_id; 
  2. +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+ 
  3. | id | select_type | table | type | possible_keys | key      | key_len | ref             | rows   | Extra                           | 
  4. +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+ 
  5. |  1 | SIMPLE      | a     | ALL  | NULL          | NULL     | NULL    | NULL            | 100395 | Using temporary; Using filesort | 
  6. |  1 | SIMPLE      | b     | ref  | order_id      | order_id | 4       | test.a.order_id |      1 |                                 | 
  7. +----+-------------+-------+------+---------------+----------+---------+-----------------+--------+---------------------------------+ 
  8. 2 rows in set (0.00 sec) 

7.Using where

当有where子句时,extra都会有说明。

8.Using sort_union(...)/Using union(...)/Using intersect(...)

下面的例子中user_id是一个检索范围,此时mysql会使用sort_union函数来进行索引的合并。而当user_id是一个固定值时,请参看上面type说明5.index_merge的例子,此时会使用union函数进行索引合并。

  1. mysql> explain select * from t_order where order_id=100 or user_id>10; 
  2. +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+ 
  3. | id | select_type | table   | type        | possible_keys   | key             | key_len | ref  | rows | Extra                                          | 
  4. +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+ 
  5. |  1 | SIMPLE      | t_order | index_merge | PRIMARY,user_id | user_id,PRIMARY | 5,4     | NULL |    2 | Using sort_union(user_id,PRIMARY); Using where | 
  6. +----+-------------+---------+-------------+-----------------+-----------------+---------+------+------+------------------------------------------------+ 
  7. 1 row in set (0.00 sec) 

对于Using intersect的例子可以参看下例,user_id与express_type发生了索引交叉合并。

  1. mysql> explain select * from t_order where express_type=1 and user_id=100; 
  2. +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+ 
  3. | id | select_type | table   | type        | possible_keys        | key                  | key_len | ref  | rows | Extra                                              | 
  4. +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+ 
  5. |  1 | SIMPLE      | t_order | index_merge | user_id,express_type | user_id,express_type | 5,1     | NULL |    1 | Using intersect(user_id,express_type); Using where | 
  6. +----+-------------+---------+-------------+----------------------+----------------------+---------+------+------+----------------------------------------------------+ 
  7. 1 row in set (0.00 sec) 

9.Using index for group-by

表明可以在索引中找到分组所需的所有数据,不需要查询实际的表。

  1. mysql> explain select user_id from t_order group by user_id; 
  2. +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+ 
  3. | id | select_type | table   | type  | possible_keys | key     | key_len | ref  | rows | Extra                    | 
  4. +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+ 
  5. |  1 | SIMPLE      | t_order | range | NULL          | user_id | 5       | NULL |    3 | Using index for group-by | 
  6. +----+-------------+---------+-------+---------------+---------+---------+------+------+--------------------------+ 
  7. 1 row in set (0.00 sec) 

除了上面的三个说明,还需要注意rows的数值,多行之间的数值是乘积的关系,可以估算大概要处理的行数,如果乘积很大,那就很有优化的必要了。

转自:http://blog.chinaunix.net/uid-540802-id-3419311.html

Guess you like

Origin blog.csdn.net/weberhuangxingbo/article/details/91383042