Table of Contents
1. Introduction to EXPLAIN
2. The meaning of each field in the execution plan
2.1 id
DERIVED The
2.5 possible_keys and key
2.6 key_len
2.9.2 Using temporary(十死无生)
2.9.3 Using index(发财了)
2.9.4 Using where
2.9.5 Using join buffer
2.9.6 impossible where
2.9.7 select tables optimized away
2.9.8 distinct
3. 实例分析
Home Database Mysql Tutorial explain usage and result analysis in MySQL (detailed explanation)

explain usage and result analysis in MySQL (detailed explanation)

Jul 07, 2020 pm 04:04 PM
explain mysql

explain usage and result analysis in MySQL (detailed explanation)

1. Introduction to EXPLAIN

Using the EXPLAIN keyword can simulate the optimizer's execution of SQL query statements, so as to know how MySQL processes your SQL statements. Analyze the performance bottlenecks of your query statements or table structures.
Through EXPLAIN, we can analyze the following results:

  • The reading sequence of the table
  • The operation type of the data reading operation
  • Which indexes can be used
  • Which indexes are actually used
  • References between tables
  • How many rows of each table are queried by the optimizer

Used as follows:

EXPLAIN SQL statement

EXPLAIN SELECT * FROM t1
Copy after login

Information contained in the execution plan
explain usage and result analysis in MySQL (detailed explanation)

2. The meaning of each field in the execution plan

2.1 id

The sequence number of the select query, including a set of numbers, indicating the order in which select clauses or operation tables are executed in the query

There are 3 cases for the result of id

  • #The id is the same, the execution order is from top to bottom
    explain usage and result analysis in MySQL (detailed explanation)
    [Summary] The order of loading the table is as shown in the table column above: t1 t3 t2

  • id is different. If it is a subquery, the sequence number of the id will increase, and the larger the id value is. The higher the priority, the sooner it will be executed

explain usage and result analysis in MySQL (detailed explanation)


  • explain usage and result analysis in MySQL (detailed explanation)


explain usage and result analysis in MySQL (detailed explanation)

  • ## As shown in the figure, when the id is 1, the table displays

    , which refers to the table with id 2, that is, the derived table of the t3 table.
  • 2.2 select_typeCommon and commonly used values ​​are as follows:

    are used to indicate the type of query, mainly to distinguish between ordinary Complex queries such as queries, union queries, subqueries, etc.
  • SIMPLE Simple select query

    , the query
  • does not contain subqueries or UNION
  • If in a PRIMARY query contains any complex

    subparts,
  • the outermost query is marked as PRIMARY
  • ##SUBQUERY
  • in SELECT Or the WHERE list contains the subquery

DERIVED The

subquery contained in the FROM list is marked as DERIVED

(derivative), and MySQL will execute it recursively These subqueries put the

results in the temporary table


explain usage and result analysis in MySQL (detailed explanation)UNION If the second SELECT appears after UNION, it is marked as UNION: If UNION is included in In the subquery of the FROM clause, the outer SELECT will be marked as: DERIVED

UNION RESULT SELECT

###### that obtains the result from the UNION table 2.3 table###### refers to the currently executed table######2.4 type######type shows which type is used in the query. The types included in type include as shown in the figure below Several types: #########From best to worst are: ###
system > const > eq_ref > ref > range > index > all
Copy after login
###### Generally speaking, it is necessary to ensure that the query reaches at least the range level, and preferably reaches the ref. ######
  • system The table has only one row of records (equal to the system table). This is a special column of const type. It does not usually appear and this can be ignored.
  • const means that it is found through the index once, and const is used to compare the primary key or unique index. Because only one row of data is matched, it is very fast. If you place the primary key in the where list, MySQL can convert the query into a constant.
    explain usage and result analysis in MySQL (detailed explanation)
    First perform a subquery to obtain a result of the d1 temporary table. The subquery condition is id = 1, which is a constant, so the type is const. The id of 1 is equivalent to querying only one record, so the type is system.
  • eq_ref Unique index scan, for each index key, only one record in the table matches it. Commonly seen in primary key or unique index scans
  • ref Non-unique index scans return all rows that match a single value. This is essentially an index access. It returns all rows that match a certain value. For single value rows, however, it may find multiple matching rows, so it should be a mix of find and scan.
    explain usage and result analysis in MySQL (detailed explanation)
  • range Retrieve only rows in a given range, use an index to select rows, the key column shows which index is used, usually in your where statement For queries such as between, , in, etc., this range scan index is better than a full table scan, because it only needs to start at a certain point in the index and end at another point, without scanning the entire index.
    explain usage and result analysis in MySQL (detailed explanation)
  • index Full Index Scan, the difference between Index and All is that the index type only traverses the index tree. This is usually faster than ALL because index files are usually smaller than data files. (That is to say, although all and Index both read the entire table, index is read from the index, and all is read from the hard disk)
    explain usage and result analysis in MySQL (detailed explanation)
    id is the primary key, so there is a primary key index
  • all Full Table Scan will traverse the entire table to find matching rows
    explain usage and result analysis in MySQL (detailed explanation)

