{"id":4336,"date":"2019-06-16T09:48:28","date_gmt":"2019-06-16T01:48:28","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=4336"},"modified":"2019-06-16T10:22:44","modified_gmt":"2019-06-16T02:22:44","slug":"mysql-%e4%b8%bb%e9%94%ae%e7%b4%a2%e5%bc%95%e5%9c%a8-rr-%e5%92%8c-rc-%e9%9a%94%e7%a6%bb%e7%ba%a7%e5%88%ab%e4%b8%8b%e7%ad%89%e5%80%bc%e6%9f%a5%e8%af%a2%e7%9a%84%e5%8a%a0%e9%94%81%e6%83%85%e5%86%b5","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4336","title":{"rendered":"MySQL \u4e3b\u952e\u7d22\u5f15\u5728 RR \u548c RC \u9694\u79bb\u7ea7\u522b\u4e0b\u7684\u52a0\u9501\u60c5\u51b5\u603b\u7ed3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u7fa4\u91cc\u4e0d\u5c11\u4eba\u8ba8\u8bba\u4e86\u4e00\u4e9b\u9ad8\u8d28\u91cf\u7684\u95ee\u9898\u3002\u6211\u4eca\u5929\u62bd\u65f6\u95f4\u7ed9\u5927\u5bb6\u603b\u7ed3\u4e00\u4e2a MySQL InnoDB \u5b58\u50a8\u5f15\u64ce\u5404\u79cd\u4e0d\u540c SQL \u60c5\u51b5\u4e0b\uff0c\u52a0\u884c\u9501\u3001\u95f4\u9699\u9501\u3001next-key lock \u505a\u4e00\u4e2a\u603b\u7ed3\u3002\u5982\u679c\u6709\u9519\u8bef\u7684\u5730\u65b9\uff0c\u8bf7\u5927\u5bb6\u6307\u6b63\uff01<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcly1g42r43acmhj20jn09et9g.jpg\" alt=\"\" \/><figcaption>MySQL\u7684\u6240\u6709\u9501<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u8bb2\u6e05\u695a\u76f8\u5173\u52a0\u9501\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u5148\u6765\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u9a8c\u8bc1\u7528\u7684\u8868\u3002\u7ed3\u6784\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE `xttblog` (\n  `id` bigint(20) NOT NULL,\n  `name` varchar(16) COLLATE utf8mb4_bin DEFAULT NULL,\n  `idcard` bigint(20) NOT NULL,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u63d2\u5165\u51e0\u6761\u6d4b\u8bd5\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>insert into `xttblog` values(1,\u2019\u4e1a\u4f59\u8349\u2019,100),(5,'xttblog\u2019,200),\n(8,\u2019xttblog\u2019,300),(10,\u2019\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\u2019,400);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u5148\u6765\u89e3\u91ca\u4e00\u4e0b\uff0cRR \u548c RC\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RR \u9694\u79bb\u7ea7\u522b\u5c31\u662f\uff0c\u53ef\u91cd\u590d\u8bfb\u3002REPEATABLE READ\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RC \u9694\u79bb\u7ea7\u522b\u5c31\u662f\uff0c\u8bfb\u5df2\u63d0\u4ea4\u3002READ COMMITTED\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u641e\u660e\u767d RR \u548c RC \u540e\uff0c\u6211\u4eec\u5148\u6765\u770b\u7b2c\u4e00\u79cd\u60c5\u51b5\u3002\u67e5\u8be2\u6761\u4ef6\u662f\u805a\u7c07\u7d22\u5f15\uff0c\u4e5f\u5c31\u662f\u4e3b\u952e\u7d22\u5f15\u7684\u7cbe\u786e\u5339\u914d\u60c5\u51b5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5728id=1\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0X\u9501\nupdate `xttblog` set name=\u2018x' where id=1;  \n\/\/ \u5728id=1\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0S\u9501\nselect * from `xttblog` where id=1 lock in share mode;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c SQL \u662f\u7cbe\u786e\u67e5\u8be2\uff0c\u4e0d\u7ba1\u662f RR \u8fd8\u662f RC \u9694\u79bb\u7ea7\u522b\u4e0b\uff0c\u90fd\u4f1a\u5728\u547d\u4e2d\u7684\u7d22\u5f15\u4e0a\u52a0 record lock\uff08\u884c\u9501\uff09\u3002X \u9501\u662f\u6392\u5b83\u9501\uff08Exclusive Lock\uff09\uff0c\u4e5f\u6709\u4eba\u79f0\u4f5c\u662f\u5199\u9501\u3002S \u9501\u662f\u5171\u4eab\u9501\uff08Shared Lock\uff09\uff0c\u4e5f\u6709\u4eba\u79f0\u4f5c\u662f\u8bfb\u9501\u3002\u4e0d\u7ba1\u662f X \u9501\uff0c\u8fd8\u662f S \u9501\uff0c\u90fd\u662f\u52a0\u5728\u884c\u4e0a\u7684\u3002\u6ce8\u610f\uff0c\u884c\u9501\u4e0d\u662f\u52a0\u5728\u8bb0\u5f55\u4e0a\u7684\uff0c\u800c\u662f\u52a0\u5728\u7d22\u5f15\u4e0a\u7684\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u662f\u8303\u56f4\u67e5\u8be2\uff0c\u90a3\u4e48\u5728\u5728 RC \u9694\u79bb\u7ea7\u522b\u4e0b\uff0c\u4f1a\u5728\u6240\u6709\u547d\u4e2d\u7684\u884c\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0 record locks\uff08\u9501\u884c\uff09\u3002\u56e0\u4e3a RC \u7ea7\u522b\u4e0b\u662f\u6ca1\u6709\u95f4\u9699\u9501\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5728id=8\u548c10\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0X\u9501\nupdate `xttblog` set name='\u6781\u5ba2\u65f6\u95f4\u8fd4\u73b024' where id&gt;7;  \n\/\/ \u5728id=1\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0X\u9501\nupdate `xttblog` set name='\u6781\u5ba2\u65f6\u95f4\u8fd4\u73b024' where id&lt;=1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e09\u79cd\u60c5\u51b5\uff0c\u5728 RR \u9694\u79bb\u7ea7\u522b\u4e0b\uff0c\u4f1a\u5728\u6240\u6709\u547d\u4e2d\u7684\u884c\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0 next-key locks\uff08\u9501\u4f4f\u884c\u548c\u95f4\u9699\uff09\u3002\u6700\u540e\u547d\u4e2d\u7684\u7d22\u5f15\u7684\u540e\u4e00\u6761\u8bb0\u5f55\uff0c\u4e5f\u4f1a\u88ab\u52a0\u4e0a next-key lock\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5728id=8\u300110(\u3001+\u221e)\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0X\u9501\n\/\/ \u5728(5,8)(8,10)(10,+\u221e)\u52a0gap lock\nupdate `xttblog` set name=\u2018x' where id&gt;7;  \n\/\/ \u5728id=1\u30015\u7684\u805a\u7c07\u7d22\u5f15\u4e0a\u52a0X\u9501\n\/\/ \u5728(-\u221e,1)(1,5)\u52a0gap lock\nupdate `xttblog` set name=\u2018x' where id&lt;=1;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u56db\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u4e3a\u7a7a\uff0c\u5373\u6ca1\u6709\u547d\u4e2d\u4efb\u4f55\u805a\u65cf\u7d22\u5f15\uff0c\u90a3\u4e48\uff0c\u5728 RC \u9694\u79bb\u7ea7\u522b\u4e0b\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u4f1a\u9501\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e94\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u67e5\u8be2\u7ed3\u679c\u4e3a\u7a7a\uff0c\u5373\u6ca1\u6709\u547d\u4e2d\u4efb\u4f55\u805a\u65cf\u7d22\u5f15\uff0c\u90a3\u4e48\uff0c\u5728 RR \u9694\u79bb\u7ea7\u522b\u4e0b\uff0c\u4f1a\u9501\u4f4f\u67e5\u8be2\u76ee\u6807\u6240\u5728\u7684\u95f4\u9699\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5728(1,5)\u52a0gap lock\nupdate `xttblog` set name=\u2018x' where id=2;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\uff0c\u5bf9\u4e8e\u4e3b\u952e\u7d22\u5f15\u52a0\u9501\u7684\u573a\u666f\u57fa\u672c\u4e0a\u5df2\u7ecf\u5168\u90e8\u8986\u76d6\u5230\u4e86\u3002\u5982\u679c\u4f60\u8fd8\u6709\u4ec0\u4e48\u7591\u95ee\u60f3\u8ba8\u8bba\u7684\uff0c\u8bf7\u52a0\u6211\u5fae\u4fe1\uff1axttblog\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">RR \u9694\u79bb\u7ea7\u522b\u5b9e\u73b0\u8d77\u6765\uff0c\u8981\u6bd4 RC \u590d\u6742\u7684\u591a\u3002\u7ed9\u4f60\u7559\u4e00\u4e2a\u95ee\u9898\uff0cMySQL \u662f\u51fa\u4e8e\u4ec0\u4e48\u6837\u7684\u8003\u8651\uff0c\u9ed8\u8ba4\u662f\u91c7\u7528 RR \u9694\u79bb\u7ea7\u522b\uff1f<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4336\">MySQL \u4e3b\u952e\u7d22\u5f15\u5728 RR \u548c RC \u9694\u79bb\u7ea7\u522b\u4e0b\u7684\u52a0\u9501\u60c5\u51b5\u603b\u7ed3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u7fa4\u91cc\u4e0d\u5c11\u4eba\u8ba8\u8bba\u4e86\u4e00\u4e9b\u9ad8\u8d28\u91cf\u7684\u95ee\u9898\u3002\u6211\u4eca\u5929\u62bd\u65f6\u95f4\u7ed9\u5927\u5bb6\u603b\u7ed3\u4e00\u4e2a MySQL InnoDB \u5b58\u50a8\u5f15\u64ce\u5404\u79cd\u4e0d\u540c  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[83,1683,1148,1931,1682],"class_list":["post-4336","post","type-post","status-publish","format-standard","hentry","category-sql","tag-mysql","tag-next-key-locks","tag-1148","tag-1931","tag-gap-locks"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4336"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4336\/revisions"}],"predecessor-version":[{"id":4338,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4336\/revisions\/4338"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}