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查询>的一段话足以 ...
随机推荐
- h5可伸缩布局方案
https://github.com/amfe/lib-flexible ib.flexible 移动端自适应方案,相关文章请参考此处 Update[2016年01月13日] 首先,由衷的感谢@完颜( ...
- Android使用OKHttp库实现视频文件的上传到服务器
目录 1 服务器接口简介 2 Android端代码实现 2.1 xml布局文件 2.2 Activity类 2.3 Okhttp网络通信类 1 服务器接口简介 此处我使用的服务器接口是使用Flask编 ...
- 用 bottle.py 写了个简单的升级包上传
可以当作一个 demo 来玩吧,在这里分享一下.里面涉及的内容包含了文件上传,cookie 设置和读取,重定向(redirect). from bottle import run, post, get ...
- qt坐标系统与布局的简单入门
qt坐标系统 qt坐标系统比較简单 ); 上面的代码把button显示为父窗体的20,20处宽度为100,高度为100 接下去是布局 qt里面布局须要增加<QLayout.h>这个头 ...
- RSA密钥生成、加密解密、签名验签
RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...
- MYSQL数据库注释
//修改注释 alter table user comment = '我要修改注释'; //新建表设定表注释及解释说明. create table AuthUser( ID ) primary key ...
- java new关键字
//new关键字://1.表示创建一个对象//2.表示实例化对象//3.表示申请内存空间 在python中其实就是一个实例化的过程
- TCP/IP协议——TCP/IP协议栈及框架
TCP/IP协议同ISO/OSI模型一样,也可以安排成栈形式.但这个栈不同于ISO/OSI版本,比ISO/OSI栈少,所以又称之为短栈.另外,需要知道的是:TCP/IP协议栈只是许多支持ISO/OSI ...
- jeecg平台testDatagrid
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 基于Android的小巫新闻客户端开发系列教程
<ignore_js_op> 141224c6n6x7wmu1aacap7.jpg (27.51 KB, 下载次数: 0) 下载附件 保存到相册 23 秒前 上传 <ignor ...