当前位置:网站首页>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
边栏推荐
- SAP FICO 第三节 BDC和LTMC导入S4财务科目
- Evolution of coupon architecture under C2B mode
- Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql
- 【每日一练】day(14)
- JTAG debugging source level debugging of arm bare board debugging
- 2022 "strong country Cup" preliminary WP (with script and detailed process)
- ROI pooling and ROI align
- 嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
- (Niuke multi School II) j-link with arithmetic progress (least square method / three points)
- Pdf snapshot artifact
猜你喜欢

4、 MFC toolbar, runtime class information mechanism, runtime creation mechanism

(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)

(Niuke multi school I in 2022) i-chiitoitsu (expected DP)

node.express中req.body总是undefind解决

R奇怪语法总结

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

Promise implementation

Big talk · book sharing | Haas Internet of things device cloud integrated development framework

Unity model simplification / consolidation one click plug-in
![(16)[系统调用]追踪系统调用(3环)](/img/b0/011351361135fd9f8e2d0d31749f73.png)
(16)[系统调用]追踪系统调用(3环)
随机推荐
mysql数据库备份和恢复
剑指 Offer 32 - I. 从上到下打印二叉树
leetcode/二进制加法
[daily practice] day (14)
JS gets the text selected by the mouse and is in the selected state
【每日一练】day(14)
(14) [driver development] configuration environment vs2019 + wdk10 write XP driver
HTB-Devel
Siggraph 2022 -- rendering iridescent rock dove neck feathers
Unity Animator动画与状态机
Review of three traversal methods of map
MySQL queries the table name under the current database
Date (day 76)
Netease game Flink SQL platform practice
Detailed annotation and analysis of start.s of uboot
(Niuke multi School II) G-LINK with monotonic subsequence (construction question)
[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node
Promise implementation
leetcode/ 前 n 个数字二进制中 1 的个数
Machine learning keras fitting sine function