分析MySQL中update修改数据与原数据相同是否会再次执行
本篇文章给大家带来的内容是关于分析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)“这个操作,该加锁的加锁,该更新的更新。
以上是分析MySQL中update修改数据与原数据相同是否会再次执行的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

无法以 root 身份登录 MySQL 的原因主要在于权限问题、配置文件错误、密码不符、socket 文件问题或防火墙拦截。解决方法包括:检查配置文件中 bind-address 参数是否正确配置。查看 root 用户权限是否被修改或删除,并进行重置。验证密码是否准确无误,包括大小写和特殊字符。检查 socket 文件权限设置和路径。检查防火墙是否阻止了 MySQL 服务器的连接。

MySQL修改表结构时,通常使用元数据锁,可能导致锁表。为了减少锁的影响,可采取以下措施:1. 使用在线DDL保持表可用;2. 分批执行复杂修改;3. 在小表或非高峰期操作;4. 使用PT-OSC工具实现更精细的控制。

MySQL 数据库中,用户和数据库的关系通过权限和表定义。用户拥有用户名和密码,用于访问数据库。权限通过 GRANT 命令授予,而表由 CREATE TABLE 命令创建。要建立用户和数据库之间的关系,需创建数据库、创建用户,然后授予权限。

数据集成简化:AmazonRDSMySQL与Redshift的零ETL集成高效的数据集成是数据驱动型组织的核心。传统的ETL(提取、转换、加载)流程复杂且耗时,尤其是在将数据库(例如AmazonRDSMySQL)与数据仓库(例如Redshift)集成时。然而,AWS提供的零ETL集成方案彻底改变了这一现状,为从RDSMySQL到Redshift的数据迁移提供了简化、近乎实时的解决方案。本文将深入探讨RDSMySQL零ETL与Redshift集成,阐述其工作原理以及为数据工程师和开发者带来的优势。

1.使用正确的索引索引通过减少扫描的数据量来加速数据检索select*fromemployeeswherelast_name='smith';如果多次查询表的某一列,则为该列创建索引如果您或您的应用根据条件需要来自多个列的数据,则创建复合索引2.避免选择*仅选择那些需要的列,如果您选择所有不需要的列,这只会消耗更多的服务器内存并导致服务器在高负载或频率时间下变慢例如,您的表包含诸如created_at和updated_at以及时间戳之类的列,然后避免选择*,因为它们在正常情况下不需要低效查询se

MySQL无法直接在Android上运行,但可以通过以下方法间接实现:使用轻量级数据库SQLite,由Android系统自带,无需单独服务器,资源占用小,非常适合移动设备应用。远程连接MySQL服务器,通过网络连接到远程服务器上的MySQL数据库进行数据读写,但存在网络依赖性强、安全性问题和服务器成本等缺点。

MySQL 有免费的社区版和收费的企业版。社区版可免费使用和修改,但支持有限,适合稳定性要求不高、技术能力强的应用。企业版提供全面商业支持,适合需要稳定可靠、高性能数据库且愿意为支持买单的应用。选择版本时考虑的因素包括应用关键性、预算和技术技能。没有完美的选项,只有最合适的方案,需根据具体情况谨慎选择。

MySQL数据库性能优化指南在资源密集型应用中,MySQL数据库扮演着至关重要的角色,负责管理海量事务。然而,随着应用规模的扩大,数据库性能瓶颈往往成为制约因素。本文将探讨一系列行之有效的MySQL性能优化策略,确保您的应用在高负载下依然保持高效响应。我们将结合实际案例,深入讲解索引、查询优化、数据库设计以及缓存等关键技术。1.数据库架构设计优化合理的数据库架构是MySQL性能优化的基石。以下是一些核心原则:选择合适的数据类型选择最小的、符合需求的数据类型,既能节省存储空间,又能提升数据处理速度
