MySQL执行update语句和原数据相同会再次执行吗

 更新时间:2019年04月04日 10:10:29   作者:阿里云云栖社区  
这篇文章主要给大家介绍了关于MySQL执行update语句和原数据相同是否会再次执行的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用MySQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

背景

本文主要测试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=rowbinlog_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)“这个操作,该加锁的加锁,该更新的更新。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

相关文章

  • 利用mycat实现mysql数据库读写分离的示例

    利用mycat实现mysql数据库读写分离的示例

    本篇文章主要介绍了利用mycat实现mysql数据库读写分离的示例,mycat是最近很火的一款国人发明的分布式数据库中间件,它是基于阿里的cobar的基础上进行开发的,有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • 在MySQLDump中使用-w语句进行备份的方法

    在MySQLDump中使用-w语句进行备份的方法

    这篇文章主要介绍了在MySQLDump中使用-w语句进行备份的方法,MySQLDump是一款很有人气的MySQL数据转存工具,需要的朋友可以参考下
    2015-05-05
  • 探讨MySQL 保存日期用哪种数据类型

    探讨MySQL 保存日期用哪种数据类型

    在设计数据库表时不可避免的需要用到时间类型,到底选择那种数据类型来表示时间是一个值的讨论的问题,这篇文章主要介绍了MySQL保存日期用哪种数据类型,需要的朋友可以参考下
    2023-08-08
  • mysql中日期的加减 date_add()、date_sub() 函数及用法小结

    mysql中日期的加减 date_add()、date_sub() 函数及用法小结

    在Mysql中,date_add与date_sub分别是指对于日期的一个加减操作,date_add是指从日期中加上指定的时间间隔,date_sub是指从日期中减去指定的时间间隔,本文通过实例讲解mysql中日期的加减 date_add()、date_sub() 函数及用法小结,感兴趣的朋友一起看看吧
    2023-11-11
  • MySQL存储引擎简介及MyISAM和InnoDB的区别

    MySQL存储引擎简介及MyISAM和InnoDB的区别

    这篇文章主要介绍了MySQL存储引擎简介及MyISAM和InnoDB的区别,重点介绍了MyISAM 和 InnoDB的区别,需要的朋友可以参考下
    2014-05-05
  • 解决MySQL因不能创建 PID 导致无法启动的方法

    解决MySQL因不能创建 PID 导致无法启动的方法

    这篇文章主要给大家介绍了关于解决MySQL因不能创建 PID 导致无法启动的方法,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • CentOS 7搭建多实例MySQL8的详细教程(想要几个搞几个)

    CentOS 7搭建多实例MySQL8的详细教程(想要几个搞几个)

    这篇文章主要介绍了CentOS 7搭建多实例MySQL8的详细教程(想要几个搞几个),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • MySQL 分页查询的优化技巧

    MySQL 分页查询的优化技巧

    这篇文章主要介绍了MySQL 分页查询的优化技巧,帮助大家更好的理解和学习使用MySQL,感兴趣的朋友可以了解下
    2021-05-05
  • MySQL转换Oracle的需要注意的七个事项

    MySQL转换Oracle的需要注意的七个事项

    有很多应用项目, 刚起步的时候用MySQL数据库基本上能实现各种功能需求,随着应用用户的增多,数据量的增加,MySQL渐渐地出现不堪重负的情况:连接很慢甚至宕机,于是就有MySQL转换Oracle的需求,应用程序也要相应做一些修改。
    2010-12-12
  • Win10安装mysql8.0.15 winx64及连接服务器过程中遇到的问题

    Win10安装mysql8.0.15 winx64及连接服务器过程中遇到的问题

    这篇文章主要介绍了Win10安装mysql8.0.15 winx64及连接服务器过程中遇到的问题,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12

最新评论