Mysql Explain type detailed explanation

What is EXPLAIN

MySQL provides an EXPLAIN command, which can analyze SQL statements and output detailed information about SQL execution for developers to optimize.

For example, to analyze a SELECT statement

EXPLAIN SELECT * FROM `user` WHERE id = 1 

Insert image description here

EXPLAIN type field in results

Tips: Common scanning methods

  • system: system table, a small amount of data, often does not require disk IO
  • const: constant connection
  • eq_ref: primary key index (primary key) or non-null unique index (unique not null) equal value scan
  • ref: non-primary key non-unique index equivalent scan
  • range: range scan
  • index: index tree scan ALL: full table scan (full table scan)

type scan mode from fast to slow
system > const > eq_ref > ref > range > index > ALL

1.system

Insert image description here

In the above example, data is queried from the system label proxies_priv of the system library mysql. The data here is already loaded in the memory when the Mysql service is started, and no disk IO is required.
Explanation in the official documentation: This table has only one row (=system table). This is a special case of const join type

2.const

simulated data

create table user (
  id int primary key,
  name varchar(20)
)engine=innodb;

insert into user values(1,'ar414');
insert into user values(2,'zhangsan');
insert into user values(3,'lisi');
insert into user values(4,'wangwu');

Explain analysis results
Insert image description here

In the above example, id is the primary key, and the connection part is the constant 1. It can be found through the index once, which is very fast.

Scenes:

  • Hit primary key or unique index
  • The connected part is a constant value (const)

3.eq_ref

simulated data

create table user (
  id int primary key,
  name varchar(20)
)engine=innodb;

insert into user values(1,'ar414');
insert into user values(2,'zhangsan');
insert into user values(3,'lisi');
insert into user values(4,'wangwu');

create table user_balance (
  uid int primary key,
  balance int
)engine=innodb;

insert into user_balance values(1,100);
insert into user_balance values(2,200);
insert into user_balance values(3,300);
insert into user_balance values(4,400);
insert into user_balance values(5,500);

Explain analysis results
Insert image description here

In the above example, for each row (row) in the user table in the front table, only one row in the user_balance table is scanned. This type of scanning is also very fast.

Scenes:

  • Join query
  • Hit primary key or unique not null index
  • Equijoin

4.ref

simulated data

Difference from eq_ref simulated data: the primary key index in the user_balance table is changed to a normal index

create table user (
  id int primary key,
  name varchar(20)
)engine=innodb;

insert into user values(1,'ar414');
insert into user values(2,'zhangsan');
insert into user values(3,'lisi');
insert into user values(4,'wangwu');

create table user_balance (
  uid int,
  balance int,
  index(uid)
)engine=innodb;

insert into user_balance values(1,100);
insert into user_balance values(2,200);
insert into user_balance values(3,300);
insert into user_balance values(4,400);
insert into user_balance values(5,500);

Explain analysis results

Joint table query

Insert image description here

Since the back table uses ordinary non-unique indexes, for each row (row) in the user table in the front table, more than one row of data in the user_balance table in the back table may be scanned.

Single table query
Insert image description here

When the id is changed to a normal non-unique index, the constant connection query is also downgraded from const to ref. Because of the non-unique index, more than one row of data may be scanned.

Each ref match may return multiple rows of data. Although it is slower than eq_ref, it is still a fast join type.

Scenes:

  • Joint table query
  • Ordinary non-unique index

5.range

simulated data

create table user (
  id int primary key,
  name varchar(20)
)engine=innodb;

insert into user values(1,'ar414');
insert into user values(2,'zhangsan');
insert into user values(3,'lisi');
insert into user values(4,'wangwu');
insert into user values(5,'zhaoliu');

Explain analysis results

between

Insert image description here

in>

Insert image description here

,>=,<,<=

Insert image description here

Range is easier to understand. It is a range query on the index. It scans the index for values ​​within a specific range.

6.index

Voiceover: The current test table is InnoDb, MyISAM has a built-in counter, and it reads directly from the counter during count().

Insert image description here

index type, you need to scan all the data on the index, it is only slightly faster than a full table scan

7.ALL

simulated data

create table user (
  id int,
  name varchar(20)
)engine=innodb;

insert into user values(1,'ar414');
insert into user values(2,'zhangsan');
insert into user values(3,'lisi');
insert into user values(4,'wangwu');
insert into user values(5,'zhaoliu');

Explain the analysis results.
Insert image description here
If no index is built on id, the whole table will be scanned.

Summarize

  • type type from fast to slow: system>const>eq_ref>ref>range>index>ALL
  • As a qualified back-end developer, you should be familiar with Explain
  • Create correct indexes based on business rather than indexing each field (abuse)

Guess you like

Origin blog.csdn.net/gaoxu529/article/details/123342556