打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MySQL的EXPLAIN会修改数据测试

在博客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())));

 

估计这事只能那些高手深入代码研究才能搞清楚这种机制,暂且记录一下这个现象。

 

参考资料:

 

https://www.cnblogs.com/abclife/p/14101191.html

https://www.docs4dev.com/docs/zh/mysql/5.7/reference/derived-tables.html

https://www.docs4dev.com/docs/zh/mysql/5.7/reference/explain-output.html

https://dev.mysql.com/doc/refman/8.0/en/explain-output.html

https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
95%的人都不知道 MySQL还有索引管理与执行计划
?MySQL的 select 和 sort 变量
Mysql Explain 详解及Extended选项的使用
MySQL索引失效分析
mysql count(*) count(val) count(1)比较
【转】MySQL 性能优化神器 Explain 使用分析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服