EXPLAIN mysql optimization of (a)

Database optimization is the most commonly used commands with check writing sql explain whether or not to use the index:

Such as:

(root@localhost) [akapp]>explain select * from sc_activity where id='3a2cd2a83892d322c1332acdfe';
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE noticed after reading const tables |
+----+-------------+-------+------+---------------+------+---------+------+------+-----------------------------------------------------+
1 row in set (0.00 sec)

(root@localhost) [akapp]>explain select * from sc_activity where id like '%3a2cd2a83892d322c1332acdfe%';
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | sc_activity | ALL | NULL | NULL | NULL | NULL | 6665 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+------+-------------+

The meaning of each column is as follows:

1, id: SQL execution sequence identity.

 

sql performed from the inside out, through the above observation that sql is performed in accordance with descending id.

2, select_type: select type

1), SIMPLE (without using UNION or subqueries, etc.)
2), a PRIMARY: outermost SELECT
. 3), DERIVED: derived table SELECT (FROM clause sub-query)
. 4), UNION: UNION second or the back of the SELECT statement
5), UNION rESULT: the results of UNION.
6), DEPENDENT UNION: UNION second or later SELECT statement, depending on the outside of the query
7), SUBQUERY: a first subquery SELECT
8), DEPENDENT SUBQUERY: a first sub-query SELECT depending on outside of inquiry
3, table: the name of the table.
Sometimes not real table names, see derivedx (x is a number, I understand that the results of the first steps performed)
4, of the type: the type of connection operation.
This column is very important, which shows the connection using categories, with or without the use of an index. Among various types of association, it is the most efficient system, and then followed by const, eq_ref, ref, range, index , and All. In general, the inquiry was to ensure that at least the range level best to achieve ref, otherwise we may experience performance problems.

1), system
tables only one line: system table. This is a special case of the type of connection const
2), 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, the actual value is constant because MYSQL first read value and then using it as a constant to treat
3), eq_ref
in connection, MYSQL in the query, from the preceding table, the joint for each record are from table reading a record, it query uses the index primary key or all of the unique key used
4), ref
this type of connection is only the query using not the only or primary key bond or is these types of sections (for example, using It occurs when) the leftmost prefix. For each row of the table before the joint, all records are read from the table. This type relies heavily on the index matching records how much (as possible)

5), range
of this type of connection to use the index to return a range of rows, such as using> or <happens when looking for something
6), index
the connection type for each record in front of the table jointly full scan (ratio ALL better, because the index is generally less than the data table)
. 7), ALL
this type of connection to the front of each record full scan joint, which is generally relatively poor, should be avoided. Because it needs to scan the entire table. You can add more indexes to solve this problem.
5, possible_key: MySQL in the search data records can be selected for each index name.
Here the index name is specified when creating the index the index nickname; if the index is not a nickname, the default display is the index in the first column name (in the example cited in the previous section is the "firstname"). The default meaning of the name index is often not obvious.

6, key: it shows the actual use MySQL index.

MySQL key data column is the actual selection index, if it is empty (or NULL), then the index is not used MySQL.

7, key_len: the length of the index portion is used, in bytes.

Key_len can tell you the value of the index in the United mysql which will actually use the index. In the above example, The key_len is 102, which accounts for 50 bytes firstname, lastname of 50 bytes, age 2 bytes (smallint The storage size is 2 bytes). If MySQL to use only part of the index in the firstname, the key_len will be 50. Without loss of accuracy, key_len data column in the smaller value is better (meaning faster).

8, ref: shows which columns or constants are selected together with a key from a table using the row.

ref data column gives the name of the association in another data table data columns.

9, rows: MySQL perceived it the number of records that must be scanned before finding the correct result.

This data better.

10, extra: Additional information

Using index and Using where encounter more, we can focus on a note, see using filesort is the focus of attention.

1), Distinct
Once MYSQL found from row to row with the United match, the search is no longer
2), Not EXISTS
MYSQL optimized LEFT JOIN, once it finds a match LEFT JOIN standard line, no longer searched
3) , Range checked for each
is not found over the index, so for each combination of a row from the front table, which index is used MYSQL Based check, and use it to return rows from the table. This is an index of one of the slowest connection
4), 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 of the rows is not recommended if this is not sorted according to the type of connection lines and all the row pointer store and sort key match condition
5) Using index
column using only data from the index information without reading the actual action the table returns, which occurs when all of the requests on the table columns are part of the same index of
6) 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 carry out different set of columns, BY rather than on the GROUP
7) the Using the WHERE
use the WHERE clause to restrict which rows match with the next table or It is returned to the user. If you do not want to return all the rows in the table, and the connection type ALL or index, this will happen, or is the query in question
8) Impossible WHERE noticed after reading const tables | 5.7.17 show no matching row in const table
The reason "Impossible WHERE noticed after reading const tables" is such that, when the following conditions are met the WHERE clause exists in the query, MySQL will first search before EXPLAIN on that condition that the corresponding record, and a record of the actual used to replace all values ​​in this table attribute query. This is because when the following four conditions are met, will make the hits can only produce a query against the table. In the case of the table can not be hit data will prompt "line no match is found in the const table table," and that "const table" to refer to the table to meet the following four conditions. This is a MySQL optimization strategy.

a, when the query contains a table of non-null primary key or unique index column
b, the determination condition for this column is equivalent condition
c, the target value of the same type with the type of column
d, determining a target value constants

 

Guess you like

Origin www.cnblogs.com/wzk-0000/p/11079414.html