mysql ------ explain Tools

Based mysql5.7, innodb storage engine

Use keywords to simulate explain the optimizer to execute SQL statements, analyze the performance bottleneck in your query or keyword structure increases explain before the select statement, MySQL will set a mark on the query, execute query returns execution plan information, rather than the implementation of this SQL, if from contains sub-queries will execute the sub-query results into a temporary table

The end of the construction of the table statement uses to text

explain select * from actor;

 

Each table will output a line in the query, if there are two tables join query by join, then the output will be two lines

 

explain the results of field descriptions

1. 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. id column the greater the higher execution priority, id from executing the same downward, id finally performed to NULL.

2. select_type列

select_type indicates that the corresponding line is a simple or complex queries.

1) simple: simple query. Query does not contain sub-queries and union

2) primary: a complex query in the outermost select

3) subquery: select included in the sub-queries (not in the from clause)

4) derived: included in the from clause subqueries. MySQL will store the result in a temporary table, also known as

Derived table (derived English meaning)

5) union: select the second and subsequent union of the

Example:

Because I need to shut down mysql 5.7 is optimized to merge derived table, otherwise invisible derived Table

set session optimizer_switch = 'derived_merge = off'; # Close mysql5.7 new features derived merger table optimization

set session optimizer_switch = 'derived_merge = on'; # restore the default configuration

explain select (select 1 from actor where id = 1) from (select * from film where id = 1) der;

 

explain select * from actor union select * from  film;

 

3. table column

This column indicates which table to explain the line being accessed.

When there is a subquery in the from clause, table columns are <derivedN> format, showing the current query dependent query id = N, so the first query execution id = N.

When a union, the value table column UNION RESULT <union 1,2>, 1 and 2 represent the row select id involved in the union.

4. partitions column

If the query is based on the partition table, then displays the query will visit the district

5. type column is more critical

This column shows the type of association or the type of access that MySQL decides how to find rows in the table.

In order from best to worst are: System> const> eq_ref> ref> the Range> index> ALL ,

In general, the inquiry was reached to ensure range level best to achieve ref,

NULL:

mysql query can be decomposed in the optimization phase, the implementation phase do not need to re-access the table or index. For example: Select the minimum value in the index column, can be done alone lookup index is not required to access the table during execution

explain select min(id) from film;

const, system

mysql can be optimized for a part of the query and converts it into a constant. When comparing all columns constants or unique key for the primary key, the table up to a matching row 1 is read, faster. const system is a special case, only one data table system

explain select * from  select * from film where id = 1;

The first statement by a unique primary key index query, only one matching data, it is const, the second query is a query on the basis of the results, only one result data, the second query directly is system.

eq_ref

All part of the primary key or unique key index to be connected using, it will only return a qualifying record. This is probably the best connection type, simple select query does not appear in this type outside const.

explain select * from film_actor left join film on film_actor.film_id = film.id;

ref

Compared eq_ref, without the use of a unique index, but use some common prefix index or unique index, the index and a value to be compared may find more qualifying rows.

1. Simple select query, name is the general index (non-primary key index)

explain select * from film_actor where film_id = 1;

 

2. The association table query, idx_film_actor_id joint index film_id and actor_id, the use of the prefix film_id here to the left part of film_actor.

explain select film_id from film left join film_actor on film.id = film_actor.film_id;

range

Scanning range, usually 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, which is usually faster than ALL few.

explain select * from film;

ALL

That is, a full table scan, which means mysql need to find the line from start to finish required. Often this requires adding indexes to optimize the

explain select * from actor;

 

 

6. possible_keys列

This column shows the query which may use the index to find.

There are columns when possible possible_keys explain, while the case key display NULL, this situation is not much because the data in the table, mysql think this query index of little help, choose a full table query.

If the column is NULL, no relevant index. In this case, you can see if you can create an appropriate index to improve query performance, and then view the results by checking with explain the where clause.

 

7. key列

This column shows which index mysql actually used to optimize access to the table.

If you do not use the index, the column is NULL. If you want to force mysql to use or ignore possible_keys column index, the use of force index in the query, ignore index.

8. key_len column

This column shows the number of bytes in the index mysql used, this value can be calculated by the specific use in which the column index.

For example, the joint index idx_film_actor_id FILM_ACTOR and a film_id int actor_id two columns, and each is a 4-byte int. By results of key_len = 4 inferred query uses the first column: film_id column to perform an index lookup.

explain select * from film_actor where film_id = 2;

key_len following calculation rules:

String  

char (n): n byte length

varchar (n): 2-byte string length storage, if it is utf-8, then the length 3n + 2

Numeric types

tinyint: 1 byte

smallint: 2 bytes

int: 4 bytes

bigint: 8 bytes

Time Type date: 3 bytes

timestamp: 4 bytes

datetime: 8 bytes

If the field is allowed to NULL, it requires 1 byte record is NULL

The maximum length of index is 768 bytes, when the string is too long, mysql will do processing a prefix index similar to the left, the first half of the character is extracted indexed.

9. 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 (example: film.id)

10. rows Column

This column is to be read and mysql estimate the number of rows of detection, note that this is not the number of rows in the result set.

11. filtered column

How much data filtering, accounting

12. Extra column is more critical

This column shows that the additional information. Important common values ​​are as follows:

1) the Using index : Use a covering index

We know Innodb, the general index leaf node is stored in primary key index, so we finally need to find data by ordinary look, Finally, you need to fetch data in the primary key index tree above, but if if the field we query itself in the general index tree top present, there is no need to return to the primary key index tree to take the data; including sorting, too, may be used directly sorted index.

explain select name from film where name = "film0";

2) where the Using : Use where statement to process the results, the query column is not covered by the index

explain select * from actor where name = 'a';

 

 

. 3) the Using index for condition Condition : column of the query is not completely covered by the index, where a range of conditions leading column;

explain select * from film_actor where film_id > 2;

4) the Using the Temporary : MySQL needs to create a temporary table to process the query. This happens generally is to be optimized, first thought of using the index to optimize.

explain select distinct name from actor;

actor.name no index, then create a temporary table to distinct Zhang

 

film.name established idx_name index, when this time is extra query using index, did not use a temporary table

explain select distinct name from film;

 

5) the 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.

actor.name not create indexes, the entire table will browse actor, save the sort key name and the corresponding id, name and then sort and retrieve rows

explain select * from actor order by name;

film.name established idx_name index, then the query is extra using index, because the index has always been a good sequence of rows of data structures, can be used as sort of an index

explain select * from film order by name;

. 6) Optimized the Select Tables Away : using some aggregation function (such as max, min) to access a field in the presence of an index

explain select min(id) from film;

Built table sql:

DROP TABLE IF EXISTS `actor`;
CREATE TABLE `actor` (
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `actor` (`id`, `name`) VALUES (1,'a'), (2,'b'), (3,'c');

DROP TABLE IF EXISTS `film`;
CREATE TABLE `film` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `film` (`id`, `name`) VALUES (3,'film0'),(1,'film1'),(2,'film 2');

DROP TABLE IF EXISTS `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;
INSERT INTO `film_actor` (`id`, `film_id`, `actor_id`) VALUES (1,1,1), (2,1,2),(3,2,1);

 

Guess you like

Origin www.cnblogs.com/nijunyang/p/11407620.html