【背景】

  某某同学执行了一下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引发了写入操作!

【参考连接】

Derived Tables

【学习交流】

-----------------------------http://www.sqlpy.com-------------------------------------------------

-----------------------------http://www.sqlpy.com-------------------------------------------------

  

MySQL通过Explain查看select语句的执行计划结果触发写操作的更多相关文章

  1. oracle中查看sql语句的执行计划

    1.在pl/sql中打开cmd命令容器 2.在cmd命令窗口中输入:explain plan for select * from t; 3.查看sql语句的执行计划:select * from tab ...

  2. mysql中explain查看sql语句索引使用情况

    explain + sql: mysql> explain select * from user; +----+-------------+-------+------+------------ ...

  3. 【Oracle】三种方式查看SQL语句的执行计划

    查看执行计划的方式有三种: EXPLAIN PLAN .V$SQL_PLAN .SQL*PLUS AUTOTRACE 1.EXPLAIN PLAN: 显示执行相应语句时可以使用的理论计划 读取执行计划 ...

  4. mysql优化:explain分析sql语句执行效率

    Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...

  5. mysql优化–explain分析sql语句执行效率

    Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决,Explain可以用来查看SQL语句的执行效 果,可以帮助选择更好的索引和优化查询语句,写出更好的优 ...

  6. MySQL学习----explain查看一条sql 的性能

    在开发的过程中,对于我们写的sql语句,我们有时候会考虑sql语句的性能,那么explain就是首选.Explain命令在解决数据库性能上是第一推荐使用命令,大部分的性能问题可以通过此命令来简单的解决 ...

  7. 容易被忽略的事----sql语句中select语句的执行顺序

    关于Sql中Select语句的执行顺序,一直很少注意这个问题,对于关键字的使用也很随意,至于效率问题,因为表中的数据量都不是很大,所以也不是很在意. 今天在一次面试的时候自己见到了,感觉没一点的印象, ...

  8. mysql怎么限制某些查询语句的执行?

    mysql怎么限制某些查询语句的执行? 比如某些sql语句执行时间很长,超过10s,怎么样超过10s就不让其执行? 后续更新中...

  9. select 语句的执行顺序

    select 语句的执行顺序 借用ItZik Ben-Gan.Lubor Kollar.Dejan Sarka所著的<Sql Server 2005 技术内幕:T-SQL查询>的一段话足以 ...

随机推荐

  1. WIP 003 - Create page with a tablewalker

    Need ability to delete records by click the trash can Need ability to add new records to database an ...

  2. 写一个function,清除字符串前后的空格。(兼容所有浏览器)

    代码: function trim(str) { if (str && typeof str === "string") { return str.replace( ...

  3. JAVA & Android 等待线程池内任务全部完成后退出

    void shutdownAndAwaitTermination(ExecutorService pool) { pool.shutdown(); // Disable new tasks from ...

  4. Flash:移除匿名函数监听器EventListener

    private function handleCreationComplete():void { sampleButton.addEventListener(MouseEvent.CLICK, cre ...

  5. jenkins相关默认路径

    安装完成后,有如下相关目录:(1)/usr/lib/jenkins/:jenkins安装目录,WAR包会放在这里.( 2 ) /etc/sysconfig/jenkins:jenkins配置文件,“端 ...

  6. rac安装_grid安装校验报错之grid未建立信任关系

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  7. ubuntu安装过程中遇到问题小结

    一.下载 官网下载地址:https://www.ubuntu.com/download/desktop/contribute?version=16.04.4&architecture=amd6 ...

  8. python配置文件转dict

    配置文件有很多种,如JSON,properties,conf,xml等. 除非需要跟别的语言进行交互,python本身是完全可以取代所有配置文件的.使用python进行配置可以使用非常灵活地执行一些逻 ...

  9. 支持向量机-SVM 学习

    一 .支持向量机(SVM) 1.1 符号定义 标签 y 不再取 0 或 1,而是: y∈{-1, 1} 定义函数: 向量,没有第 0 个维度,b 为截距,预测函数定义为: 1.2 函数间隔与几何间隔 ...

  10. 线程同步之mutex和Semaphore

    表示之前对semaphore信号量木有神码概念. 比较纳闷这玩意要干嘛,好吧继续stackflow: Mutex can be released only by thread that had acq ...