ToB企服应用市场:ToB评测及商务社交产业平台

标题: 面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不 [打印本页]

作者: 冬雨财经    时间: 2022-9-16 17:14
标题: 面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不
来源:https://developer.aliyun.com/article/694162
背景

本文主要测试MySQL执行update语句时,针对与原数据(即未修改)相同的update语句会在MySQL内部重新执行吗?
测试环境

binlog_format为ROW

参数
  1. root@localhost : (none) 04:53:15> show variables like 'binlog_row_image';
  2. +------------------+-------+
  3. | Variable_name    | Value |
  4. +------------------+-------+
  5. | binlog_row_image | FULL  |
  6. +------------------+-------+
  7. 1 row in set (0.00 sec)
  8. root@localhost : (none) 04:53:49> show variables like 'binlog_format';
  9. +---------------+-------+
  10. | Variable_name | Value |
  11. +---------------+-------+
  12. | binlog_format | ROW   |
  13. +---------------+-------+
  14. 1 row in set (0.00 sec)
  15. root@localhost : test 05:15:14> show variables like 'transaction_isolation';
  16. +-----------------------+-----------------+
  17. | Variable_name         | Value           |
  18. +-----------------------+-----------------+
  19. | transaction_isolation | REPEATABLE-READ |
  20. +-----------------------+-----------------+
  21. 1 row in set (0.00 sec)
复制代码
测试步骤

session1
  1. root@localhost : test 04:49:48> begin;
  2. Query OK, 0 rows affected (0.00 sec)
  3. root@localhost : test 04:49:52> select * from test where id =1;
  4. +----+------+------+------+
  5. | id | sid  | mid  | name |
  6. +----+------+------+------+
  7. |  1 |  999 |  871 | NW   |
  8. +----+------+------+------+
  9. 1 row in set (0.00 sec)
  10. root@localhost : (none) 04:54:03> show engine innodb status\Gshow master status\G
  11. ...
  12. ---
  13. LOG
  14. ---
  15. Log sequence number 12090390
  16. Log flushed up to   12090390
  17. Pages flushed up to 12090390
  18. Last checkpoint at  12090381
  19. 0 pending log flushes, 0 pending chkp writes
  20. 33 log i/o's done, 0.00 log i/o's/second
  21. *************************** 1. row ***************************
  22.              File: mysql-bin.000001
  23.          Position: 154
  24.      Binlog_Do_DB:
  25. Binlog_Ignore_DB:
  26. Executed_Gtid_Set:
  27. 1 row in set (0.00 sec)
复制代码
session2
  1. root@localhost : test 04:47:45> update test set sid=55 where id =1;
  2. Query OK, 1 row affected (0.01 sec)
  3. Rows matched: 1  Changed: 1  Warnings: 0
  4. root@localhost : (none) 04:54:03> show engine innodb status\Gshow master status\G
  5. ...
  6. ---
  7. LOG
  8. ---
  9. Log sequence number 12091486
  10. Log flushed up to   12091486
  11. Pages flushed up to 12091486
  12. Last checkpoint at  12091477
  13. 0 pending log flushes, 0 pending chkp writes
  14. 39 log i/o's done, 0.00 log i/o's/second
  15. *************************** 1. row ***************************
  16.              File: mysql-bin.000001
  17.          Position: 500
  18.      Binlog_Do_DB:
  19. Binlog_Ignore_DB:
  20. Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
  21. 1 row in set (0.00 sec)
复制代码
session1
  1. root@localhost : test 04:49:57> update test set sid=55 where id =1;   
  2. Query OK, 0 rows affected (0.00 sec)
  3. Rows matched: 1  Changed: 0  Warnings: 0
  4. root@localhost : (none) 04:54:03> show engine innodb status\Gshow master status\G
  5. ...
  6. ---
  7. LOG
  8. ---
  9. Log sequence number 12091486
  10. Log flushed up to   12091486
  11. Pages flushed up to 12091486
  12. Last checkpoint at  12091477
  13. 0 pending log flushes, 0 pending chkp writes
  14. 39 log i/o's done, 0.00 log i/o's/second
  15. *************************** 1. row ***************************
  16.              File: mysql-bin.000001
  17.          Position: 500
  18.      Binlog_Do_DB:
  19. Binlog_Ignore_DB:
  20. Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
  21. 1 row in set (0.00 sec)
  22. root@localhost : test 04:52:05> select * from test where id =1;
  23. +----+------+------+------+
  24. | id | sid  | mid  | name |
  25. +----+------+------+------+
  26. |  1 |  999 |  871 | NW   |
  27. +----+------+------+------+
  28. 1 row in set (0.00 sec)
  29. root@localhost : test 04:52:42> commit;
  30. Query OK, 0 rows affected (0.00 sec)
  31. root@localhost : test 04:52:52> select * from test where id =1;
  32. +----+------+------+------+
  33. | id | sid  | mid  | name |
  34. +----+------+------+------+
  35. |  1 |   55 |  871 | NW   |
  36. +----+------+------+------+
  37. 1 row in set (0.00 sec)
复制代码
总结

