MySQL通过Explain查看select语句的执行计划结果触发写操作
【背景】
某某同学执行了一下Explain结果结果发现数据库有了一条写入操作,恭喜这位同学你的锅到货了,你签收一下;
对! 你没有听错,在一种场景下就算是Explain也会引发数据的写操作,就这是外层查询访问任意表,内层查询调用function
在function有写入动作的情况下会发生写入。
【硬生生的套上一个场景】
假设我们有一个Person表,每访问一次Person表都记录一次在什么时候,访问了哪一行,表结构设计如下
create table if not exists person(
id int not null auto_increment primary key,
name varchar(16)
); create table if not exists person_opration_log(
id int not null auto_increment primary key,
pid int not null,
access_datetime datetime
); delimiter //
create function fun_person_log(pid int) returns int
begin
insert into person_opration_log(pid,access_datetime) values(pid,now());
return pid;
end // delimiter ;
正常的数据访问SQL如下,但是它并不写日志
mysql> select
-> id,
-> name
-> from person
-> where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | 项羽 |
+----+--------+
1 row in set (0.00 sec)
如果我们要写日志可以分两步走,先访问再计一笔日志
mysql> select
-> id,
-> name
-> from person
-> where id = 1;
+----+--------+
| id | name |
+----+--------+
| 1 | 项羽 |
+----+--------+
1 row in set (0.00 sec) mysql>
mysql> select fun_person_log(1);
+-------------------+
| fun_person_log(1) |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.05 sec) mysql>
mysql> select * from person_opration_log ;
+----+-----+---------------------+
| id | pid | access_datetime |
+----+-----+---------------------+
| 1 | 1 | 2018-10-06 17:12:31 |
+----+-----+---------------------+
1 row in set (0.00 sec)
【牛人想出的新点子把两步合成一步】
牛人的新点子
mysql> select
-> fun_person_log(100) as id ,
-> name
-> from person
-> where id = (select fun_person_log(100));
Empty set (0.04 sec) mysql>
mysql> select * from person_opration_log;
+----+-----+---------------------+
| id | pid | access_datetime |
+----+-----+---------------------+
| 1 | 1 | 2018-10-06 17:12:31 |
| 2 | 100 | 2018-10-06 17:15:29 |
+----+-----+--------------------
牛人的新点子刚好入坑,我们可以explain一下
mysql> explain select
-> fun_person_log(250) as id ,
-> name
-> from person
-> where id = (select fun_person_log(250));
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
| 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | no matching row in const table |
| 2 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+
2 rows in set, 1 warning (0.08 sec) mysql>
mysql> select * from person_opration_log;
+----+-----+---------------------+
| id | pid | access_datetime |
+----+-----+---------------------+
| 1 | 1 | 2018-10-06 17:12:31 |
| 2 | 100 | 2018-10-06 17:15:29 |
| 3 | 250 | 2018-10-06 17:17:23 |
+----+-----+---------------------+
3 rows in set (0.00 sec)
看吧! explain引发了写入操作!
【参考连接】
【学习交流】
-----------------------------http://www.sqlpy.com-------------------------------------------------


-----------------------------http://www.sqlpy.com-------------------------------------------------
MySQL通过Explain查看select语句的执行计划结果触发写操作的更多相关文章
- oracle中查看sql语句的执行计划
1.在pl/sql中打开cmd命令容器 2.在cmd命令窗口中输入:explain plan for select * from t; 3.查看sql语句的执行计划:select * from tab ...
- mysql中explain查看sql语句索引使用情况
explain + sql: mysql> explain select * from user; +----+-------------+-------+------+------------ ...
- 【Oracle】三种方式查看SQL语句的执行计划
查看执行计划的方式有三种: EXPLAIN PLAN .V$SQL_PLAN .SQL*PLUS AUTOTRACE 1.EXPLAIN PLAN: 显示执行相应语句时可以使用的理论计划 读取执行计划 ...
- mysql优化:explain分析sql语句执行效率
Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...
- mysql优化–explain分析sql语句执行效率
Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...
- MySQL学习----explain查看一条sql 的性能
在开发的过程中,对于我们写的sql语句,我们有时候会考虑sql语句的性能,那么explain就是首选.Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决 ...
- 容易被忽略的事----sql语句中select语句的执行顺序
关于Sql中Select语句的执行顺序,一直很少注意这个问题,对于关键字的使用也很随意,至于效率问题,因为表中的数据量都不是很大,所以也不是很在意. 今天在一次面试的时候自己见到了,感觉没一点的印象, ...
- mysql怎么限制某些查询语句的执行?
mysql怎么限制某些查询语句的执行? 比如某些sql语句执行时间很长,超过10s,怎么样超过10s就不让其执行? 后续更新中...
- select 语句的执行顺序
select 语句的执行顺序 借用ItZik Ben-Gan.Lubor Kollar.Dejan Sarka所著的<Sql Server 2005 技术内幕:T-SQL查询>的一段话足以 ...
随机推荐
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- NameNode重新格式化以后DataNode不能启动
最近重新格式化NameNode以后,发现几个DataNode都不能启动了. 这是因为dfs.name.dir路径下面有一个current/VERSION文件,里面有一个clusterID,重新格式化以 ...
- Semaphore的使用
Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数,并提供了同步机制.使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数. Semapho ...
- 转-ubuntu清理卸载wine的残余项目
背景:前段时间,装了wine试用了一下,感觉实在没啥意思就卸载了.但是卸载以后发现还有些尾巴碍眼,如打开文件时右键菜单里就会有“使用notepad打开”的选项,虽然没有什么别的问题,但是看着碍眼.所以 ...
- Webwork【01】Webwork与 Struct 的前世今生
Struts 1是全世界第一个发布的MVC框架,它由Craig McClanahan在2001年发布,该框架一经推出,就得到了世界上Java Web开发者的拥护,经过长达6年时间的锤炼,Struts ...
- java 日期Date类型比较大小
java 日期Date类型比较大小 CreateTime--2018年5月31日16点39分 Author:Marydon import java.text.DateFormat; import ...
- weblogic服务目录迁移记录
weblogic服务,由于前期的规划不好,导致后期有点问题!为了更加规范运行服务及执行相关操作,故进行服务迁移... 先决条件:weblogic都是单个aminserver运行的,单个服务 问题解决: ...
- cnpm不是内部或外部命令 cnpm: command not found
问题是处在于 你没用用淘宝的镜像 安装cnpm 不信 你打下cnpm -v, 看是 是不是也不是内部命令: 好了,那就安装下吧 npm install cnpm -g --registry=htt ...
- CentOS6.6安装S******Sockets服务端
1.查看系统 [root@localhost ~]# cat /etc/issue CentOS release 6.6 (Final) [root@localhost ~]# uname -a Li ...
- ios中自定义button
自定义button #import <UIKit/UIKit.h> #define KFont 15 @interface DIYButton : UIButton @property(n ...