Explain Tool Introduction
Use EXPLAIN keyword can simulate the optimizer to execute SQL statements, query performance bottleneck analysis statements or structure. Increase explaion keyword before the select statement, MySQL will set a mark on the query, the query returns information to perform an execution plan, instead of executing SQL.
Explaion analysis examples
-- actor建表语句:
CREATE TABLE `actor` (
`id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`update_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
-- film建表语句:
CREATE TABLE `film` (
`id` int(11) NOT NULL,
`name` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
-- film_actor建表语句:
CREATE TABLE `film_actor` (
`id` int(11) NOT NULL,
`film_id` int(11) NOT NULL,
`actor_id` int(11) NOT NULL,
`remark` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_film_actor_id` (`film_id`,`actor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Execution explain:
explain select * from actor;
If the select statement returns the results in front of the select statement with explain the return of this query is executed SQL statement.
EXPLAIN two variants
explain extended
Extra query optimization provide some information on the basis of explain. Followed by the query can be optimized by show warnings commands to see what the optimizer optimized. Additionally, filtered column, is a half-point value ratio, rows * filtered / 100 can estimate the number of lines to and a table explain front connection (the previous table refer to the id value explain is smaller than the current table id value table).
explain EXTENDED select * from actor where id = 1;
explain partitions
Compared explain more than a field partitions, if the query is based on the partition table, then displays the query will access the partition.
Explain columns
id列
id number of the column is select the serial number, there are a few select few id, id and order is the order of appearance of select growth.
The greater the higher execution priority id, id from the same execution down, id finally executed as NULL.
explain select (select 1 from actor where id = 1) from (select * from film
where id = 1) der;
select type列
select type indicates that the corresponding line is a simple or complex queries.
simple: simple query. Query does not contain sub-queries and union.
explain select * from film where id=1
primary: Complex queries outermost select
subquery: subqueries of the select (not in the from clause)
derived: query comprising from sub-clause. MySQL will store the result in a temporary table, also called derived tables.
explain select (select 1 from actor where id = 1) from (select * from film
where id = 1) der;
union: Subsequent selelct in union keyword.
EXPLAIN select 1 union all select 1;
table column
This column indicates which table to explain the line being accessed.
When there are sub-clause from the query, table column
When a union, the value table column UNION RESULT <union 1,2>, 1 and 2 represent the row select id involved in the union.
type column
This column shows the type of association or the type of access that MySQL decides how to find rows in the table, find the approximate range of data rows corresponding.
In order from best to worst are: system> const> eq_ref> ref > range> index> All
general, have to ensure that the query reaches the level range, preferably up to ref.
NULL: MySQL query can be decomposed in the optimization phase, the implementation phase do not need to access the table or index. For example: Select the minimum value in the index column, lookup index may be done individually, when executed without access table.
EXPLAIN select min(id) from film;
- const, system: mysql can be optimized for a part of the query and converts it to a constant (which can be seen as the result show warnings). When comparing all columns constants for primay key or unique key, so there is at most one matching table row, 1 reading, speed reading faster. const system when the system is a special case of only one row in the table of matched elements.
EXPLAIN select * from (select * from film where id= 1) as tmp;
- eq_ref: all parts primay key or unique key index to be connected using, it will only return a qualifying record. This is probably the best outside const join type, simple select query does not appear this type.
EXPLAIN select * from (select * from film where id= 1) as tmp;
- ref: Compared eq_ref, NA unique index, but the use of the general index or unique index part of the prefix, and the index to a value compared may find more qualifying rows.
Simple select query, name is the general index (non-primary key index or unique index)
EXPLAIN select * from film where name='film1';
Related query, idx_film_actor_id
it is film_id
and actor_id
the joint index, using here to film_actor
the left of the prefix film_id
part.
EXPLAIN select film_id from film LEFT JOIN film_actor on film.id = film_actor.film_id;
- range: range scan typically occurs in (), between,>, <,> =, etc. operations. Using an index to retrieve a row given range.
EXPLAIN select * from actor WHERE id >1;
- index: full table scan index is usually faster than the number of All
EXPLAIN select * from film;
- all: namely, a full table scan, means that MySQL need to find the line from start to finish required. In this case the need to increase the index to be optimized.
EXPLAIN SELECT * from actor;
possible_keys列
This column shows what may select a query type to find.
explain may occur when possible_keys
there is a column, and displayed as a key to NULL, because this situation is not much data tables, MySQL think this query index of little help, choose a full table scan.
If this column is NULL, there is no relevant index. In this case, it is possible to improve query performance by checking where clause to see if you can create an appropriate index, then view the results with explain.
EXPLAIN SELECT * from film_actor where film_id =1;
key列
This column shows which index to access the table of MySQL actually used.
If you do not use the index, then re-classified as NULL. If you want to force MySQL to use or ignore the possible_keys
column index, the use of force index in the query, ignore index.
key_len column
This column shows the number of bytes used in the index mysql can be estimated which columns are used specifically by the index value.
EXPLAIN SELECT * from film_actor where film_id =1;
film_actor
Indexing joint idx_film_actor_id
made film_id
and actor_id
two columns id, int and each is 4 bytes. By results key_len
= 4 inferred query uses the first columns: film_id
Column index lookup is performed.
ken_len
Calculation rules are as follows:
String
char (n): n byte length
varchar (n): n string length bytes of storage, if it is utf-8, then the length is 3n + 2Value Type
tinyint: 1 byte
smallint: 2 byte
int: 4 bytes
bigint: 8 bytesTime Type
date: 3 bytes
timestamp: 4-byte
datetime: 8 bytes
If the field is NULL allowed, requires 1 byte NULL records whether
the index is a maximum length of 768 bytes, when the string is too long, MySQL will do a similar process prefixed index, the character string is extracted out of the front half portion of the indexing .
The ref column
This column shows the index key columns of records in a table lookup column values or constants used in common are: const (constant), field names and so on. Generally value the right of the search criteria or conditions associated with the equal sign, if it is so constant ref column is const, const ref, then the column is the field name.
EXPLAIN SELECT * from film_actor where film_id =1;
row column
This column is a mysql estimate the number of rows to be read and tested, note that this is not the number of rows in the result set.
Extra column
This column is additional information.
- Using index: Use a covering index (field result is the index set, i.e. after the select film_id)
explain select film_id from film_actor where film_id=1;
- Using index condition: column of the query is not completely covered by the index, where the condition is a preamble range
explain select * from film_actor where film_id > 1;
- Using where: using the where clause to process the results, the query column is not covered by the index
explain select * from actor where name ='a'
- Using temporary: mysql need to create a temporary table to process the query. This happens usually to be optimized, we must first think of is the index optimization.
explain select DISTINCT name from actor;
actor.name no index, then create a temporary table to deal with distinct.
explain select DISTINCT name from film;
file.name the establishment of a common index, Extra Using index is at this time when the inquiry, did not use temporary tables.
- Using filesort: The external sort rather than an index sort, the sort from memory, or needs to be done to sort data in the disk is small. In this case also want to consider using the general index optimization.
explain select * from actor order by name;
actor.name not create an index, you will browse acotr entire table, saving the sort key name and the corresponding id, then sort name and retrieve the rows.
explain select * from film order by name;
film.name established idx_name index, at this time when the query is extra Using index.
- select tables optimized away: using some aggregation function (example: max, min) to access a field in the presence of an index
explain select min(id) from film ;
I'm not concerned about the number of the public?
- Sweep end the two-dimensional code number [of public concern Xiaoqiang advanced road] can receive as follows:
- Learning materials: 1T Video Tutorial: front and rear end covers Javaweb instructional videos, machine learning / AI instructional videos, Linux system tutorial videos, IELTS video tutorials;
- More than 100 books: containing C / C ++, Java, Python programming language must see three books, LeetCode explanations Daquan;
- Software tools: Most software includes almost you might use in programming the road;
- Project Source: 20 JavaWeb source projects.