{"id":4930,"date":"2020-03-26T18:30:56","date_gmt":"2020-03-26T10:30:56","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4930"},"modified":"2020-03-26T18:30:56","modified_gmt":"2020-03-26T10:30:56","slug":"innodb-%e4%b8%ad-rrrepeatable-read-%e9%9a%94%e7%a6%bb%e7%ba%a7%e5%88%ab%e4%b8%8b%e4%b8%8d%e5%ad%98%e5%9c%a8%e5%b9%bb%e8%af%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4930","title":{"rendered":"InnoDB \u4e2d RR(Repeatable Read) \u9694\u79bb\u7ea7\u522b\u4e0b\u4e0d\u5b58\u5728\u5e7b\u8bfb\uff1f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4f17\u6240\u5468\u77e5\uff0cMySQL \u662f\u56fd\u5185\u4f7f\u7528\u6700\u591a\u7684\u6570\u636e\u5e93\uff0c\u62e5\u6709\u4f17\u591a\u7684\u7528\u6237\uff0c\u4f46\u662f\u5404\u65b9\u9762\u7684\u76f8\u5173\u6587\u7ae0\uff0c\u4e92\u76f8\u9a73\u8bba\uff01\u4eca\u5929\u6211\u4eec\u6765\u8bf4\u4e00\u8bf4\uff0cInnoDB \u4e2d RR(Repeatable Read) \u9694\u79bb\u7ea7\u522b\u4e0b\u5230\u5e95\u5b58\u4e0d\u5b58\u5728\u5e7b\u8bfb\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sql\">\u6807\u51c6 SQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6807\u51c6 SQL\uff0c\u6807\u51c6\u60c5\u51b5\u4e0b\uff0cRR(Repeatable Read) \u9694\u79bb\u7ea7\u522b\u4e0b\u80fd\u89e3\u51b3\u4e0d\u53ef\u91cd\u590d\u8bfb\uff08\u5f53\u884c\u4fee\u6539\uff09\u7684\u95ee\u9898\uff0c\u4f46\u662f\u4e0d\u80fd\u89e3\u51b3\u5e7b\u8bfb\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\uff0c\u5c31\u6709\u4e0d\u5c11\u4eba\u5f97\u51fa\u7ed3\u8bba\uff1aMySQL \u7684 InnoDB \u4e2d RR(Repeatable Read) \u9694\u79bb\u7ea7\u522b\u4e0b\u5b58\u5728\u5e7b\u8bfb\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6839\u636e\u6211\u524d\u9762\u7684\u90a3\u7bc7\uff0c<a href=\"https:\/\/www.xttblog.com\/?p=4926\">MySQL \u52a0\u9501\u5904\u7406\u5206\u6790<\/a>\u53ef\u4ee5\u5f97\u77e5\uff0c\u5bf9\u4e8e Innodb\uff0cRepeatable Read (RR) \u9488\u5bf9\u5f53\u524d\u8bfb\uff0cRR \u9694\u79bb\u7ea7\u522b\u4fdd\u8bc1\u5bf9\u8bfb\u53d6\u5230\u7684\u8bb0\u5f55\u52a0\u9501 (\u8bb0\u5f55\u9501)\uff0c\u540c\u65f6\u4fdd\u8bc1\u5bf9\u8bfb\u53d6\u7684\u8303\u56f4\u52a0\u9501\uff0c\u65b0\u7684\u6ee1\u8db3\u67e5\u8be2\u6761\u4ef6\u7684\u8bb0\u5f55\u4e0d\u80fd\u591f\u63d2\u5165 (\u95f4\u9699\u9501)\uff0c\u4e0d\u5b58\u5728\u5e7b\u8bfb\u73b0\u8c61\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u548c\u524d\u9762\u6807\u51c6\u60c5\u51b5\u4e0b\u5c31\u76f8\u4e92\u9a73\u8bba\u4e86\uff0c\u5230\u5e95\u662f\u600e\u4e48\u56de\u4e8b\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u5c11\u7f51\u53cb\u90fd\u6709\u8fd9\u6837\u7684\u7591\u60d1\uff01<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5f31\u5f31\u5730\u95ee\u4e00\u53e5\uff0c\u6211\u770b\u7684\u4e66\u91cc\u9762\u90fd\u8bf4\u7684\u662f RR \u9694\u79bb\u7ea7\u522b\u4e0d\u5141\u8bb8\u810f\u8bfb\u548c\u4e0d\u53ef\u91cd\u590d\u8bfb\uff0c\u4f46\u662f\u53ef\u4ee5\u5e7b\u8bfb\uff0c\u600e\u4e48\u548c\u4f5c\u8005\u8bf4\u7684\u4e0d\u4e00\u6837\u5462\uff1f<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u56e0\u4e3a mysql innodb \u5f15\u64ce\u7684\u5b9e\u73b0\uff0c\u8ddf\u6807\u51c6\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 MySQL 5.6 \u7684\u7248\u672c\u4e2d\uff0c\u5b98\u65b9<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/set-transaction.html#isolevel_repeatable-read\">isolevel_repeatable-read<\/a>\u6709\u8fd9\u6837\u4e00\u6bb5\u89e3\u91ca\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>For locking reads (SELECT with FOR UPDATE or LOCK IN SHARE MODE), UPDATE, and DELETE statements, locking depends on whether the statement uses a unique index with a unique search condition, or a range-type search condition. For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it. For other search conditions, InnoDB locks the index range scanned, using gap locks or next-key locks to block insertions by other sessions into the gaps covered by the range.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ffb\u8bd1\u8fc7\u6765\u7684\u5927\u81f4\u610f\u601d\u662f\u8bf4\uff0c\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u5982\u679c\u67e5\u8be2\u6761\u4ef6\u80fd\u4f7f\u7528\u4e0a\u552f\u4e00\u7d22\u5f15\uff0c\u6216\u8005\u662f\u4e00\u4e2a\u552f\u4e00\u7684\u67e5\u8be2\u6761\u4ef6\uff0c\u90a3\u4e48\u4ec5\u52a0\u884c\u9501\uff0c\u5982\u679c\u662f\u4e00\u4e2a\u8303\u56f4\u67e5\u8be2\uff0c\u90a3\u4e48\u5c31\u4f1a\u7ed9\u8fd9\u4e2a\u8303\u56f4\u52a0\u4e0a gap \u9501\u6216\u8005 next-key\u9501 (\u884c\u9501 + gap \u9501)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8fd9\u53e5\u8bdd\u7684\u7406\u89e3\u6765\u770b\uff0c\u548c\u6587\u7ae0\u91cc\u7684\u89e3\u91ca\u4e00\u6837\uff0c\u7531\u4e8e RR \u7ea7\u522b\u5bf9\u4e8e\u8303\u56f4\u4f1a\u52a0 GAP \u9501\uff0c\u8fd9\u4e2a\u548c sql \u7684\u6807\u51c6\u662f\u6709\u4e00\u4e9b\u5dee\u5f02\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\uff0c\u901a\u8fc7 MySQL \u7684\u6e90\u7801\u4e5f\u53ef\u4ee5\u770b\u51fa InnoDB \u4e2d RR(Repeatable Read) \u9694\u79bb\u7ea7\u522b\u4e0b\u662f\u5426\u5b58\u5728\u5e7b\u8bfb\u95ee\u9898\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u9645\u4e0a\uff0c\u6b63\u5982\u5b98\u65b9\u6240\u8bf4\uff0cInnodb \u7684 RR \u9694\u79bb\u754c\u522b\u5bf9\u8303\u56f4\u4f1a\u52a0\u4e0a GAP\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\u662f\u4e0d\u4f1a\u5b58\u5728\u5e7b\u8bfb\u3002\u90a3\u5982\u679c\u6709\u201c\u4f8b\u5916\u201d\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u518d\u6765\u770b\u770b MySQL \u5b98\u7f51\u4e0a\u7684\u53e6\u4e00\u6bb5\u89e3\u91ca\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>By default, InnoDB operates in REPEATABLE READ transaction isolation level and with the innodb<em>locks<\/em>unsafe<em>for<\/em>binlog system variable disabled. In this case, InnoDB uses next-key locks for searches and index scans, which prevents phantom rows (see Section 13.6.8.5, \u201cAvoiding the Phantom Problem Using Next-Key Locking\u201d).<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ffb\u8bd1\u8fc7\u6765\uff0c\u5927\u81f4\u7684\u610f\u601d\u662f\u8bf4\uff1a\u5f53\u9694\u79bb\u7ea7\u522b\u662f\u53ef\u91cd\u590d\u8bfb\uff0c\u4e14\u7981\u7528 innodb<em>locks<\/em>unsafe<em>for<\/em>binlog \u7684\u60c5\u51b5\u4e0b\uff0c\u5728\u641c\u7d22\u548c\u626b\u63cf index \u7684\u65f6\u5019\u4f7f\u7528\u7684 next-key locks \u53ef\u4ee5\u907f\u514d\u5e7b\u8bfb\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MySQL \u7684 rr \u7ea7\u522b\u89e3\u51b3\u4e86\u5927\u90e8\u5206\u5e7b\u8bfb\u95ee\u9898\uff0c\u4f46\u6709\u51e0\u4e2a\u4f8b\u5916\u3002\u5177\u4f53\u53ef\u4ee5\u770bhttps:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/innodb-consistent-read.html<\/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=4930\">InnoDB \u4e2d RR(Repeatable Read) \u9694\u79bb\u7ea7\u522b\u4e0b\u4e0d\u5b58\u5728\u5e7b\u8bfb\uff1f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5\uff0cMySQL \u662f\u56fd\u5185\u4f7f\u7528\u6700\u591a\u7684\u6570\u636e\u5e93\uff0c\u62e5\u6709\u4f17\u591a\u7684\u7528\u6237\uff0c\u4f46\u662f\u5404\u65b9\u9762\u7684\u76f8\u5173\u6587\u7ae0\uff0c\u4e92\u76f8\u9a73\u8bba\uff01\u4eca\u5929\u6211\u4eec\u6765\u8bf4\u4e00\u8bf4 [&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":[2097,1674,1968,1683,1682],"class_list":["post-4930","post","type-post","status-publish","format-standard","hentry","category-sql","tag-2pl","tag-innodb","tag-mvcc","tag-next-key-locks","tag-gap-locks"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4930","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=4930"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4930\/revisions"}],"predecessor-version":[{"id":4931,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4930\/revisions\/4931"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}