binlog_format为STATEMENT

参数
  1. root@localhost : (none) 04:53:15> show variables like 'binlog_row_image';
  2. +------------------+-------+
  3. | Variable_name    | Value |
  4. +------------------+-------+
  5. | binlog_row_image | FULL  |
  6. +------------------+-------+
  7. 1 row in set (0.00 sec)
  8. root@localhost : (none) 05:16:08>  show variables like 'binlog_format';
  9. +---------------+-----------+
  10. | Variable_name | Value     |
  11. +---------------+-----------+
  12. | binlog_format | STATEMENT |
  13. +---------------+-----------+
  14. 1 row in set (0.00 sec)
  15. root@localhost : test 05:15:14> show variables like 'transaction_isolation';
  16. +-----------------------+-----------------+
  17. | Variable_name         | Value           |
  18. +-----------------------+-----------------+
  19. | transaction_isolation | REPEATABLE-READ |
  20. +-----------------------+-----------------+
  21. 1 row in set (0.00 sec)
复制代码
测试步骤

session1
  1. root@localhost : test 05:16:42> begin;
  2. Query OK, 0 rows affected (0.00 sec)
  3. root@localhost : test 05:16:44> select * from test where id =1;
  4. +----+------+------+------+
  5. | id | sid  | mid  | name |
  6. +----+------+------+------+
  7. |  1 |  111 |  871 | NW   |
  8. +----+------+------+------+
  9. 1 row in set (0.00 sec)
  10. root@localhost : (none) 05:16:51> show engine innodb status\Gshow master status\G
  11. ...
  12. ---
  13. LOG
  14. ---
  15. Log sequence number 12092582
  16. Log flushed up to   12092582
  17. Pages flushed up to 12092582
  18. Last checkpoint at  12092573
  19. 0 pending log flushes, 0 pending chkp writes
  20. 45 log i/o's done, 0.00 log i/o's/second
  21. *************************** 1. row ***************************
  22.              File: mysql-bin.000001
  23.          Position: 154
  24.      Binlog_Do_DB:
  25. Binlog_Ignore_DB:
  26. Executed_Gtid_Set:
  27. 1 row in set (0.00 sec)
复制代码
session2
  1. root@localhost : test 05:18:30> update test set sid=999 where id =1;
  2. Query OK, 1 row affected (0.00 sec)
  3. Rows matched: 1  Changed: 1  Warnings: 0
  4. root@localhost : (none) 05:18:47> show engine innodb status\Gshow master status\G
  5. ...
  6. ---
  7. LOG
  8. ---
  9. Log sequence number 12093678
  10. Log flushed up to   12093678
  11. Pages flushed up to 12093678
  12. Last checkpoint at  12093669
  13. 0 pending log flushes, 0 pending chkp writes
  14. 51 log i/o's done, 0.14 log i/o's/second
  15. *************************** 1. row ***************************
  16.              File: mysql-bin.000001
  17.          Position: 438
  18.      Binlog_Do_DB:
  19. Binlog_Ignore_DB:
  20. Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
  21. 1 row in set (0.00 sec)
复制代码
session1
  1. root@localhost : test 05:16:47> update test set sid=999 where id =1;
  2. Query OK, 0 rows affected (0.00 sec)
  3. Rows matched: 1  Changed: 0  Warnings: 0
  4. root@localhost : (none) 05:20:03> show engine innodb status\Gshow master status\G
  5. ...
  6. ---
  7. LOG
  8. ---
  9. Log sequence number 12094504
  10. Log flushed up to   12094504
  11. Pages flushed up to 12094504
  12. Last checkpoint at  12094495
  13. 0 pending log flushes, 0 pending chkp writes
  14. 56 log i/o's done, 0.00 log i/o's/second
  15. *************************** 1. row ***************************
  16.              File: mysql-bin.000001
  17.          Position: 438
  18.      Binlog_Do_DB:
  19. Binlog_Ignore_DB:
  20. Executed_Gtid_Set: 8392d215-4928-11e9-a751-0242ac110002:1
  21. 1 row in set (0.00 sec)
  22. root@localhost : test 05:19:33> select * from test where id =1;     
  23. +----+------+------+------+
  24. | id | sid  | mid  | name |
  25. +----+------+------+------+
  26. |  1 |  999 |  871 | NW   |
  27. +----+------+------+------+
  28. 1 row in set (0.00 sec)
  29. root@localhost : test 05:20:44> commit;
  30. Query OK, 0 rows affected (0.01 sec)
  31. root@localhost : test 05:20:57> select * from test where id =1;
  32. +----+------+------+------+
  33. | id | sid  | mid  | name |
  34. +----+------+------+------+
  35. |  1 |  999 |  871 | NW   |
  36. +----+------+------+------+
  37. 1 row in set (0.00 sec)
复制代码
总结

近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2022最新版)
2.劲爆!Java 协程要来了。。。
3.Spring Boot 2.x 教程,太全了!
4.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
5.《Java开发手册(嵩山版)》最新发布,速速下载!
觉得不错,别忘了随手点赞+转发哦!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4