{"id":5535,"date":"2024-11-14T12:06:37","date_gmt":"2024-11-14T04:06:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5535\/"},"modified":"2024-11-14T12:06:37","modified_gmt":"2024-11-14T04:06:37","slug":"mysql%e4%b9%90%e8%a7%82%e9%94%81%e6%80%8e%e4%b9%88%e5%bc%80","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5535\/","title":{"rendered":"mysql\u4e50\u89c2\u9501\u600e\u4e48\u5f00"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e50\u89c2\u9501\u901a\u8fc7\u68c0\u67e5\u6570\u636e\u81ea\u4e0a\u6b21\u8bfb\u53d6\u4ee5\u6765\u7684\u7248\u672c\u662f\u5426\u5df2\u66f4\u6539\u6765\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\u3002\u8981\u542f\u7528\u4e50\u89c2\u9501\uff0c\u53ef\u4ee5\u4f7f\u7528 read_committed \u6216 repeatable_read \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff0c\u6216\u5728 where \u5b50\u53e5\u4e2d\u4f7f\u7528\u7248\u672c\u5217\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052908302838365.jpg\" class=\"aligncenter\" title=\"mysql\u4e50\u89c2\u9501\u600e\u4e48\u5f00\u63d2\u56fe\" alt=\"mysql\u4e50\u89c2\u9501\u600e\u4e48\u5f00\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u542f\u7528 MySQL \u4e50\u89c2\u9501<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u4e50\u89c2\u9501\uff1f<\/strong><\/p>\n<p>\u4e50\u89c2\u9501\u662f\u4e00\u79cd\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u5b83\u5047\u8bbe\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u6570\u636e\u4e0d\u4f1a\u88ab\u5176\u4ed6\u4e8b\u52a1\u4fee\u6539\u3002\u5728\u66f4\u65b0\u6570\u636e\u4e4b\u524d\uff0c\u5b83\u5c06\u9996\u5148\u68c0\u67e5\u6570\u636e\u81ea\u4e0a\u6b21\u8bfb\u53d6\u4ee5\u6765\u7684\u7248\u672c\u662f\u5426\u5df2\u66f4\u6539\u3002\u5982\u679c\u7248\u672c\u53d1\u751f\u66f4\u6539\uff0c\u5219\u66f4\u65b0\u64cd\u4f5c\u5c06\u88ab\u4e2d\u6b62\u3002<\/p>\n<p><strong>\u542f\u7528 MySQL \u4e50\u89c2\u9501<\/strong><\/p>\n<p>\u8981\u542f\u7528 MySQL \u4e2d\u7684\u4e50\u89c2\u9501\uff0c\u9700\u8981\u4f7f\u7528 READ_COMMITTED \u6216 REPEATABLE_READ \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u3002<\/p>\n<p><strong>1. READ_COMMITTED<\/strong><\/p>\n<p>READ_COMMITTED \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u5141\u8bb8\u4e8b\u52a1\u8bfb\u53d6\u672a\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u4f46\u53ea\u80fd\u8bfb\u53d6\u5df2\u63d0\u4ea4\u7684\u6570\u636e\u3002\u5728\u4f7f\u7528\u8be5\u7ea7\u522b\u65f6\uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u4e8b\u52a1\u5728\u5f53\u524d\u4e8b\u52a1\u8bfb\u53d6\u6570\u636e\u540e\u66f4\u65b0\u4e86\u6570\u636e\uff0c\u5219\u5f53\u524d\u4e8b\u52a1\u5728\u66f4\u65b0\u6570\u636e\u65f6\u5c06\u5931\u8d25\u3002<\/p>\n<p><strong>2. REPEATABLE_READ<\/strong><\/p>\n<p>REPEATABLE_READ \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u6bd4 READ_COMMITTED \u7ea7\u522b\u66f4\u4e25\u683c\u3002\u5b83\u786e\u4fdd\u5728\u4e8b\u52a1\u6267\u884c\u671f\u95f4\uff0c\u6570\u636e\u4e0d\u4f1a\u88ab\u5176\u4ed6\u4e8b\u52a1\u4fee\u6539\u3002\u5982\u679c\u53e6\u4e00\u4e2a\u4e8b\u52a1\u66f4\u65b0\u4e86\u5f53\u524d\u4e8b\u52a1\u6b63\u5728\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u5219\u5f53\u524d\u4e8b\u52a1\u5728\u66f4\u65b0\u6570\u636e\u65f6\u5c06\u5931\u8d25\u3002<\/p>\n<p>\u8981\u8bbe\u7f6e\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<\/p>\n<pre>SET TRANSACTION ISOLATION LEVEL READ COMMITTED;\nSET TRANSACTION ISOLATION LEVEL REPEATABLE READ;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9664\u4e86\u8bbe\u7f6e\u9694\u79bb\u7ea7\u522b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 WHERE \u5b50\u53e5\u4e2d\u7684\u7248\u672c\u5217\u6765\u5b9e\u73b0\u4e50\u89c2\u9501\u3002\u8fd9\u5c06\u68c0\u67e5\u7248\u672c\u5217\u662f\u5426\u81ea\u4e0a\u6b21\u8bfb\u53d6\u4ee5\u6765\u53d1\u751f\u66f4\u6539\uff0c\u5e76\u6839\u636e\u9700\u8981\u4e2d\u6b62\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<pre>SELECT * FROM table WHERE version = @current_version;\n\nUPDATE table SET ... WHERE version = @current_version;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u9996\u5148\u8bfb\u53d6\u8868\u4e2d\u7684\u6570\u636e\u5e76\u5b58\u50a8\u5f53\u524d\u7248\u672c\u3002\u7136\u540e\uff0c\u5728\u66f4\u65b0\u6570\u636e\u4e4b\u524d\uff0c\u68c0\u67e5\u7248\u672c\u662f\u5426\u5df2\u66f4\u6539\u3002\u5982\u679c\u7248\u672c\u5df2\u66f4\u6539\uff0c\u5219\u66f4\u65b0\u64cd\u4f5c\u5c06\u4e2d\u6b62\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e50\u89c2\u9501\u600e\u4e48\u5f00\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u4e50\u89c2\u9501\u901a\u8fc7\u68c0\u67e5\u6570\u636e\u81ea\u4e0a\u6b21\u8bfb\u53d6\u4ee5\u6765\u7684\u7248\u672c\u662f\u5426\u5df2\u66f4\u6539\u6765\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\u3002\u8981\u542f\u7528\u4e50\u89c2\u9501\uff0c\u53ef\u4ee5\u4f7f\u7528 read_committed \u6216 repeatable_read \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff0c\u6216\u5728 where \u5b50\u53e5\u4e2d\u4f7f\u7528\u7248\u672c\u5217\u3002 \u5982\u4f55\u542f\u7528 MySQL \u4e50\u89c2\u9501 \u4ec0\u4e48\u662f\u4e50\u89c2\u9501\uff1f \u4e50\u89c2\u9501\u662f\u4e00\u79cd\u5e76\u53d1\u63a7\u5236\u673a\u5236\uff0c\u5b83\u5047\u8bbe\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u6570\u636e\u4e0d\u4f1a\u88ab\u5176\u4ed6\u4e8b\u52a1\u4fee\u6539\u3002\u5728\u66f4\u65b0\u6570\u636e\u4e4b\u524d\uff0c\u5b83\u5c06\u9996\u5148\u68c0\u67e5\u6570\u636e\u81ea\u4e0a\u6b21\u8bfb\u53d6\u4ee5\u6765\u7684\u7248\u672c\u662f\u5426\u5df2\u66f4\u6539\u3002\u5982\u679c\u7248\u672c\u53d1\u751f\u66f4\u6539\uff0c\u5219\u66f4\u65b0\u64cd\u4f5c\u5c06\u88ab\u4e2d\u6b62\u3002 \u542f\u7528 MySQL \u4e50\u89c2\u9501 \u8981\u542f\u7528 MySQL \u4e2d\u7684\u4e50\u89c2\u9501\uff0c\u9700\u8981\u4f7f\u7528 READ_COMMITTED \u6216 REPEATABLE_READ \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u3002 1. READ_COMMITTED READ_COMMITTED \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u5141\u8bb8\u4e8b\u52a1\u8bfb\u53d6\u672a\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u4f46\u53ea\u80fd\u8bfb\u53d6\u5df2\u63d0\u4ea4\u7684\u6570\u636e\u3002\u5728\u4f7f\u7528\u8be5\u7ea7\u522b\u65f6\uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u4e8b\u52a1\u5728\u5f53\u524d\u4e8b\u52a1\u8bfb\u53d6\u6570\u636e\u540e\u66f4\u65b0\u4e86\u6570\u636e\uff0c\u5219\u5f53\u524d\u4e8b\u52a1\u5728\u66f4\u65b0\u6570\u636e\u65f6\u5c06\u5931\u8d25\u3002 2. REPEATABLE_READ REPEATABLE_READ \u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u6bd4 READ_COMMITTED \u7ea7\u522b\u66f4\u4e25\u683c\u3002\u5b83\u786e\u4fdd\u5728\u4e8b\u52a1\u6267\u884c\u671f\u95f4\uff0c\u6570\u636e\u4e0d\u4f1a\u88ab\u5176\u4ed6\u4e8b\u52a1\u4fee\u6539\u3002\u5982\u679c\u53e6\u4e00\u4e2a\u4e8b\u52a1\u66f4\u65b0\u4e86\u5f53\u524d\u4e8b\u52a1\u6b63\u5728\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u5219\u5f53\u524d\u4e8b\u52a1\u5728\u66f4\u65b0\u6570\u636e\u65f6\u5c06\u5931\u8d25\u3002 \u8981\u8bbe\u7f6e\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; \u767b\u5f55\u540e\u590d\u5236 \u9664\u4e86\u8bbe\u7f6e\u9694\u79bb\u7ea7\u522b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 WHERE \u5b50\u53e5\u4e2d\u7684\u7248\u672c\u5217\u6765\u5b9e\u73b0\u4e50\u89c2\u9501\u3002\u8fd9\u5c06\u68c0\u67e5\u7248\u672c\u5217\u662f\u5426\u81ea\u4e0a\u6b21\u8bfb\u53d6\u4ee5\u6765\u53d1\u751f\u66f4\u6539\uff0c\u5e76\u6839\u636e\u9700\u8981\u4e2d\u6b62\u66f4\u65b0\u64cd\u4f5c\u3002 \u793a\u4f8b SELECT * FROM table [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5535","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5535"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5535\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}