MySQL的EXPLAIN会修改数据测试
文章转载自:https://www.cnblogs.com/kerrycode/p/14138626.html
在博客“Explain命令可能会修改MySQL数据”了解到MySQL中EXPLAIN可能会修改数据,这个现象确实挺让人意外和震惊的,像SQL Server或Oracle数据库,查看执行计划是不会真的执行的SQL语句的,但是MySQL确实有点“古怪”。
下面,我们简单准备一下测试环境数据。
mysql> create table test(id int, name varchar(12));
Query OK, 0 rows affected (0.33 sec)
mysql> insert into test
-> select 1, 'kerry' from dual union all
-> select 2, 'ken' from dual union all
-> select 3, 'jerry' from dual;
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> DELIMITER &&
mysql> create function cleanup() returns char(50) charset utf8mb4
-> DETERMINISTIC
-> begin
-> delete from test;
-> return 'OK';
-> end &&
Query OK, 0 rows affected (0.07 sec)
mysql> DELIMITER ;
接下来,我们测试验证一下
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.18 |
+-----------+
1 row in set (0.00 sec)
mysql> select * from test;
+------+-------+
| id | name |
+------+-------+
| 1 | kerry |
| 2 | ken |
| 3 | jerry |
+------+-------+
3 rows in set (0.00 sec)
mysql> explain select * from (select cleanup()) as t;
+----+-------------+------------+------------+--------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+--------+---------------+------+---------+------+------+----------+----------------+
| 1 | PRIMARY | <derived2> | NULL | system | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
| 2 | DERIVED | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+----+-------------+------------+------------+--------+---------------+------+---------+------+------+----------+----------------+
2 rows in set, 1 warning (0.07 sec)
mysql> select * from test;
Empty set (0.00 sec)
随后翻看官方文档,发现官方文档其实也有简单介绍,只不过一句话带过,不细心的话,还真给忽略了,原文如下:
It is possible in some cases to execute statements that modify data when EXPLAIN SELECT is used with a subquery; for more information, see Section 13.2.11.8, “Derived Tables”.
所以这么说它还不算是一个Bug,也就是说MySQL中使用EXPLAIN查看执行计划时,对应的子查询中调用函数是会执行的。例如,官方文档中还有这么一段描述
This also means that an EXPLAIN SELECT statement such as the one shown here may take a long time to execute because the BENCHMARK() function is executed once for each row in t1:
EXPLAIN SELECT * FROM t1 AS a1, (SELECT BENCHMARK(1000000, MD5(NOW())));
估计这事只能那些高手深入代码研究才能搞清楚这种机制,暂且记录一下这个现象。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2020-04-02 fastdfs-zyc管理FastDFS的web界面
2020-04-02 go-fastdfs和配套使用的web页面
2020-04-02 详细说明-CentOS7部署FastDFS+nginx模块(包含集群方式)
2020-04-02 CentOS7部署FastDFS+nginx模块
2020-04-02 FastDFS 分布式存储图书
2020-04-02 FastDFS配置文件思维导图(内含各配置文件详细参数说明)
2020-04-02 CentOS7部署FastDFS+nginx模块