mysql explain the usage of the output column id Detailed

Reference mysql5.7 en manual, explanation of the columns id:

The SELECT identifier. This is the sequential number of the SELECT within the query. The value can be NULL if the row refers to the union result of other rows. In this case, the table column shows a value like <unionM,N> to indicate that the row refers to the union of the rows with id values of M and N.

Translation: SELECT identifier, which is a query SELECT sequence number, and if this line relate the results of other joint line, this value may be NULL. In this case, table column explain output columns will show a value as <unionM, N> to indicate that the row relates to combination with the id value M and N lines.

Examples
# 快速创建三个表tb1, tb2, tb3
# 创建tb1
mysql> create table tb1(
    -> id int unsigned not null primary key auto_increment comment '主键,自增',
    -> name varchar(30) not null default '' comment '姓名'
    -> ) engine=innodb auto_increment=1 default charset=utf8 collate=utf8_general_ci;
Query OK, 0 rows affected (0.01 sec)

# 创建tb2
mysql> create table tb2 like tb1;
Query OK, 0 rows affected (0.02 sec)

# 创建tb3
mysql> create table tb3 like tb1;
Query OK, 0 rows affected (0.02 sec)

First, the same id, performed in order from top to bottom table column

# id都是1,值相同,执行顺序是从上至下依次是tb1, tb2, tb3
mysql> explain select tb2.* from tb1, tb2, tb3 where tb1.id=tb2.id and tb1.id=tb3.id and tb1.name='jerry';

Second, different id, if the query is a child, id serial number is incremented, id value the greater the higher the priority, the first to be executed

# 分别向三个表中插入记录
mysql> insert into tb1 values(null, 'tom');
Query OK, 1 row affected (0.00 sec)

mysql> insert into tb2 select * from tb1;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into tb3 select * from tb1;
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from tb1, tb2, tb3;
+----+------+----+------+----+------+
| id | name | id | name | id | name |
+----+------+----+------+----+------+
|  1 | tom  |  1 | tom  |  1 | tom  |
+----+------+----+------+----+------+
1 row in set (0.00 sec)

# id不相同并且子查询的id是递增的,此时table列的执行顺序是tb3, tb1, tb2
# tb3的id是3优先被执行,其次是tb1, tb2
mysql> explain select tb2.* from tb2 where id=(select id from tb1 where id=(select tb3.id from tb3 where tb3.name='tom'));

Three, id same different, exist

# id相同都是1,顺序执行依次是tb3, tb2
mysql> explain select tb2.* from (select tb3.id from tb3 where tb3.name='') as n1, tb2 where n1.id=tb2.id;

In the above example simulate the situation is not the same id different mixing can look below screenshot

As shown above, if the same id, may be considered as a group, (in this group) down from the execution order; in all groups, the greater the id value, the higher priority, the first run.

Fourth, the case for the use of union

# id有相同也有不同,相同id为一组,所以执行顺序为tb1, tb2, tb3
mysql> explain select tb3.* from tb3 union select tb2.* from (select tb1.* from tb1) as n1, tb2;

to sum up

Note: The above example shows the result of the test resulting in centos7 and mysql5.7.26, mysql version or other environmental results may vary.

欢迎访问我的个人站点:瑾年笔记

Guess you like

Origin www.cnblogs.com/goujian/p/12015886.html