MySQL performance analysis artifact Explain, you do not know it? Then you are out of

EXPLAIN shows how MySQL uses indexes to deal with the SELECT statement and connection table. You can help choose a better index and write more optimized queries.

Recommended Reading

Explain

Query execution plan

  • Use explain keyword, it can simulate execution of the SQL statement optimizer
  • So we know how to deal with MYSQL sql statement
  • You can analyze performance bottlenecks query or table structure by Explain

effect

  • Check the reading order of the table
  • Data read operation type of operation
  • See which index you can use
  • See which index is actually used
  • Check references between tables
  • Check how many rows per table is executed optimizer

Instructions

  • Before you can add EXPLAIN SELECT statement, such as:
    Here Insert Picture Description

Analysis contains information

id
  • SELECT identifier. This is a SELECT query serial number.
EXPLAIN SELECT * FROM department d, 
(SELECT * FROM employee GROUP BY dep_id) t 
WHERE d.id = t.dep_id;

Here Insert Picture Description

  • Table order of execution: employee-> d-><derived2>
  • Summary:id相同,顺序走;不同,看谁大,大的先执行 .
select_type
  • SELECT type, which can be any of the following:
    • SIMPLE: Simple select query, the query does not contain subqueries or UNION
    • PRIMARY: If the query contains any complex sub-queries, the outermost query were marked as primary.
      • Here Insert Picture Description
    • UNION: If the second select occur after union, were labeled union; union if the query from clause included in the sub, the outer layer will be marked as select deriver
    • DEPENDENT UNION: UNION in the second SELECT statement or the back, depending on outside of inquiry
    • UNION RESULT: Getting Results select from the union table, UNION merge two result sets in the final
      • Here Insert Picture Description
    • SUBQUERY: Contains sub-queries in the select or where in.
      • Here Insert Picture Description
    • DEPENDENT SUBQUERY: The first sub-query SELECT, depending on outside inquiry
    • DERIVED: From subquery included in the list are marked as derived (derivative), which places the result in a temporary table
      • Here Insert Picture Description
table
  • Referenced table row output (display data which is related to the line of tables)
partitions
  • Partition table (if the query is based on the partition table, it will show the partition query access)
type
  • system: TABLE only one row (= system table). This is a special case of the const join type.
  • const: Table up a matching line, const primary or unique index for comparison. Direct access to a primary key or unique index, 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: Unique index scan. For each index key, only one record in the table match. Common in the primary key or unique index scan, 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: Non-unique index scan. 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: Ref This type replaces the following form IN subquery: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subqueryan index lookup function, can completely replace sub-queries more efficient.
  • index_subquery: This type is similar to the coupling unique_subquery. IN subquery can be replaced, but only for the 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 of a given range, using an index to select the rows.
  • index: ALL the coupling with 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.

Requirements: In general, at least to ensure that the query range level, preferably to ref

possible_keys
  • Application of this index may be displayed in tables, one or more; relates to the field if the query index exists, the index will be listed, but not necessarily actually used by the query
key
  • Index actually used, if NULL, then do not use indexes; if the query using a covering index, the index appears only in the key list; possible_keys与key关系 :理论应该用到哪些索引,实际用到了哪些索引;the fields covered by the index and query fields created just fit, which we call covering index.
key_len
  • Indicates the number of bytes used in the index, the index can be used in the query length is calculated by the column. 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. Whether the index is introduced to, in the end which several references to the Index
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. According to the statistics table and choose the index case, a rough estimate of the required record number of lines to be read to find, how many rows per table has long been optimized queries too.
filtered
  • Shows the percentage of the estimated value of the number of rows filtered through conditions. The ratio of the number of records to satisfy the query, note the percentage, not a specific number of records; the better the value, the value of filtered column depends statistics are not very accurate.
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.
    • Not exists: 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.
    • range checked for each record (index map: #): 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.
    • Using filesort: MySQL requires additional one pass to find out how to retrieve the rows in sorted order.
    • Using index: 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.
    • Using temporary: In order to resolve the query, MySQL needs to create a temporary table to hold the result.
    • Using where: WHERE clause restricts a row which matches the next table or sent to a client.
    • Using sort_union(...), Using union(...), Using intersect(...): These functions illustrate how index_merge join type merge index scan.
    • Using index for group-by: Using index a manner similar to the access list, Using index for group-by expressed MySQL found an index that can be used to query all the columns GROUP BY or DISTINCT query, rather than additional search your hard disk to access the actual table.

The more you know, the more you do not know.
Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
If you have other questions, welcome message, we can discuss, learn together and progress together

Published 204 original articles · won praise 138 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/105239545