背景

  某某同学执行了一下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. JDK5.0 特性-线程 Condition

    来自:http://www.cnblogs.com/taven/archive/2011/12/17/2291471.html import java.util.concurrent.Executor ...

  2. Container [pid=6263,containerID=container_1494900155967_0001_02_000001] is running beyond virtual memory limits

    以Spark-Client模式运行,Spark-Submit时出现了下面的错误: User: hadoop Name: Spark Pi Application Type: SPARK Applica ...

  3. (素材源代码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象牛逼Demo

    猫猫分享,必须精品 原创文章,欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:代码 #import "ViewCon ...

  4. Redis问题MISCONF Redis is configured to save RDB snapshots....

    Redis问题MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on di ...

  5. MongoDB numa系列问题三:overcommit_memory和zone_reclaim_mode

    内核参数overcommit_memory : 它是 内存分配策略 可选值:0.1.2.0:表示内核将检查是否有足够的可用内存供应用进程使用:如果有足够的可用内存,内存申请允许:否则,内存申请失败,并 ...

  6. Cocos2d-js 3.0 颜色变换(调整sprite/图片的色调)

    Flash在滤镜方面做得比较成熟,starling也有很多现成的办法. 但Cocos2D这里就显得比较单薄,百度/谷歌很少相关资料. 后续如果有时间,再慢慢整理各种滤镜效果.     这里先介绍一下颜 ...

  7. Double-check idiom for lazy initialization of instance fields

  8. 【DB2】数据迁移

    数据迁移概述 在日常生活中常有数据的导入导出,为此db2提出了很多工具可以选择,export.import.load.db2look.db2move.db2dart,如下图所示: a.最上面虚线框部分 ...

  9. python之模块pydoc

    # -*- coding: cp936 -*- #python 27 #xiaodeng import pydoc #主要用于从python模块中自动生成文档,这些文档可以基于文本呈现,也可以生成we ...

  10. 转 解决configure: error: Please reinstall the libcurl distribution

    今天配置一台server的php支持curl的时候, 出现如下报错 checking for cURL in default path... not foundconfigure: error: Pl ...