MySQL explain instructions

Source: Internet
Author: User
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:

Example: Explain select surname, first_name Form A, B where a. ID = B. ID

The analysis result is as follows:

Table
Type
Possible_keys
Key
Key_len
Ref
Rows
Extra

A
Range
ID

First_name
First_name
9
Null
23112
Using where

Using Temporary

Using filesort

B
Ref
ID

First_name
ID
4
ID
2
Using where

Description of the explain column:

Table
Which table is the data of this row?

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 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
It indicates which column of the index is used. If possible, it is a constant.

Rows
MySQL considers that the number of rows that must be checked to return the requested data

Extra
Additional 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 any more.

Not exists
MySQL optimizes left join. Once it finds a row that matches the left join standard,

No more search

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 and uses 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 part of the same index.

Using Temporary
When you see 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
The where clause is used to limit which rows match the next table or are returned to the user. If you do not want to return all rows in the table and the connection type is all or index, this may occur or the query is faulty.

Explanations of different connection types (sorted by efficiency order)

System
The 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 a 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 connection, MySQL reads a record 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 uses an index to return rows in a range, such as> or <what happens when something is searched

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.