2.5 possible_keys and key

possible_keys Displays one or more indexes that may be applied to this table. If an index exists on the field involved in the query, the index will be listed, but may not be actually used by the query.

key

  • The actual index used. If it is NULL, no index is used. (Possible reasons include no index being created or index failure)
    explain usage and result analysis in MySQL (detailed explanation)
  • If covering index is used in the query (the field to be queried after selecting is exactly the same as the created index field) the same), the index only appears in the key list
    explain usage and result analysis in MySQL (detailed explanation)
    explain usage and result analysis in MySQL (detailed explanation)

2.6 key_len

represents the number of bytes used in the index, This column can be used to calculate the length of the index used in the query. The shorter the length, the better without losing accuracy. The value displayed by key_len is the maximum possible length of the index field, not the actual length used. That is, key_len is calculated based on the table definition, not retrieved from the table.
explain usage and result analysis in MySQL (detailed explanation)

2.7 ref

The column showing the index is used, preferably a constant if possible. Which columns or constants are used to find the value on the index column.


explain usage and result analysis in MySQL (detailed explanation)

2.8 rows

According to table statistics and index selection, roughly estimate the number of rows that need to be read to find the required records, that is, use The less the better


explain usage and result analysis in MySQL (detailed explanation)

2.9 Extra

Contains important additional information that is not suitable for explicit use in other columns

2.9.1 Using filesort (narrow escape)

Indicates that mysql will use an external index to sort the data instead of reading it in the index order within the table. The sorting operation that cannot be completed using indexes in MySQL is called "file sorting".


explain usage and result analysis in MySQL (detailed explanation)

2.9.2 Using temporary(十死无生)

使用了用临时表保存中间结果,MySQL在对查询结果排序时使用临时表。常见于排序order by和分组查询group by。
explain usage and result analysis in MySQL (detailed explanation)

2.9.3 Using index(发财了)

表示相应的select操作中使用了覆盖索引(Covering Index),避免访问了表的数据行,效率不错。如果同时出现using where,表明索引被用来执行索引键值的查找;如果没有同时出现using where,表明索引用来读取数据而非执行查找动作。
explain usage and result analysis in MySQL (detailed explanation)
explain usage and result analysis in MySQL (detailed explanation)

2.9.4 Using where

表明使用了where过滤

2.9.5 Using join buffer

表明使用了连接缓存,比如说在查询的时候,多表join的次数非常多,那么将配置文件中的缓冲区的join buffer调大一些。

2.9.6 impossible where

where子句的值总是false,不能用来获取任何元组

SELECT * FROM t_user WHERE id = '1' and id = '2'
Copy after login

2.9.7 select tables optimized away

在没有GROUPBY子句的情况下,基于索引优化MIN/MAX操作或者对于MyISAM存储引擎优化COUNT(*)操作,不必等到执行阶段再进行计算,查询执行计划生成的阶段即完成优化。

2.9.8 distinct

优化distinct操作,在找到第一匹配的元组后即停止找同样值的动作

3. 实例分析

explain usage and result analysis in MySQL (detailed explanation)

  • 执行顺序1:select_type为UNION,说明第四个select是UNION里的第二个select,最先执行【select name,id from t2】
  • 执行顺序2:id为3,是整个查询中第三个select的一部分。因查询包含在from中,所以为DERIVED【select id,name from t1 where other_column=’’】
  • 执行顺序3:select列表中的子查询select_type为subquery,为整个查询中的第二个select【select id from t3】
  • 执行顺序4:id列为1,表示是UNION里的第一个select,select_type列的primary表示该查询为外层查询,table列被标记为<derived3></derived3>,表示查询结果来自一个衍生表,其中derived3中的3代表该查询衍生自第三个select查询,即id为3的select。【select d1.name …】
  • 执行顺序5:代表从UNION的临时表中读取行的阶段,table列的表示用第一个和第四个select的结果进行UNION操作。【两个结果union操作】

推荐学习:mysql教程

The above is the detailed content of explain usage and result analysis in MySQL (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles