EXPLAIN the EXPLAIN command in MySQL (for example, MySQLexplain)
BitsCN.com
Explain shows how mysql uses indexes to process select statements and connect tables. It can help you select better indexes and write more optimized query statements.
You can add the explain statement before the select statement:
For example:
explain select surname,first_name form a,b where a.id=b.id
Description of the EXPLAIN column:
Table: displays the data of this row about which table
Type: this is an important column that shows the type used by the connection. The connection types from the best to the worst are const, eq_reg, ref, range, indexhe, and ALL.
Possible_keys: displays the indexes that may be applied to this table. If it is null, there is no possible index. You can select an appropriate statement from the WHERE statement for the relevant domain.
Key: actually used index. If it is NULL, no index is used. In rare cases, MYSQL selects an optimized index. In this case, you can use index (indexname) in the SELECT statement to force an INDEX or use ignore index (indexname) to force MYSQL to IGNORE the INDEX.
Key_len: the length of the index used. The shorter the length, the better.
Ref: indicates which column of the index is used. if possible, it is a constant.
Rows: the number of rows that MYSQL deems necessary to check to return the requested data
Extra: Extra information about how MYSQL parses the query. We will discuss it in Table 4.3, but here we can see that the bad examples are Using temporary and Using filesort, which means MYSQL cannot use indexes at all, and the result is that the retrieval will be slow.
Meaning of the description returned by the extra column
Distinct: once MYSQL finds the row that matches the row, it does not search again.
Not exists: MYSQL optimizes left join. once it finds a row that matches the left join standard, it no longer searches.
Range checked for each Record (index map: #): no ideal index is found. Therefore, for each row combination in the preceding table, MYSQL checks which index is used, use it to return rows from the table. This is one of the slowest connections using indexes.
Using filesort: When you see this, the query needs to be optimized. MYSQL requires additional steps to find out how to sort the returned rows. It sorts all rows according to the connection type and the row pointer that stores the sort key value and all rows matching the condition.
Using index: The column data is returned from a table that only uses the information in the index but does not read the actual action. This occurs when all the request columns in the table are in the same index.
When Using temporary sees this, the query needs to be optimized. Here, MYSQL needs to create a temporary table to store the results. this usually happens when order by is applied to different column sets, rather than group.
Where used uses the WHERE clause to limit which rows match the next table or return the rows to the user. If you do not want to return ALL rows in the table and the connection type is ALL or index, this will happen, or if there is a problem with the query interpretation of different connection types (sort by efficiency order)
The system table has only one row: system table. This is a special case of the const connection type.
Const: the maximum value of a record in the table can match this query (the index can be a primary key or a unique index ). Because there is only one row, this value is actually a constant, because MYSQL first reads this value and treats it as a constant.
Eq_ref: during the connection, MYSQL reads a record from the table from the union of each record in the previous table during query, it is used when you query all data that uses the index as the primary key or unique key.
Ref: this connection type only occurs when the query uses keys that are not the only or primary key, or some of these types (for example, using the leftmost prefix. For each row union in the previous table, all records are read from the table. This type depends heavily on the number of records matched by the index-the fewer the better
Range: this connection type returns rows in a range using an index, for example,> or <查找东西时发生的情况< p>
Index: This connection type performs a full scan of each record in the preceding table (better than ALL, because the index is generally smaller than the table data)
ALL: this connection type performs a full scan for each of the preceding records. this is generally poor and should be avoided as much as possible.
Source: http://database.51cto.com/art/200912/168453.htm
BitsCN.com