{"id":2082,"date":"2023-04-01T21:51:17","date_gmt":"2023-04-01T13:51:17","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2082"},"modified":"2023-04-06T15:49:26","modified_gmt":"2023-04-06T07:49:26","slug":"have-mysql-transactions-been-quarantined","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/have-mysql-transactions-been-quarantined\/","title":{"rendered":"MySQL\u4e8b\u52a1\u7a76\u7adf\u6709\u6ca1\u6709\u88ab\u9694\u79bb"},"content":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e8b\u52a1\u6765\u8bf4\uff0c\u8bfb\u5230\u7684\u503c\u5e94\u8be5\u662f\u76f8\u540c\u7684\uff0c\u4f46\u6709\u6ca1\u6709\u60f3\u8fc7\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff0c\u5b83\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u4f1a\u4e0d\u4f1a\u6709\u4e00\u4e9b\u7279\u6b8a\u7684\u60c5\u51b5\u5b58\u5728\uff1f\u672c\u7bc7\u6587\u7ae0\u4f1a\u8be6\u7ec6\u7684\u8bb2\u89e3 RR \u7ea7\u522b\u4e0b\u4e8b\u52a1\u88ab\u9694\u79bb\u7684\u539f\u7406\u3002\u5728\u9605\u8bfb\u540e\u5e94\u8be5\u4e86\u89e3\u5982\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>\u4e86\u89e3 MySQL \u4e2d\u7684\u4e24\u79cd\u89c6\u56fe<\/li>\n<li>\u4e86\u89e3 RR \u7ea7\u522b\u4e0b\uff0c\u5982\u4f55\u5b9e\u73b0\u7684\u4e8b\u52a1\u9694\u79bb<\/li>\n<li>\u4e86\u89e3\u4ec0\u4e48\u662f\u5f53\u524d\u8bfb\uff0c\u4ee5\u53ca\u5f53\u524d\u8bfb\u4f1a\u9020\u6210\u54ea\u4e9b\u95ee\u9898<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u660e\u786e\u89c6\u56fe\u7684\u6982\u5ff5<\/h2>\n<p>\u5728 MySQL \u4e2d\uff0c\u89c6\u56fe\u6709\u4e24\u79cd\u3002\u7b2c\u4e00\u79cd\u662f View\uff0c\u4e5f\u5c31\u662f\u5e38\u7528\u6765\u67e5\u8be2\u7684\u865a\u62df\u8868\uff0c\u5728\u8c03\u7528\u65f6\u6267\u884c\u67e5\u8be2\u8bed\u53e5\u4ece\u800c\u83b7\u53d6\u7ed3\u679c\uff0c\u8bed\u6cd5\u5982<code>create view<\/code><\/p>\n<p>\u7b2c\u4e8c\u79cd\u5219\u662f\u5b58\u50a8\u5f15\u64ce\u5c42 InnoDB \u7528\u6765\u5b9e\u73b0 MVCC\uff08Mutil-Version Concurrency Control | \u591a\u7248\u672c\u5e76\u53d1\u63a7\u5236\uff09\u65f6\u7528\u5230\u7684\u4e00\u81f4\u6027\u89c6\u56fe<code>consistent read view<\/code>\uff0c\u7528\u4e8e\u652f\u6301 RC \u548c RR \u9694\u79bb\u7ea7\u522b\u7684\u5b9e\u73b0\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u5b9a\u4e49\u5728\u4e8b\u52a1\u6267\u884c\u671f\u95f4\uff0c\u4e8b\u52a1\u5185\u80fd\u770b\u5230\u4ec0\u4e48\u6837\u7684\u6570\u636e\u3002<\/p>\n<p>\u4e8b\u52a1\u771f\u6b63\u7684\u542f\u52a8\u65f6\u673a\uff1a<\/p>\n<p>\u5728\u4f7f\u7528<code>begin<\/code>\u6216<code>start transation<\/code>\u65f6\uff0c\u4e8b\u52a1\u5e76\u6ca1\u6709\u771f\u6b63\u5f00\u59cb\u8fd0\u884c\uff0c\u800c\u662f\u5728\u6267\u884c\u4e00\u4e2a\u5bf9 InnoDB \u8868\u7684\u64cd\u4f5c\u65f6\uff08\u5373\u7b2c\u4e00\u4e2a\u5feb\u7167\u8bfb\u64cd\u4f5c\u65f6\uff09\uff0c\u4e8b\u52a1\u624d\u771f\u6b63\u542f\u52a8\u3002<\/p>\n<p>\u5982\u679c\u60f3\u8981\u7acb\u5373\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1\uff0c\u53ef\u4ee5\u7528<code>start transaction with consistent snapshot<\/code>\u547d\u4ee4\u3002<\/p>\n<h2>\u4e0d\u671f\u5f85\u7684\u7ed3\u679c\uff0c\u4e8b\u52a1\u6ca1\u6709\u88ab\u9694\u79bb<\/h2>\n<p>\u5728\u4e4b\u524d MySQL \u4e8b\u52a1 \u4ecb\u7ecd\u4e2d\uff0c\u77e5\u9053\u5728 RR \u7684\u7ea7\u522b\u7684\u4e8b\u52a1\u4e0b\uff0c\u5982\u679c\u5176\u4ed6\u4e8b\u52a1\u4fee\u6539\u4e86\u6570\u636e\uff0c\u4e8b\u52a1\u4e2d\u770b\u5230\u7684\u6570\u636e\u548c\u542f\u52a8\u4e8b\u52a1\u65f6\u7684\u6570\u636e\u662f\u4e00\u81f4\u7684\uff0c\u5e76\u4e0d\u4f1a\u53d7\u5176\u4ed6\u4e8b\u52a1\u7684\u5f71\u54cd\u3002\u53ef\u662f\uff0c\u6709\u6ca1\u6709\u4ec0\u4e48\u7279\u6b8a\u7684\u60c5\u51b5\u5462\uff1f<\/p>\n<p>\u770b\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p>\u521b\u5efa\u8868\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; CREATE TABLE `t` (\n  `id` int(11) NOT NULL,\n  `k` int(11) DEFAULT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB;\ninsert into t(id, k) values(1,1),(2,2);<\/code><\/pre>\n<p>\u6309\u7167\u4e0b\u56fe\u5f00\u542f\u4e8b\u52a1\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL\u4e8b\u52a1\u9694\u79bb-\u4e8b\u52a1\u5f00\u542f\u793a\u4f8b.png\" alt=\"MySQL\u4e8b\u52a1\u9694\u79bb-\u4e8b\u52a1\u5f00\u542f\u793a\u4f8b\" \/><\/p>\n<p>\u8fd9\u65f6\u5bf9\u4e8e\u4e8b\u52a1 A \u6765\u8bf4\uff0c\u67e5\u8be2 k \u7684\u503c\u4e3a 1, \u800c<strong>\u4e8b\u52a1 B \u7684 K \u4e3a 3<\/strong>\u3002\u4e0d\u662f\u8bf4\uff0c\u5728 RR \u7ea7\u522b\u4e0b\u7684\u4e8b\u52a1\uff0c\u4e0d\u53d7\u5176\u4ed6\u4e8b\u52a1\u5f71\u54cd\u5417\uff0c\u4e3a\u4ec0\u4e48\u4e8b\u52a1 B \u7ed3\u679c\u4e3a 3 \u800c\u4e0d\u662f\u6240\u671f\u5f85\u7684 2\u3002\u8fd9\u5c31\u6d89\u53ca\u5230\u4e86 MVCC \u4e2d \u201c\u5feb\u7167\u201d \u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<h2>MVCC \u7684\u5b9e\u73b0 &#8211; \u5feb\u7167<\/h2>\n<h3>\u4ec0\u4e48\u662f \u201c\u5feb\u7167\u201d\uff1f<\/h3>\n<p>\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u4e8b\u52a1\u542f\u52a8\u65f6\u4f1a\u57fa\u4e8e\u6574\u5e93\u62cd\u4e2a\u201c\u5feb\u7167\u201d\uff0c\u7528\u4e8e\u8bb0\u5f55\u5f53\u524d\u72b6\u6001\u4e0b\u7684\u6570\u636e\u4fe1\u606f\u3002\u53ef\u8fd9\u6837\u7684\u8bdd\uff0c\u5bf9\u4e8e\u5e93\u6bd4\u8f83\u5927\u7684\u60c5\u51b5\uff0c\u4e8b\u52a1\u7684\u542f\u52a8\u5e94\u8be5\u975e\u5e38\u6162\u3002\u53ef\u5b9e\u9645\u4e0a\u7684\u6267\u884c\u8fc7\u7a0b\u975e\u5e38\u5feb\uff0c\u539f\u56e0\u5c31\u5728\u4e8e InnoDB \u4e2d\u7684\u5b9e\u73b0\u3002<\/p>\n<h3>\u201c\u5feb\u7167\u201d\u7684\u5b9e\u73b0<\/h3>\n<p>\u5728 InnoDB \u4e2d\uff0c\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u6709\u552f\u4e00\u7684\u4e8b\u52a1 ID\uff0c\u53eb\u505a<code>transaction id<\/code>\u3002\u5728\u4e8b\u52a1\u5f00\u59cb\u65f6\uff0c\u6309\u7167\u4e25\u683c\u9012\u589e\u7684\u987a\u5e8f\u5411 InnoDB \u4e8b\u52a1\u7cfb\u7edf\u7533\u8bf7<\/p>\n<p>\u6570\u636e\u5e93\u4e2d\uff0c\u6bcf\u884c\u6570\u636e\u5177\u6709\u591a\u4e2a\u7248\u672c\u3002\u5728\u6bcf\u6b21\u5f00\u542f\u66f4\u65b0\u7684\u4e8b\u52a1\u65f6\uff0c\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u7248\u672c\uff0c\u5e76<strong>\u628a<code>transaction id<\/code>\u8d4b\u503c\u7ed9\u5f53\u524d\u6570\u636e\u7248\u672c\u7684\u4e8b\u52a1 ID\uff0c\u8bb0\u4e3a<code>row trx_id<\/code><\/strong><\/p>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u5bf9\u4e8e\u540c\u4e00\u884c\u6570\u636e\u8fde\u7eed\u66f4\u65b0\u4e86 4 \u6b21\uff0c\u5bf9\u5e94 4 \u4e2a\u7248\u672c\u5982\u4e0b\u3002\u5bf9\u4e8e\u6700\u65b0\u7248\u672c V4 \u60c5\u51b5\uff0cK \u4e3a 22\uff0c\u662f\u88ab\u4e8b\u52a1 id \u4e3a 25 \u6240\u66f4\u65b0\u7684\uff0c\u8fdb\u800c row trx_id \u662f 25<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL\u4e8b\u52a1\u9694\u79bb-\u5feb\u7167\u5b9e\u73b0.png\" alt=\"MySQL\u4e8b\u52a1\u9694\u79bb-\u5feb\u7167\u5b9e\u73b0\" \/><\/p>\n<p>\u5728\u6bcf\u6b21\u66f4\u65b0\u65f6\uff0c\u90fd\u4f1a\u751f\u6210\u4e00\u6761\u56de\u6eda\u65e5\u5fd7\uff08undo log\uff09\uff0c\u4e0a\u56fe\u4e2d\u4e09\u4e2a\u865a\u62df\u7bad\u5934\uff08U1\uff0cU2\uff0cU3\uff09\u5c31\u662f undo log\u3002\u6bcf\u6b21\u5f00\u542f\u4e8b\u52a1\u7684\u89c6\u56fe V1\uff0cV2\uff0cV3 \u7269\u7406\u4e0a\u5e76\u4e0d\u771f\u5b9e\u5b58\u5728\uff0c\u800c\u662f\u901a\u8fc7\u5f53\u524d\u4e8b\u52a1\u7248\u672c\u548c undo log \u8ba1\u7b97\u51fa\u6765\u3002<\/p>\n<p>\u4e86\u89e3\u4e86 row trx_id \u548c transaction id\uff0c\u5c31\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4e86\u89e3\u4e8b\u52a1\u5177\u4f53\u662f\u5982\u4f55\u8fdb\u884c\u9694\u79bb\u7684\u4e86\u3002<\/p>\n<h2>\u4e8b\u52a1\u9694\u79bb\u7684\u5b9e\u73b0<\/h2>\n<p>\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u5982\u679c\u8981\u60f3\u5b9e\u73b0\u4e00\u4e2a\u4e8b\u52a1\u542f\u52a8\u65f6\uff0c\u80fd\u591f\u770b\u5230\u6240\u6709\u5df2\u7ecf\u63d0\u4ea4\u7684\u4e8b\u52a1\u7ed3\u679c\uff0c\u800c\u5728\u4e8b\u52a1\u6267\u884c\u671f\u95f4\uff0c\u5176\u4ed6\u4e8b\u52a1\u7684\u66f4\u65b0\u5747\u4e0d\u53ef\u89c1\u7684\u6548\u679c\u3002<\/p>\n<p>\u53ea\u9700\u8981\u5728\u4e8b\u52a1\u542f\u52a8\u65f6\u89c4\u5b9a\uff0c<strong>\u4ee5\u542f\u52a8\u7684\u65f6\u523b\u4e3a\u51c6\uff0c\u5982\u679c\u4e00\u4e2a\u6570\u636e\u7248\u672c\u5728\u542f\u52a8\u524d\u751f\u6210\uff0c\u5c31\u53ef\u4ee5\u67e5\u770b\u3002\u5982\u679c\u5728\u542f\u52a8\u540e\u751f\u6210\uff0c\u5219\u4e0d\u80fd\u67e5\u770b\uff0c\u901a\u8fc7 undo log \u4e00\u76f4\u67e5\u8be2\u4e0a\u4e00\u4e2a\u7248\u672c\u6570\u636e\uff0c\u76f4\u5230\u627e\u5230\u542f\u52a8\u524d\u751f\u6210\u7684\u6570\u636e\u7248\u672c\u6216\u8005\u81ea\u5df1\u66f4\u65b0\u7684\u6570\u636e\u624d\u7ed3\u675f<\/strong>\u3002<\/p>\n<p>\u5728\u5177\u4f53\u5b9e\u73b0\u4e0a\uff0cInnoDB \u4e3a\u6bcf\u4e2a\u4e8b\u52a1\u6784\u9020\u4e00\u4e2a\u6570\u7ec4\uff0c\u7528\u6765\u4fdd\u5b58\u4e8b\u52a1\u542f\u52a8\u77ac\u95f4\uff0c\u5f53\u524d\u6b63\u5728\u6d3b\u8dc3\uff08\u542f\u52a8\u6ca1\u6709\u63d0\u4ea4\uff09\u7684\u6240\u6709\u4e8b\u52a1 ID\u3002<strong>\u6570\u7ec4\u91cc ID \u6700\u5c0f\u4e3a\u4f4e\u6c34\u4f4d\uff0c\u5f53\u524d\u7cfb\u7edf\u91cc\u9762\u521b\u5efa\u8fc7\u7684\u4e8b\u52a1 ID \u6700\u5927\u503c + 1 \u4e3a\u9ad8\u6c34\u4f4d\u3002\u8fd9\u4e2a\u6570\u7ec4\u548c\u9ad8\u6c34\u4f4d\uff0c\u5c31\u7ec4\u6210\u4e86\u5f53\u524d\u4e8b\u52a1\u7684\u4e00\u81f4\u6027\u89c6\u56fe\uff08read-view\uff09<\/strong>\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL\u4e8b\u52a1\u9694\u79bb-\u4e8b\u52a1\u9694\u79bb\u5b9e\u73b0.png\" alt=\"MySQL\u4e8b\u52a1\u9694\u79bb-\u4e8b\u52a1\u9694\u79bb\u5b9e\u73b0\" \/><\/p>\n<p>\u6570\u636e\u662f\u5426\u770b\u89c1\uff0c\u5c31\u662f\u901a\u8fc7\u6bd4\u8f83\u6570\u636e\u7684 row trx_id \u548c \u4e00\u81f4\u6027\u89c6\u56fe\u7684\u5bf9\u6bd4\u800c\u5f97\u5230\u7684\u3002<\/p>\n<p>\u5728\u6bd4\u8f83\u65f6\uff1a<\/p>\n<p><strong>1\u3001\u5982\u679c row trx_id \u51fa\u73b0\u5728\u7eff\u8272\u90e8\u5206\uff0c\u8868\u793a\u8be5\u7248\u672c\u662f\u5df2\u63d0\u4ea4\u7684\u4e8b\u52a1\u6216\u8005\u5f53\u524d\u7684\u4e8b\u52a1\u81ea\u5df1\u751f\u6210\u7684\uff0c\u8be5\u6570\u636e\u53ef\u89c1<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u4e8b\u52a1A<\/th>\n<th style=\"text-align: left;\">\u4e8b\u52a1B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set k = k+1 where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">commit;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e8b\u52a1 A \u4fee\u6539 k \u503c\u540e\u63d0\u4ea4\uff0c\u63a5\u7740\u4e8b\u52a1 B \u67e5\u8be2 k \u503c\u3002\u8fd9\u65f6\u5bf9\u4e8e\u542f\u52a8\u7684\u4e8b\u52a1 B \u6765\u8bf4\uff0ck \u503c\u7684 row trx_id \u7b49\u4e8e\u4e8b\u52a1 A \u7684<code>transaction id<\/code>\u3002\u800c\u4e8b\u52a1 B \u5728 \u4e8b\u52a1 A \u4e4b\u540e\u7533\u8bf7\uff0c\u5047\u8bbe\u5f53\u524d\u6d3b\u8dc3\u4e8b\u52a1\u53ea\u6709 B\u3002B \u7684<code>transaction id<\/code>\u80af\u5b9a\u5927\u4e8e\u4e8b\u52a1 A\uff0c\u6240\u4ee5\u5f53\u524d\u7248\u672c row trx_id \u4e00\u5b9a\u5c0f\u4e8e\u4f4e\u6c34\u4f4d\uff0c\u8fdb\u800c k \u503c\u4e3a A \u4fee\u6539\u540e\u7684\u503c\u3002<\/p>\n<p><strong>2\u3001\u5982\u679c\u5728\u7ea2\u8272\u90e8\u5206\uff0c\u8868\u793a\u7531\u672a\u6765\u7684\u4e8b\u52a1\u751f\u6210\u7684\uff0c\u8be5\u6570\u636e\u4e0d\u53ef\u89c1<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u4e8b\u52a1A<\/th>\n<th style=\"text-align: left;\">\u4e8b\u52a1B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">update t set k = k+1 where id=1;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">commit;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e8b\u52a1 A \u5f00\u542f\u540e\uff0c\u67e5\u8be2 k \u503c\uff0c\u4f46\u672a\u63d0\u4ea4\u4e8b\u52a1\u3002\u4e8b\u52a1 B \u5728\u4e8b\u52a1 A \u5f00\u542f\u540e\u4fee\u6539 K \u503c\u3002\u6b64\u65f6\u5bf9\u4e8e\u4e8b\u52a1 A \u6765\u8bf4\uff0c\u4fee\u6539\u540e k \u503c\u7684<code>row trx_id<\/code>\u7b49\u4e8e\u4e8b\u52a1B <code>transaction id<\/code>\u3002\u5047\u8bbe\u5f53\u524d\u7684\u6d3b\u8dc3\u7684\u53ea\u6709\u4e8b\u52a1 A\uff0c\u5219<code>row trx_id<\/code>\u5927\u4e8e\u9ad8\u6c34\u4f4d\u7684\u503c\uff0c\u6240\u4ee5\u4e8b\u52a1 B \u7684\u4fee\u6539\u5bf9 A \u4e0d\u53ef\u89c1\u3002<\/p>\n<p><strong>3\u3001\u5982\u679c\u843d\u5728\u9ec4\u8272\u90e8\u5206\uff0c\u4e24\u79cd\u60c5\u51b5<\/strong><\/p>\n<p>\uff081\uff09row trx_id \u5728\u6570\u7ec4\u4e2d\uff0c\u8868\u793a\u8be5\u7248\u672c\u662f\u7531\u672a\u63d0\u4ea4\u7684\u4e8b\u52a1\u751f\u6210\u7684\uff0c\u4e0d\u53ef\u89c1<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u4e8b\u52a1A<\/th>\n<th style=\"text-align: left;\">\u4e8b\u52a1B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set k = k+1 where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e8b\u52a1 A\uff0cB \u5148\u540e\u5f00\u542f\uff0c\u5047\u8bbe\u53ea\u6709 A\uff0cB \u4e24\u4e2a\u6d3b\u8dc3\u7684\u4e8b\u52a1\u3002\u6b64\u65f6\u5bf9\u4e8e\u4e8b\u52a1 B \u6765\u8bf4\u4e00\u81f4\u6027\u89c6\u56fe\u4e2d\u7684\u6570\u7ec4\u5305\u542b\u4e8b\u52a1 A \u548c B \u7684 transaction id<\/p>\n<p>\u5f53\u4e8b\u52a1 B \u67e5\u8be2 k \u503c\u65f6\uff0c\u53d1\u73b0\u6570\u7ec4\u4e2d\u5305\u542b\u4e8b\u52a1 A \u7684 transaction id\uff0c\u8bf4\u660e\u662f\u672a\u63d0\u4ea4\u7684\u4e8b\u52a1\u3002\u6240\u4ee5\u4e0d\u53ef\u89c1<\/p>\n<p>\u8fd9\u65f6\uff0c\u5047\u8bbe\u8ba9\u4e8b\u52a1 A \u5728\u4e8b\u52a1 B \u67e5\u8be2\u524d\u63d0\u4ea4\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u4e8b\u52a1A<\/th>\n<th style=\"text-align: left;\">\u4e8b\u52a1B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set k = k+1 where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">commit;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f53\u5bf9\u4e8e\u4e8b\u52a1 B \u6765\u8bf4\uff0c\u4e8b\u52a1 A \u867d\u7136\u63d0\u4ea4\u4e86\uff0c\u5f53\u5176\u63d0\u4ea4\u7684 row transaction_id \u4f9d\u7136\u662f B \u542f\u52a8\u65f6\u7684\u6d3b\u8dc3\u4e8b\u52a1\u91cc\u9762\u7684\uff0c\u6240\u4ee5\u66f4\u65b0\u4f9d\u7136\u4e0d\u53ef\u89c1\u3002<\/p>\n<p><strong>\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ea\u8981\u5728\u4e8b\u52a1\u542f\u52a8\u65f6\uff0c\u540c\u65f6\u6d3b\u8dc3\u7684\u5176\u4ed6\u4e8b\u52a1\uff0c\u65e0\u8bba\u662f\u5426\u6bd4\u5f53\u524d\u4e8b\u52a1\u63d0\u4ea4\u7684\u65e9\u6216\u665a\uff0c\u90fd\u662f\u5c5e\u4e8e\u4e0d\u53ef\u89c1\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<p>\uff082\uff09row trx_id \u4e0d\u5728\u6570\u7ec4\u4e2d\uff0c\u8868\u793a\u8be5\u7248\u672c\u662f\u7531\u5df2\u63d0\u4ea4\u7684\u4e8b\u52a1\u751f\u6210\uff0c\u53ef\u89c1<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u4e8b\u52a1A (transaction id = 100)<\/th>\n<th style=\"text-align: left;\">\u4e8b\u52a1B (transaction id = 101)<\/th>\n<th style=\"text-align: left;\">\u4e8b\u52a1 C (transaction id = 102)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set k = k+1 where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">update t set k = k+1 where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">commit;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5047\u8bbe\u5f53\u524d\u53ea\u6d3b\u8dc3 A\uff0cC \u4e24\u4e2a\u4e8b\u52a1\u3002\u5bf9\u4e8e\u4e8b\u52a1 C \u6765\u8bf4\uff0c\u4e00\u81f4\u6027\u89c6\u56fe\u6570\u7ec4\u4e3a[100,102]\u3002\u5f53\u524d k \u7684 row trx_id \u4e3a 101\uff0c\u4e0d\u5728\u4e00\u81f4\u6027\u6570\u7ec4\u4e2d\u3002\u8bf4\u660e\u662f\u5df2\u7ecf\u63d0\u4ea4\u4e8b\u52a1\uff0c\u6240\u4ee5\u6570\u636e\u53ef\u89c1\u3002<\/p>\n<p><strong>InnoDB \u5c31\u662f\u5229\u7528\u4e86\u6240\u6709\u6570\u636e\u90fd\u6709\u591a\u4e2a\u7248\u672c\u8fd9\u4e2a\u7279\u6027\uff0c\u5b9e\u73b0\u4e86\u79d2\u7ea7\u521b\u5efa\u5feb\u7167\u7684\u80fd\u529b<\/strong>\u3002<\/p>\n<p>\u73b0\u5728\u56de\u5230\u6587\u7ae0\u5f00\u59cb\u7684\u4e09\u4e2a\u4e8b\u52a1\uff0c\u73b0\u5728\u5206\u6790\u4e0b\u4e3a\u4ec0\u4e48\u4e8b\u52a1 A \u7684\u7ed3\u679c\u662f 1\u3002<\/p>\n<p>\u5047\u8bbe\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5728 A \u542f\u52a8\u65f6\uff0c\u7cfb\u7edf\u53ea\u6709\u4e00\u4e2a\u6d3b\u8dc3\u7684\u4e8b\u52a1 ID \u4e3a 99<\/li>\n<li>A\uff0cB\uff0cC \u4e8b\u52a1\u7684\u7248\u672c\u53f7\u4e3a 100,101,102 \u4e14\u5f53\u524d\u7cfb\u7edf\u4e2d\u53ea\u6709\u8fd9\u56db\u4e2a\u4e8b\u52a1<\/li>\n<li>\u4e09\u4e2a\u4e8b\u52a1\u5f00\u59cb\u524d\uff0c(1,1)\u5bf9\u5e94 row trx_id \u4e3a 90<\/li>\n<\/ol>\n<p>\u8fd9\u6837\u5bf9\u4e8e\u4e8b\u52a1 A\uff0cB\uff0cC \u4e2d\u4e00\u81f4\u6027\u6570\u7ec4\u548c row trx_id \u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL\u4e8b\u52a1\u9694\u79bb-\u9ec4\u8272\u53ef\u89c1.png\" alt=\"MySQL\u4e8b\u52a1\u9694\u79bb-\u9ec4\u8272\u53ef\u89c1\" \/><\/p>\n<p>\u53f3\u4fa7\u663e\u793a\u7684\u56de\u6eda\u6bb5\u7684\u5185\u5bb9\uff0c\u7b2c\u4e00\u6b21\u66f4\u65b0\u4e3a\u4e8b\u52a1 C\uff0c\u5176 row trx_id \u7b49\u4e8e 102\uff0c\u503c\u4e3a (1,2)\u3002\u6700\u65b0 row trx_id \u4e3a\u4e8b\u52a1 B \u7684 101\uff0c\u503c\u4e3a (1,3)<\/p>\n<p>\u5bf9\u4e8e\u4e8b\u52a1 A \u6765\u8bf4\uff0c\u89c6\u56fe\u6570\u7ec4\u4e3a [99,100]\u3002\u8bfb\u53d6\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d row trx_id \u4e3a 101 \u7684\u6570\u636e\u3002\u53d1\u73b0\u6bd4\u9ad8\u6c34\u4f4d\u5927\uff0c\u843d\u5728\u7ea2\u8272\uff0c\u4e0d\u53ef\u89c1<\/li>\n<li>\u5411\u4e0a\u67e5\u627e\uff0c\u53d1\u73b0 row trx_id \u4e3a 102 \u7684\uff0c\u6bd4\u9ad8\u6c34\u4f4d\u5927\uff0c\u4e0d\u53ef\u89c1<\/li>\n<li>\u5411\u4e0a\u67e5\u627e\uff0c\u53d1\u73b0 row trx_id \u4e3a 90\uff0c\u6bd4\u4f4e\u6c34\u4f4d\u5c0f\uff0c\u843d\u5728\u7eff\u8272\u53ef\u89c1<\/li>\n<\/ol>\n<p><strong>\u8fd9\u65f6\u4e8b\u52a1 A \u65e0\u8bba\u5728\u4ec0\u4e48\u65f6\u5019\u67e5\u8be2\uff0c\u770b\u5230\u7684\u7ed3\u679c\u90fd\u4e00\u81f4\uff0c\u8fd9\u88ab\u79f0\u4e3a\u4e00\u81f4\u6027\u8bfb<\/strong>\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u5224\u65ad\u903b\u8f91\u4e3a\u4ee3\u7801\u903b\u8f91\uff0c\u73b0\u5728\u7ffb\u8bd1\u6210\u4fbf\u4e8e\u7406\u89e3\u7684\u8bed\u8a00\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e8b\u52a1\u89c6\u56fe\u6765\u8bf4\uff0c\u9664\u4e86\u81ea\u5df1\u7684\u66f4\u65b0\u53ef\u89c1\u5916\uff1a<\/p>\n<ol>\n<li>\u7248\u672c\u672a\u63d0\u4ea4\uff0c\u4e0d\u53ef\u89c1\uff08\u5305\u542b\u4e86\u8fd8\u672a\u63d0\u4ea4\u7684\u4e8b\u52a1\uff0c\u6216\u8005\u5f00\u59cb\u540c\u65f6\u6d3b\u8dc3\uff0c\u4f46\u5148\u4e00\u6b65\u63d0\u4ea4\u7684\u4e8b\u52a1\uff09<\/li>\n<li>\u7248\u672c\u5df2\u63d0\u4ea4\uff0c\u5728\u89c6\u56fe\u540e\u521b\u5efa\u63d0\u4ea4\u7684\uff0c\u4e0d\u53ef\u89c1<\/li>\n<li>\u7248\u672c\u5df2\u63d0\u4ea4\uff0c\u5728\u89c6\u56fe\u521b\u5efa\u524d\u63d0\u4ea4\uff0c\u53ef\u89c1<\/li>\n<\/ol>\n<p>\u73b0\u5728\u5e94\u8be5\u6e05\u695a\uff0c\u53ef\u91cd\u590d\u8bfb\u7684\u80fd\u529b\u5c31\u662f\u901a\u8fc7\u4e00\u81f4\u6027\u8bfb\u5b9e\u73b0\u7684\u3002\u53ef\u662f\u5728\u6587\u7ae0\u5f00\u59cb\u90e8\u5206\u4e8b\u52a1 B \u7684\u66f4\u65b0\u8bed\u53e5\u5982\u679c\u6309\u7167\u4e00\u81f4\u6027\u8bfb\u7684\u60c5\u51b5\uff0c\u4e8b\u52a1 C \u5728\u4e8b\u52a1 B \u4e4b\u540e\u63d0\u4ea4\uff0c\u7ed3\u679c\u5e94\u8be5\u662f(1,2)\u4e0d\u662f(1,3)\u3002\u539f\u56e0\u5c31\u5728\u4e8e\u5f53\u524d\u8bfb\u7684\u5f71\u54cd\u3002<\/p>\n<h2>\u5f53\u524d\u8bfb\u7684\u5f71\u54cd<\/h2>\n<p>\u5bf9\u4e8e\u6587\u7ae0\u5f00\u5934\u90e8\u5206\u7684\u4e8b\u52a1 B \u6765\u8bf4\uff0c\u5982\u679c\u5728\u66f4\u65b0\u64cd\u4f5c\u524d\u67e5\u8be2\u4e00\u6b21\u6570\u636e\uff0c\u8fd4\u56de\u7ed3\u679c\u786e\u5b9e\u662f 1\u3002\u4f46\u7531\u4e8e\u66f4\u65b0\u64cd\u4f5c\uff0c\u5e76\u4e0d\u662f\u5728\u5386\u53f2\u7248\u672c\u4e0a\u66f4\u65b0\uff0c\u5426\u5219\u4e8b\u52a1 C \u7684\u66f4\u65b0\u5c31\u4f1a\u88ab\u8986\u76d6\u3002\u56e0\u6b64\u4e8b\u52a1 B \u7684\u66f4\u65b0\u64cd\u4f5c\u662f\u5728(1,2)\u7684\u57fa\u7840\u4e0a\u64cd\u4f5c\u7684\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u5f53\u524d\u8bfb\uff1f<\/h3>\n<p>\u5728\u66f4\u65b0\u64cd\u4f5c\u65f6\uff0c\u90fd\u662f\u5148\u8bfb\u540e\u5199\uff0c\u8fd9\u4e2a\u8bfb\uff0c\u5c31\u662f\u53ea\u80fd\u8bfb\u5f53\u524d\u7684\u503c\uff08\u6700\u65b0\u5df2\u7ecf\u63d0\u4ea4\u7684\u503c\uff09\uff0c\u8fdb\u800c\u79f0\u4e3a\u201c\u5f53\u524d\u8bfb\u201d\u3002<\/p>\n<p>\u9664 update \u8bed\u53e5\u5916\uff0c\u7ed9 select \u8bed\u53e5\u52a0\u9501\uff0c\u4e5f\u662f\u5f53\u524d\u8bfb\u3002\u9501\u7684\u7c7b\u578b\u53ef\u4ee5\u662f\u8bfb\u9501\uff08S\u9501\uff0c\u5171\u4eab\u9501\uff09\u548c\u5199\u9501\uff08X\u9501\uff0c\u6392\u4ed6\u9501\uff09\u3002<\/p>\n<p>\u6bd4\u5982\u60f3\u8ba9\u4e8b\u52a1 A \u7684\u67e5\u8be2\u8bed\u53e5\u83b7\u53d6\u5f53\u524d\u8bfb\u4e2d\u7684\u503c\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u5171\u4eab\u9501 - \u5141\u8bb8\u5176\u4ed6\u4e8b\u52a1\u8bfb\u53d6\u88ab\u9501\u5b9a\u7684\u884c\nmysql&gt; select k from t where id=1 lock in share mode;\n\n# \u6392\u5b83\u9501 - \u4e0d\u5141\u8bb8\u5176\u4ed6\u4e8b\u52a1\u8bfb\u53d6\u88ab\u9501\u5b9a\u7684\u884c\nmysql&gt; select k from t where id=1 for update;<\/code><\/pre>\n<h3>\u5728\u5f53\u524d\u8bfb\u4e0b\uff0c\u5feb\u7167\u67e5\u8be2\u7684\u8fc7\u7a0b<\/h3>\n<p>\u5728\u4e8b\u52a1 B \u66f4\u65b0\u65f6\uff0c\u5f53\u524d\u8bfb\u62ff\u5230\u7684\u503c\u4e3a(1,2)\uff0c\u66f4\u65b0\u540e\u751f\u6210\u7684\u65b0\u7248\u672c\u6570\u636e\u4e3a(1,3)\uff0c\u5f53\u524d\u7248\u672c\u7684 row trx_id 101.<\/p>\n<p>\u6240\u4ee5\u5728\u63a5\u4e0b\u91cc\u7684\u6267\u884c\u7684\u67e5\u8be2\u8bed\u53e5\u65f6\uff0c\u5f53\u524d row trx_id \u4e3a101\uff0c\u5224\u65ad\u4e3a\u81ea\u5df1\u66f4\u65b0\u7684\uff0c\u6240\u4ee5\u53ef\u89c1\u3002\u6240\u4ee5\u67e5\u8be2\u7ed3\u679c\u662f(1,3)\u3002<\/p>\n<p>\u5047\u8bbe\u4e8b\u52a1 C \u6539\u6210\u5982\u4e0b\u4e8b\u52a1 C&#8217; \u8fd9\u6837\uff0c\u5728\u4e8b\u52a1 B \u66f4\u65b0\u540e\uff0c\u518d\u63d0\u4ea4\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL\u4e8b\u52a1\u9694\u79bb-\u5f53\u524d\u8bfb\u4e0b\u5feb\u7167\u67e5\u8be2\u8fc7\u7a0b.png\" alt=\"MySQL\u4e8b\u52a1\u9694\u79bb-\u5f53\u524d\u8bfb\u4e0b\u5feb\u7167\u67e5\u8be2\u8fc7\u7a0b\" \/><\/p>\n<p>\u8fd9\u65f6\u867d\u7136(1,2)\u5df2\u7ecf\u751f\u6210\u4e86\uff0c\u4f46\u6839\u636e\u4e24\u9636\u6bb5\u9501\u534f\u8bae\uff0c\u7531\u4e8e\u4e8b\u52a1 C\u2019 \u6ca1\u6709\u63d0\u4ea4\uff0c\u6ca1\u6709\u91ca\u653e\u5199\u9501\u3002\u8fd9\u65f6\u4e8b\u52a1 B \u5c31\u4f1a\u88ab\u9501\u4f4f\uff0c\u7b49\u5230\u5176\u4ed6\u4e8b\u52a1\u91ca\u653e\u540e\uff0c\u518d\u7ee7\u7eed\u5f53\u524d\u8bfb\u3002<\/p>\n<p>\u53ef\u91cd\u590d\u8bfb\u7684\u6838\u5fc3\u5c31\u662f\u4e00\u81f4\u6027\u8bfb\uff0c\u800c\u4e8b\u52a1\u66f4\u65b0\u6570\u636e\u7684\u65f6\u5019\uff0c\u53ea\u80fd\u7528\u5f53\u524d\u8bfb\u3002\u5982\u679c\u5f53\u524d\u7684\u8bb0\u5f55\u7684\u884c\u9501\u88ab\u5176\u4ed6\u4e8b\u52a1\u5360\u7528\u7684\u8bdd\uff0c\u5c31\u9700\u8981\u8fdb\u5165\u9501\u7b49\u5f85\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u4e8b\u52a1\u6765\u8bf4\uff0c\u8bfb\u5230\u7684\u503c\u5e94\u8be5\u662f\u76f8\u540c\u7684\uff0c\u4f46\u6709\u6ca1\u6709\u60f3\u8fc7\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff0c\u5b83\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u4f1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[87],"class_list":["post-2082","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-87"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2082"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2082\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}