mysql 执行计划 extra 详解

前言

当前mysql版本8.0.23
测试数据如下


CREATE TABLE `log` (
  `id` int NOT NULL AUTO_INCREMENT,
  `user_name` varchar(200) DEFAULT NULL,
  `number` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_user_name` (`user_name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `log` (`id`, `user_name`, `number`) VALUES (1,'xie','1001');
INSERT INTO `log` (`id`, `user_name`, `number`) VALUES (2,'rui','1002');

CREATE TABLE `user` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `name` varchar(20) DEFAULT 'xie' COMMENT '名称',
  `age` int unsigned DEFAULT '18' COMMENT '年龄',
  `number` varchar(200) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `index_age` (`age`),
  KEY `idx_user_deleted_at` (`deleted_at`),
  KEY `index_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

INSERT INTO `user` (`id`, `created_at`, `updated_at`, `deleted_at`, `name`, `age`, `number`) VALUES (1,'2022-09-05 19:27:09.468','2022-09-05 19:27:09.468',NULL,'xie',27,'1001'),(2,'2022-09-05 19:27:09.468','2022-09-05 19:27:09.468',NULL,'rui',28,'1002'),(3,'2022-09-05 19:27:09.468','2022-09-05 19:27:09.468',NULL,'rui',29,'1001'),(4,'2022-09-12 13:41:05.000','2022-09-12 13:41:08.000',NULL,'m',22,'1003'),(5,'2022-09-12 13:41:17.000','2022-09-12 13:41:20.000',NULL,'g',21,'1002'),(6,'2022-09-12 13:41:30.000','2022-09-12 13:41:32.000',NULL,'l',20,'1003');

using where

name上是有索引的,但是由于查询的不是字符串,导致索引不能用,使用全表扫描 + where 条件过滤数据

mysql> explain select * from user where name = 3;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | user  | NULL       | ALL  | index_name    | NULL | NULL    | NULL |    6 |    16.67 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 3 warnings (0.00 sec)
using index

覆盖索引,避免回表

mysql> explain select id  from user where name = "xie";
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key        | key_len | ref   | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | user  | NULL       | ref  | index_name    | index_name | 63      | const |    1 |   100.00 | Using index |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
using filesort

无法使用索引排序,只能使用排序算法进行排序,会产生额外的消耗

mysql> explain select * from user order by name desc;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
|  1 | SIMPLE      | user  | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
1 row in set, 1 warning (0.00 sec)
no matching row in const table

在唯一性索引上无法匹配到数据

mysql> explain select * from user where age = 33;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                          |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
|  1 | SIMPLE      | NULL  | NULL       | NULL | NULL          | NULL | NULL    | NULL | NULL |     NULL | no matching row in const table |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
1 row in set, 1 warning (0.00 sec)
Using temporary

使用了临时表


mysql> explain select number,count(*) from user group by number;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
|  1 | SIMPLE      | user  | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | Using temporary |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
1 row in set, 1 warning (0.00 sec)

Using join buffer

由于 user.number 和 log.number 上没有索引,故使用了连接缓存

mysql> explain select * from user left join log on user.number = log.number;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                                      |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
|  1 | SIMPLE      | user  | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |   100.00 | NULL                                       |
|  1 | SIMPLE      | log   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    1 |   100.00 | Using where; Using join buffer (hash join) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL执行计划是SQL在数据库中执行情况的客观反映,也是SQL性能分析和优化的参考。通过执行计划,可以了解SQL的执行路径、使用的索引、访问的字段和表的顺序以及执行耗时等信息。MySQL中的执行计划可以通过一套工具来查看,这些工具可以帮助开发人员优化查询性能。 MySQL执行计划内容包括了SQL的执行路径、使用的索引、访问的字段和表的顺序以及执行耗时等信息。这些内容适用于分布式数据库中以MySQL为原生的数据节点上的执行计划分析。 执行计划MySQL提供的一套工具,它可以显示SQL在数据库中的执行情况,包括使用了哪些索引,查找了哪些字段和表,它们的顺序是怎样的,以及分别用了多长时间等信息。通过查看执行计划,开发人员可以更好地理解SQL的执行过程,从而对查询进行优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [数据库系列之MySQL中的执行计划](https://blog.csdn.net/solihawk/article/details/120756584)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [详解mysql执行计划](https://blog.csdn.net/qq_43418737/article/details/121963709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值