EXPLAIN analysis table query problem

1. What is table return query?

There are two types of mysql indexes: primary key index, which is actually a clustered index; apart from the primary key index, others are called non-primary key indexes. Non-primary key indexes are also called secondary indexes, or auxiliary indexes.

For primary key indexes and non-primary key indexes, the data structures used are B+Tree. The only difference is that the content stored in the leaf nodes is different:

The leaf node of the primary key index stores a complete row of data.

The leaf nodes of non-primary key indexes store primary key values.

When we need to query:

If the data is queried through the primary key index, for example, select * from user where id=100, then you only need to search the B+Tree of the primary key index to find the data.

If you query data through a non-primary key index, such as select * from user where username='javaboy', then you need to search the B+Tree of the username column index first.

After the search is completed, the value of the primary key is obtained, and then the B+Tree of the primary key index is searched to obtain a complete row of data.

For the second query method, a total of two B+Tree are searched. The first time the B+Tree is searched, the primary key value is obtained, and then the B+Tree of the primary key index is searched. This process is the so-called table return.

2. EXPLAIN analysis table return problem

You can query the SQL execution efficiency through EXPLAIN and analyze the SQL execution status through the results:

Among them, type: This is one of the most important fields, showing what type is used in the query. The connection types from best to worst are system, const, eq_reg, ref, range, index and ALL. Generally speaking, it is necessary to ensure that the query reaches at least the range level, and preferably reaches ref.

Guess you like

Origin blog.csdn.net/WXF_Sir/article/details/131717481