_ Mysql optimization of Chapter 11. (query plan papers)

Mysql Optimization (from official documents) - Chapter 11. (query plan papers)

1 EXPLAIN Output Format

As more EXPLAIN content, so there will only extract some important places, the details can refer to the original .

EXPLAIN interpretation of the results:

Column JSON Name Meaning
id select_id The SELECT identifier
select_type None The SELECT type
table table_name The table for the output row
partitions partitions The matching partitions
type access_type The join type
possible_keys possible_keys The possible indexes to choose
key key The index actually chosen
key_len key_length The length of the chosen key
ref ref The columns compared to the index
rows rows Estimate of rows to be examined
filtered filtered Percentage of rows filtered by table condition
Extra None Additional information

Pick some important columns:

  • table

    That is the table name, but it is also possible that the following three cases.

    • <union*M*,*N*>: Representing a plurality of tables union, where M and N are the first columnid
    • <derived*N*>: Indicates derived from a table in the other tables, supra, M and N represents the first column id, it is usually derived from the table in the fromstatement after theselect
    • <subquery*N*>: Indicates the subquery materialized, N of the first row id.
  • partitions

    For tables with the list of valid partition

  • type

    That is join type, there are common system``, const, eq_ref, ALLand so on, the next section will detail.

  • possible_keys

    It represents the index to be selected

  • key

    The index represents the actually used, comes from possible_keys, when possible_keysis not NULLthe time, this column also has the potential to none, because sometimes with the index does not define efficiency will be higher than sweep the table.

  • key_len

    Mysql decided to use the length of the index (which parts), when there may be a NULLtime, key_lenthe length may be larger than the column.

  • ref

    It represents a constant for comparison and index or a column.

  • rows, filtered

    rowsRepresents the number of lines to be scanned Mysql estimated, filteredthe number of rows can be estimated Mysql filtered (the value here is the number of rows returned to the user), such as rows 100, filtered 70%, then returned to the user estimated Mysql 70 rows of data.

  • Extra

    Mysql represent some additional information to resolve a query, such as Using Index,Using filesortand so on.

EXPLAIN Join Types

For type column, often used for jointhe scene, will be described with reference to low efficiency:

  • system

    constA special case, this table represents only one row of data

  • const

    It represents at most only one row of data matches, although the table may have multiple rows of data, but for a table and its join, which requires only one row of data, can be regarded as constfrequently used primary keyor unique key, such as the following statement:

    SELECT * FROM tbl_name WHERE primary_key=1;
    
    SELECT * FROM tbl_name
      WHERE primary_key_part1=1 AND primary_key_part2=2;
  • eq_ref

    Mysql index equal operation may be used, it applies only to equal, and the index type must primary keyAlternatively unique not null, when the join, which is in addition to systemand constbest case.

  • ref

    When an index is not primary key, or unique indexwhen, or only use the leftmost and they will use the refcase for =or<=>

  • fulltext

  • ref_or_null

    Ref and the like, but in addition, we also need to find additional null field, often used in some cases sub-queries, such as the following statement:

    SELECT * FROM ref_table
      WHERE key_column=expr OR key_column IS NULL;
  • index_merge

    Said they would use Section 8.2.1.3, "Index Merge Optimization" . Optimization techniques

  • unique_subquery

    And eq_refan equivalent situation, but for the sub-queries, such as the following statement:

    value IN (SELECT primary_key FROM single_table WHERE some_expr)
  • index_subquery

    And the unique_subquerylike, but is specific to the sub-queries nonunique index, such the following statement:

    value IN (SELECT key_column FROM single_table WHERE some_expr)
  • range

    Generally range will be used to find the index, in this case, keythe column represents the index is used, key_lenrepresents the longest key part to be used, refisNULL , in the index column and applies a constant to be compared, comprising: a =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, LIKE, 或者IN, for example, the following statement:

    SELECT * FROM tbl_name
      WHERE key_column = 10;
    
    SELECT * FROM tbl_name
      WHERE key_column BETWEEN 10 and 20;
    
    SELECT * FROM tbl_name
      WHERE key_column IN (10,20,30);
    
    SELECT * FROM tbl_name
      WHERE key_part1 = 10 AND key_part2 IN (10,20,30);
  • index

    Similar ALL, but in this case the index scan, usually, ExtraasUsing Index

  • ALL

    The worst case, this case can only be a full table scan, this time should consider creating an index to speed up the joinpace.

EXPLAIN Extra Information

This section is omitted, because the Extrainformation sequence are relatively straightforward, can be met is unclear reference herein .

2 Extended EXPLAIN Output Format

In the EXPLAINfollowing, the implementation of SHOW WARNINGSthe command, you will see EXPLAINsome additional information, and in 8.0.12after applicable conditions of the additional information as follows: SELECT, DELETE, INSERT, REPLACE和UPDATEin the 8.0.12before, this additional information only SELECTwill produce lower sentence, a look at the following example:

mysql> EXPLAIN
       SELECT t1.a, t1.a IN (SELECT t2.a FROM t2) FROM t1\G
*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: t1
         type: index
possible_keys: NULL
          key: PRIMARY
      key_len: 4
          ref: NULL
         rows: 4
     filtered: 100.00
        Extra: Using index
*************************** 2. row ***************************
           id: 2
  select_type: SUBQUERY
        table: t2
         type: index
possible_keys: a
          key: a
      key_len: 5
          ref: NULL
         rows: 3
     filtered: 100.00
        Extra: Using index
2 rows in set, 1 warning (0.00 sec)

mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
  Level: Note
   Code: 1003
Message: /* select#1 */ select `test`.`t1`.`a` AS `a`,
         <in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in
         ( <materialize> (/* select#2 */ select `test`.`t2`.`a`
         from `test`.`t2` where 1 having 1 ),
         <primary_index_lookup>(`test`.`t1`.`a` in
         <temporary table> on <auto_key>
         where ((`test`.`t1`.`a` = `materialized-subquery`.`a`))))) AS `t1.a
         IN (SELECT t2.a FROM t2)` from `test`.`t1`
1 row in set (0.00 sec)

Note that you can see these queries may be overridden, and contains special tags, so these statements are not necessarily legitimate SQLstatement, which is just to indicate how specific Mysql to parse and execute.

For these special markers, partially explained as follows:

  • <auto_key>

    It is automatically generated temporary key

  • ( expr)

    Indicates that the statement is cached in memory, it could be use to.

  • ( Query the fragment )
    The statement is rewritten became EXISTS

    And so on, there are many, here it is not introduced one by one.

3 Extended EXPLAIN Output Format

EXPLAIN FOR CONNECTIONStatements used in some cases performance diagnosis, such as: a sessiontime of execution of a very long, you can in one another sessionusing this command, specify a particular connection id, then you can see the amount of reasons why the implementation of such a long time, the command format is as follows:

EXPLAIN [options] FOR CONNECTION connection_id;

This command applies to SELECT, DELETE, INSERT, REPLACE, and UPDATE. However, the order does not apply to any prepared statement.

connection_idIt is an conectionidentifier that is idavailable from the INFORMATION_SCHEMA PROCESSLISTtable or the SHOW PROCESSLISTacquisition of the statement, but the user must have the relevant authority, or can not get.

Guess you like

Origin www.cnblogs.com/seancheer/p/11466458.html