MySQL执行update语句和原数据相同会再次执行吗
作者:阿里云云栖社区 发布时间:2024-01-20 16:49:31
标签:mysql,update,原数据
背景
本文主要测试MySQL执行update语句时,针对与原数据(即未修改)相同的update语句会在MySQL内部重新执行吗?
测试环境
MySQL5.7.25
Centos 7.4
binlog_format为ROW
参数
root@localhost : (none) 04:53:15> show variables like 'binlog_row_image';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| binlog_row_image | FULL |
+------------------+-------+
1 row in set (0.00 sec)
root@localhost : (none) 04:53:49> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec)
root@localhost : test 05:15:14> show variables like 'transaction_isolation';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.00 sec)
测试步骤
session1
root@localhost : test 04:49:48> begin;
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 04:49:52> select * from test where id =1;
+----+------+------+------+
| id | sid | mid | name |
+----+------+------+------+
| 1 | 999 | 871 | NW |
+----+------+------+------+
1 row in set (0.00 sec)
root@localhost : (none) 04:54:03> show engine innodb status\Gshow master status\G
...
---
LOG
---
Log sequence number 12090390
Log flushed up to 12090390
Pages flushed up to 12090390
Last checkpoint at 12090381
0 pending log flushes, 0 pending chkp writes
33 log i/o's done, 0.00 log i/o's/second
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
session2
root@localhost : test 04:47:45> update test set sid=55 where id =1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
root@localhost : (none) 04:54:03> show engine innodb status\Gshow master status\G
...
---
LOG
---
Log sequence number 12091486
Log flushed up to 12091486
Pages flushed up to 12091486
Last checkpoint at 12091477
0 pending log flushes, 0 pending chkp writes
39 log i/o's done, 0.00 log i/o's/second
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 500
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
1 row in set (0.00 sec)
session1
root@localhost : test 04:49:57> update test set sid=55 where id =1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
root@localhost : (none) 04:54:03> show engine innodb status\Gshow master status\G
...
---
LOG
---
Log sequence number 12091486
Log flushed up to 12091486
Pages flushed up to 12091486
Last checkpoint at 12091477
0 pending log flushes, 0 pending chkp writes
39 log i/o's done, 0.00 log i/o's/second
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 500
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
1 row in set (0.00 sec)
root@localhost : test 04:52:05> select * from test where id =1;
+----+------+------+------+
| id | sid | mid | name |
+----+------+------+------+
| 1 | 999 | 871 | NW |
+----+------+------+------+
1 row in set (0.00 sec)
root@localhost : test 04:52:42> commit;
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 04:52:52> select * from test where id =1;
+----+------+------+------+
| id | sid | mid | name |
+----+------+------+------+
| 1 | 55 | 871 | NW |
+----+------+------+------+
1 row in set (0.00 sec)
总结
在binlog_format=row
和binlog_row_image=FULL
时,由于MySQL 需要在 binlog 里面记录所有的字段,所以在读数据的时候就会把所有数据都读出来,那么重复数据的update不会执行。即MySQL 调用了 InnoDB 引擎提供的“修改为 (1,55)”这个接口,但是引擎发现值与原来相同,不更新,直接返回
binlog_format为STATEMENT
参数
root@localhost : (none) 04:53:15> show variables like 'binlog_row_image';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| binlog_row_image | FULL |
+------------------+-------+
1 row in set (0.00 sec)
root@localhost : (none) 05:16:08> show variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
root@localhost : test 05:15:14> show variables like 'transaction_isolation';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.00 sec)
测试步骤
session1
root@localhost : test 05:16:42> begin;
Query OK, 0 rows affected (0.00 sec)
root@localhost : test 05:16:44> select * from test where id =1;
+----+------+------+------+
| id | sid | mid | name |
+----+------+------+------+
| 1 | 111 | 871 | NW |
+----+------+------+------+
1 row in set (0.00 sec)
root@localhost : (none) 05:16:51> show engine innodb status\Gshow master status\G
...
---
LOG
---
Log sequence number 12092582
Log flushed up to 12092582
Pages flushed up to 12092582
Last checkpoint at 12092573
0 pending log flushes, 0 pending chkp writes
45 log i/o's done, 0.00 log i/o's/second
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
session2
root@localhost : test 05:18:30> update test set sid=999 where id =1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
root@localhost : (none) 05:18:47> show engine innodb status\Gshow master status\G
...
---
LOG
---
Log sequence number 12093678
Log flushed up to 12093678
Pages flushed up to 12093678
Last checkpoint at 12093669
0 pending log flushes, 0 pending chkp writes
51 log i/o's done, 0.14 log i/o's/second
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 438
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
1 row in set (0.00 sec)
session1
root@localhost : test 05:16:47> update test set sid=999 where id =1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
root@localhost : (none) 05:20:03> show engine innodb status\Gshow master status\G
...
---
LOG
---
Log sequence number 12094504
Log flushed up to 12094504
Pages flushed up to 12094504
Last checkpoint at 12094495
0 pending log flushes, 0 pending chkp writes
56 log i/o's done, 0.00 log i/o's/second
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 438
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
1 row in set (0.00 sec)
root@localhost : test 05:19:33> select * from test where id =1;
+----+------+------+------+
| id | sid | mid | name |
+----+------+------+------+
| 1 | 999 | 871 | NW |
+----+------+------+------+
1 row in set (0.00 sec)
root@localhost : test 05:20:44> commit;
Query OK, 0 rows affected (0.01 sec)
root@localhost : test 05:20:57> select * from test where id =1;
+----+------+------+------+
| id | sid | mid | name |
+----+------+------+------+
| 1 | 999 | 871 | NW |
+----+------+------+------+
1 row in set (0.00 sec)
总结
在binlog_format=statement和binlog_row_image=FULL时,InnoDB内部认真执行了update语句,即“把这个值修改成 (1,999)“这个操作,该加锁的加锁,该更新的更新。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
来源:https://zhuanlan.zhihu.com/p/59717198


