In daily work, we sometimes run slow queries to record some SQL statements that take a long time to execute. Finding these SQL statements does not mean that the job is done. Sometimes we often use the explain command to view the execution plan of these SQL statements to see whether the SQL statement uses an index and whether a full table scan is performed. This can be viewed through the explain command. So we have a deep understanding of MySQL's cost-based optimizer, and we can also get a lot of details about the access strategies that may be considered by the optimizer, and which strategy is expected to be adopted by the optimizer when running SQL statements. EXPLAIN shows how MySQL uses indexes to process select statements and join tables. It can help choose better indexes and write more optimized query statements. To use it, just add explain before the select statement: like: explain select surname,first_name form a,b where a.id=b.id 1. EXPLAIN tbl_name EXPLAIN tbl_name is a synonym for DESCRIBE tbl_name or SHOW COLUMNS FROM tbl_name. 2. EXPLAIN [EXTENDED] SELECT select_options If you place the keywords EXPLAIN before a SELECT statement, MySQL explains how it processes the SELECT, providing information about how the tables are joined and the order in which they are joined. With the help of EXPLAIN, you can know when you must add an index to the table to get a faster SELECT that uses the index to find records. You can also tell whether the optimizer joined the tables in an optimal order. To force the optimizer to join a SELECT statement in the order in which the tables are named, the statement should begin with STRAIGHT_JOIN rather than just SELECT. EXPLAIN returns one row for each table used in a SELECT statement. The tables are listed in the order in which they will be read by MySQL while processing the query. MySQL resolves all joins using a single-sweep multi-join approach. This means that MySQL reads a row from the first table, then finds a matching row in the second table, then in the third table, and so on. When all tables have been processed, it outputs the selected columns and returns to the table list until it finds a table with more matching rows. Read the next row from the table and continue processing the next table. When the EXTENDED keyword is used, EXPLAIN produces additional information that can be viewed with SHOW WARNINGS. This information shows what the optimizer does after qualifying the table and column names in the SELECT statement, rewriting and executing the optimization rules, and may include other comments about the optimization process. Each output row of EXPLAIN provides information about a table, and each row consists of the following columns: id: SELECT identifier. This is the query sequence number for the SELECT. select_type: SELECT type.
table: table name type: connection type
possible_keys: The possible_keys column indicates which index MySQL can use to find rows in the table. Note that this column is completely independent of the order of the tables shown in the EXPLAIN output. This means that some of the keys in possible_keys cannot actually be used in the order the table is generated. key: The key column shows the key (index) that MySQL actually decided to use. If no index was chosen, key is NULL. To force MySQL to use or ignore the index on the possible_keys column, use FORCE INDEX, USE INDEX, or IGNORE INDEX in your query. key_len: The key_len column shows the key length that MySQL decided to use. If key is NULL, length is NULL. Note that the key_len value allows us to determine how many parts of a multi-part keyword MySQL will actually use. ref: The ref column shows which column or constant is used along with key to select rows from the table. rows: The rows column shows the number of rows MySQL thinks it must examine to execute the query. Extra: This column contains detailed information about how MySQL solved the query.
By multiplying all the values in the rows column of EXPLAIN output, you can get a hint about how a join will work. This should tell you roughly how many rows MySQL had to examine to execute the query. This product is also used to determine which multi-table SELECT statement to execute when you use the max_join_size variable to limit the query. Summarize This is the end of this article about the use of EXPLAIN command in SQL. For more information about the use of SQL EXPLAIN command, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: Will css loading cause blocking?
Experimental environment • A minimally installed ...
Shell Script #!/bin/sh # Current directory CURREN...
Table of contents Related dependency installation...
=================================================...
This article introduces an example of how to use ...
Preface Adding drag and drop functionality is a g...
Table of contents What is the Picker component Pr...
Anyone who has studied or used HTML should be fam...
Table of contents Configuration parsing Service C...
This article shares the specific code of JavaScri...
How to obtain SQL statements with performance iss...
When an employer asks you whether an index will b...
1. Optimization of commonly used HTML tags HTML s...
Table of contents Preface: 1. Understand lock wai...
1. Implementation principle of scrolling The scro...