当前位置:网站首页>Introduction to the usage of explain and the meaning of result field in MySQL
Introduction to the usage of explain and the meaning of result field in MySQL
2022-07-25 06:15:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Be a positive person code 、 Change bug、 Improve yourself I have a paradise , Programming Oriented , Spring flowers
List of articles
Use explain Query and analysis SQl The execution record of , Can be done sql Performance optimization !
explain usage
mysql> explain select * from students;
+----+-------------+----------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | students | ALL | NULL | NULL | NULL | NULL | 3 | |
+----+-------------+----------+------+---------------+------+---------+------+------+-------+
1 row in set
mysql> explain extended select * from students;
+----+-------------+----------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | students | ALL | NULL | NULL | NULL | NULL | 3 | 100 | |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+-------+
1 row in setexplain Introduction to the meaning of the result field
id
- SELECT Identifier . This is a SELECT The query serial number of , Represents execution in a query select The order of clauses or operation tables !
select_type
SELECT type , It can be any of the following :
- SIMPLE: Simple SELECT( Don't use UNION Or subquery )
- PRIMARY: The outermost SELECT
- UNION:UNION The second or later of SELECT sentence
- DEPENDENT UNION:UNION The second or later of SELECT sentence , Depends on external queries
- UNION RESULT:UNION Result
- SUBQUERY: First in subquery SELECT
- DEPENDENT SUBQUERY: First in subquery SELECT, Depends on external queries
- DERIVED: Export table's SELECT(FROM A subquery of a clause )
table
The table referenced by the output row !
type
Connection type . Here are the various connection types , Sort from the best type to the worst type :
- system: The watch has only one line (= The system tables ). This is a const A special case of a join type .
- const: The table has at most one matching row , It will be read at the beginning of the query . Because there is only one line , The column values in this row can be considered constant by the rest of the optimizer .const The watch is fast , Because they only read once !
- eq_ref: For each row combination from the previous table , Read a row from the table . This is probably the best type of join , except const type .
- ref: For each row combination from the previous table , All rows with matching index values are read from this table .
- ref_or_null: The join type is like ref, But added MySQL You can search specifically for NULL Row of values .
- index_merge: The join type indicates that the index merge optimization method is used .
- unique_subquery: This type replaces the following form of IN The subquery ref: value IN (SELECT primary_key FROM single_table WHERE some_expr) unique_subquery Is an index lookup function , You can completely replace subqueries , More efficient .
- index_subquery: The join type is similar to unique_subquery. Can replace IN Subquery , But it is only suitable for non unique indexes in sub queries of the following forms : value IN (SELECT key_column FROM single_table WHERE some_expr)
- range: Retrieve only rows in the given range , Use an index to select rows .
- index: The connection type is the same as ALL identical , Except that only index trees are scanned . This is usually better than ALL fast , Because index files are usually smaller than data files .
- ALL: For each row combination from the previous table , Do a full table scan .
possible_keys
Pointed out that MySQL Which index can be used to find records in the table , If there is an index on the field involved in the query , Then the index will be listed , But not necessarily used by queries
key
Show MySQL The index actually used in the query , If you don't use an index , Is shown as NULL
key_len
Represents the number of bytes used in the index , You can use this column to calculate the length of the index used in the query (key_len The displayed value is the maximum possible length of the index field , It's not the actual length , namely key_len It is calculated according to the table definition , It is not retrieved from the table )
ref
Indicates the join matching condition of the above table , That is, which columns or constants are used to find values on index columns
rows
Show MySQL The number of rows that it must check to execute the query . Multiplying the data between multiple rows can estimate the number of rows to process
filtered
Shows the percentage estimate of the number of rows filtered by the condition .
Extra
This column contains MySQL Solve query details
- Distinct:MySQL Find the first 1 After a match line , Stop searching for more rows for the current row combination .
- Not exists:MySQL Able to query LEFT JOIN Optimize , Find out 1 A match LEFT JOIN After the standard line , No longer check more rows in the table for the previous row combination . range checked for each record (index map: #):MySQL No good index can be found , But found that if the column value from the previous table is known , Maybe some indexes can be used .
- Using filesort:MySQL Need an extra pass , To find out how to retrieve rows in sort order .
- Using index: To retrieve column information in a table from an actual row that uses only the information in the index tree without further search .
- Using temporary: To solve the query ,MySQL You need to create a temporary table to hold the results .
- Using where:WHERE Clause is used to restrict which row matches the next table or is sent to the customer .
- Using sort_union(…), Using union(…), Using intersect(…): These functions show how to index_merge Join type merge index scan .
- Using index for group-by: Similar to accessing tables Using index The way ,Using index for group-by Express MySQL Found an index , It can be used to check Inquiry GROUP BY or DISTINCT All columns of the query , Don't search the hard disk extra to access the actual table .
explain Using examples
mysql> show keys from students;
+----------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| students | 0 | PRIMARY | 1 | stud_id | A | 3 | NULL | NULL | | BTREE | | |
| students | 1 | index_create_date | 1 | create_date | A | 3 | NULL | NULL | YES | BTREE | | |
+----------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set
mysql> explain select * from students where stud_id = '1';
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | students | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+----------+-------+---------------+---------+---------+-------+------+-------+
1 row in set
mysql> explain select * from students where create_date = '2010-01-01';
+----+-------------+----------+------+-------------------+-------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+-------------------+-------------------+---------+-------+------+-------------+
| 1 | SIMPLE | students | ref | index_create_date | index_create_date | 4 | const | 1 | Using where |
+----+-------------+----------+------+-------------------+-------------------+---------+-------+------+-------------+
1 row in setsummary
- EXPLAIN I won't tell you about triggers 、 The impact of stored procedure information or user-defined functions on queries
- EXPLAIN Don't think about all kinds of Cache
- EXPLAIN Can't show MySQL Optimizations done when executing queries
- Some of the statistics are estimated , Not exactly
- EXPALIN Can only explain SELECT operation , Other operations need to be rewritten as SELECT Then check the execution plan .
Refer to the post
mysql in explain Meaning of usage and result
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/121081.html Link to the original text :https://javaforall.cn
边栏推荐
- Pdf snapshot artifact
- Common API of window
- “蔚来杯“2022牛客暑期多校训练营2 Link with Game Glitch (spfa找正负环)
- Review of some classic exercises of arrays
- 有什么能在网上挣钱的项目啊?做自媒体靠谱吗?
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2 link with game glitch (SPFA finds positive and negative links)
- Machine learning keras fitting sine function
- 剑指 Offer 54. 二叉搜索树的第k大节点
- HTB-Optimum
- Leetcode/ binary addition
猜你喜欢

都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
![[C language] in depth understanding of pointers and arrays (phase I)](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
[C language] in depth understanding of pointers and arrays (phase I)

How does vscode enable multiple terminals? How to display horizontally?

10、渲染基础

Special episode of Goddess Festival | exclusive interview with Chinese AI goddess Zhang Qingqing's transformation from a female learning tyrant to a female entrepreneur

How to play a data mining game entry Edition

Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)

Netease game Flink SQL platform practice

What determines the "personality" of AI robots?

Productivity tool in the new era -- flowus information flow comprehensive evaluation
随机推荐
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
HTB-Arctic
HTB-Devel
UML modeling tools Visio, rational rose, powerdesign
(15) [driver development] over written copy
Function template learning record
(16) [system call] track system call (3 rings)
[QT] solve the problem of Chinese garbled code output from QT console
Codeforces Round #809 (Div. 2)
SAP FICO section III BDC and ltmc import S4 financial account
【datawhale202207】强化学习:策略梯度和近端策略优化
Why is it that all the games are pseudorandom and can't make true random?
It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here
Dynamic planning learning notes
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
U-boot-1.1.6 transplant notes (beginner)
ROI pooling and ROI align
【luogu P6629】字符串(Runs)(树状数组)
Big talk · book sharing | Haas Internet of things device cloud integrated development framework
(牛客多校二)G-Link with Monotonic Subsequence(构造题)