猜你喜欢
- 一:编译器 编译器是一种特殊的程序,它可以把以特定编程语言写成的程序变为机器可以运行的机器码。我们把一个程序写好,这时我们利用的环境是文本编
- 为什么要模拟登录有些网站是需要登录之后才能访问的,即便是同一个网站,在用户登录前后页面所展示的内容也可能会大不相同,例如,未登录时访问Git
- 面是我下载页面down.php 的php代码 现在我发现,用迅雷,谷歌浏览器直接打开,就能输出下载文件,一点不起防盗链作用。&nb
- 实现功能:删除当前目录下,除保留目录和文件外的所有文件和目录#!bin/env pythonimport osimport os.pathi
- 前言应用在Django的项目中是一个独立的业务模块,可以包含自己的路由,视图,模板,模型.一 创建应用程序创建步骤用manage.py中的子
- 这篇博客是自己《数据挖掘与分析》课程讲到正则表达式爬虫的相关内容,主要简单介绍Python正则表达式爬虫,同时讲述常见的正则表达式分析方法,
- 下面的代码主要用于使用python语言调用NASA官方的MODIS处理工具HEG进行投影坐标转换与重采样批量处理主要参考HEG的用户手册:h
- python修改FTP服务器上的文件名,具体代码如下所示:#-*- coding:utf-8 -*-#修改ftp服务器上的文件名from f
- 简单说明这个算法主要工作是测量不同特征值之间的距离,有个这个距离,就可以进行分类了。简称kNN。已知:训练集,以及每个训练集的标签。接下来:
- 高层的期望“3个月内,我希望网站能增加X注册用户,每日的独立IP到Y,网站盈利达到Z……”作为一个团队的领袖或者产品负责人,这样的期望是根据
- 每天急匆匆赶地铁上班的时候总会一不小心就会忘记打卡,尤其是软件打卡,那有没有什么办法可以解决忘打卡的问题呢?今天给大家推荐一下一款神器,利用
- 1. 介绍上传的图片文件:如pic = request.FILES["picture"]# pic是 <class
- 爬一个网页时,要保存的数据都没有encode,就导致保存下来的中文都变成unicode了。。。那么,怎么把一个表示字符串的unicode还原
- 我是windows下安装的Anaconda2,对应的python版本是python2.7。为了方便,又借助conda安装了python3.6
- 由于公司经常需要异地办公,在调试的时候需要用到内网环境,因此手动写了个代理转发服务器給兄弟们用:socks5proxy。选型上,语言上就选择
- ERROR 1819 (HY000): Your password does not satisfy the current policy
- 题目描述输入一行或多行字符串密码,验证每行密码是否符合规范,符合提示“OK”,否则“NG”。密码规范为:1.长度超过8位2.包括大小写字母.
- 引言上一篇文章中引入了消息队列对秒杀流量做削峰的处理,我们使用的是Kafka,看起来似乎工作的不错,但其实还是有很多隐患存在,如果这些隐患不
- 今天改插件BoxScroll的时候,因为if里面的条件判断多于两个,于是立马想着改写switch。改到一半,忽然记起来JSHint等代码质量
- 这里我不想采用诸如ubuntu下的apt-get install方式进行python的安装,而是在linux下采用源码包的方式进行pytho