Explain tool introduction

Explain tool introduction

table of Contents

1. Explain tool introduction

1.1.Explain analysis example

1.2.1.type


1. Explain tool introduction

Use the EXPLAIN keyword to simulate the optimizer to execute SQL statements, analyze the performance bottleneck of your query statement or structure. Add the explain keyword before the select statement. MySQL will set a flag on the query, and the execution of the query will return the execution plan information. Instead of executing this SQL.

Note: If the from contains a subquery, the subquery will still be executed and the result will be placed in a temporary table.

1.1.Explain analysis example

Three tables need to be created as follows:

actor table:

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `actor`;
CREATE TABLE `actor` (
  `id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `actor` VALUES ('1', 'a', '2017-12-22 15:27:18');
INSERT INTO `actor` VALUES ('2', 'b', '2017-12-22 15:27:18');
INSERT INTO `actor` VALUES ('3', 'c', '2017-12-22 15:27:18');

film table:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `film`
-- ----------------------------
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 AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `film` VALUES ('3', 'film0');
INSERT INTO `film` VALUES ('1', 'film1');
INSERT INTO `film` VALUES ('2', 'film2');

film_actor table:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `film_actor`
-- ----------------------------
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` VALUES ('1', '1', '1', null);
INSERT INTO `film_actor` VALUES ('2', '1', '2', null);
INSERT INTO `film_actor` VALUES ('3', '2', '1', null);

 1.2. Explain attributes in detail

-- 走的是全表扫描
EXPLAIN SELECT * from actor;

Note: When using EXPLAIN, we temporarily pay attention to type (will be added later)

1.1.1.type

This column indicates the type of association or access type, that is, MySQL decides how to find the rows in the table, and find the approximate range of the data row records.

From the best to the worst, they are: system> const> eq_ref> ref> range> index> ALL. (After sql optimization, ensure that the query knocks down the range, preferably ref)

Note: Before introducing this, there is a special situation that needs to be focused on.

NULL: mysql can decompose the query statement in the optimization phase, and there is no need to access the table or index during the execution phase.

For example: selecting the minimum value in the index column can be done by searching the index separately, without accessing the table during execution.

EXPLAIN SELECT min(id) from film; -- type 为null 表示单独查找索引就能完成,不需要对表有操作

Analysis: For the film table, id is the primary key. This query logic can be done directly by querying the primary key. No data is queried at all (so the display is Null).

But it should be noted that being Null does not mean the fastest, it also needs to be determined according to the actual situation (such as the amount of data ps: assuming a table with tens of millions of data, do you think that id=1 is used to query fast or the full index scan takes the smallest Fast value?)

const, system: mysql can optimize a certain part of the query and convert it into a constant (see the result of show warnings).

When comparing all columns used for primary key or unique key with constants, the table has at most one matching row, which is faster to read once.

system is a special case of const. When there is only one tuple matching in the table, it is system.

Explain EXTENDED SELECT * from (select * from film where id = 1) tmp; 

Parsing: Query directly according to the primary key equivalent value. For the SQL parser, only one piece of data in a table meets the requirements. For me, it is a const (constant), and the type of the second piece of data is const (constant) )

The system of the first data, I query data from the query result, but there is only one data in the query result, so to me the type is system (that is why system is a special case of const )

eq_ref: All parts of the primary key or unique key index are used in conjunction, and at most only one eligible record will be returned. This may be the best type of join other than const, and this type will not appear in a simple select query.

EXPLAIN SELECT * from film_actor LEFT JOIN film on film_actor.film_id = film.id;

 

 Understanding: First look at the first piece of data. The type shows all, which means that all data in the film_actor table will be queried. The second piece of data is to associate the id of the film table according to the id in the film_actor (the id of the film is the primary key) ).

ref: Compared with eq_ref, it does not use a unique index, but uses a common index or a partial prefix of a unique index. The index needs to be compared with a certain value, and multiple qualified rows may be found.

  • Simple select query, name is a normal index (non-unique index)
EXPLAIN select * from film where name = 'film1';

 

  • Associated table query, idx_film_actor_id is a joint index of film_id and actor_id, and the left prefix film_id part of film_actor is used here.
Explain SELECT film_id from film LEFT JOIN film_actor on film.id = film_actor.film_id;

 

Understanding: The name attribute in film is just a normal index, not a primary key index, and the index type is ref

range: Range scanning usually occurs in operations such as in(), between ,> ,<, >=. Use an index to retrieve rows in a given range.

EXPLAIN SELECT * from actor where id > 1;

 

Understanding: This is a range search based on the primary key, I also used a normal index to perform a range search, and the final type is all

index: Scan the entire table index, which is usually faster than ALL.

 

Note: index is because all attributes in the film table have corresponding indexes.

ALL: Full table scan, which means that mysql needs to find the required rows from beginning to end. Under normal circumstances, this needs to increase the index to optimize

EXPLAIN SELECT * from actor;

 

 (There are other attributes, which will be added later)

Guess you like

Origin blog.csdn.net/baidu_31572291/article/details/115273114