{"id":2090,"date":"2023-04-01T21:57:37","date_gmt":"2023-04-01T13:57:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2090"},"modified":"2023-04-06T14:48:31","modified_gmt":"2023-04-06T06:48:31","slug":"complete-summary-of-mysql-innodb-locks","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/complete-summary-of-mysql-innodb-locks\/","title":{"rendered":"\u5173\u4e8e MySQL InnoDB \u9501\u7684\u8d85\u5168\u603b\u7ed3"},"content":{"rendered":"<p>\u51e0\u4e2a\u6708\u4e4b\u524d\uff0c\u5f00\u59cb\u6df1\u5165\u5b66\u4e60 MySQL \u3002\u8bf4\u8d77\u6570\u636e\u5e93\uff0c\u5e76\u53d1\u63a7\u5236\u662f\u5176\u4e2d\u5f88\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u4e8e\u662f\uff0c\u5c31\u8fd9\u6837\u5f00\u8d77\u4e86 MySQL \u9501\u7684\u5b66\u4e60\uff0c\u968f\u7740\u5b66\u4e60\u7684\u6df1\u5165\uff0c\u53d1\u73b0\u60f3\u8981\u66f4\u597d\u7684\u7406\u89e3\u9501\uff0c\u9700\u8981\u4e86\u89e3 MySQL \u4e8b\u52a1\uff0c\u6570\u636e\u5e95\u5c42\u7684\u5b58\u50a8\u65b9\u5f0f\uff0cMySQL \u7684\u6267\u884c\u6d41\u7a0b\uff0c\u7279\u522b\u662f\u7d22\u5f15\u7684\u9009\u62e9\u7b49\u3002<\/p>\n<h2>InnoDB \u9501\u4e00\u89c8<\/h2>\n<p>\u5148\u4ece MySQL \u5b98\u7f51\u7684\u9501\u4ecb\u7ecd\u5f00\u59cb\uff0c\u6765\u9010\u4e00\u8ba4\u8bc6\u4e0b\u8fd9\u4e9b\u8ba9\u6211\u4eec\u591c\u4e0d\u80fd\u5bd0\u7684\u5c0f\u738b\u516b\u86cb\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/innodb-locking.html#innodb-gap-locks\">https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/innodb-locking.html#innodb-gap-locks<\/a><\/p>\n<p><!-- more --><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL-InnoDB\u9501-\u5b98\u65b9\u6587\u6863.png\" alt=\"MySQL-InnoDB\u9501-\u5b98\u65b9\u6587\u6863\" \/><\/p>\n<h3>Shared and Exclusive Locks<\/h3>\n<p>\u8fd9\u4e8c\u4f4d\u6b63\u5f0f\u79f0\u547c\u5462\uff0c\u5c31\u662f\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\uff0c\u5176\u5b9e\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u8bfb\u9501\u548c\u5199\u9501\u3002\u5b83\u4eec\u4e4b\u95f4\u7684\u4e92\u65a5\u89c4\u5219\uff0c\u60f3\u5fc5\u90fd\u6e05\u695a\uff0c\u5c31\u4e0d\u8d58\u8ff0\u4e86\u3002\u4f46\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0c<strong>\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\u662f\u6807\u51c6\u7684\u5b9e\u73b0\u884c\u7ea7\u522b\u7684\u9501<\/strong>\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5f53\u7ed9 select \u8bed\u53e5\u5e94\u7528<code>lock in share mode<\/code>\u6216\u8005<code>for update<\/code>\uff0c\u6216\u8005\u66f4\u65b0\u67d0\u6761\u8bb0\u5f55\u65f6\uff0c\u52a0\u7684\u90fd\u662f\u884c\u7ea7\u522b\u7684\u9501\u3002<\/p>\n<p>\u4e0e\u884c\u7ea7\u522b\u7684\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\u7c7b\u4f3c\u7684\uff0c\u8fd8\u6709\u8868\u7ea7\u522b\u7684\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\u3002\u5982<code>LOCK TABLES ... WRITE\/READ<\/code>\u7b49\u547d\u4ee4\uff0c\u5b9e\u73b0\u7684\u5c31\u662f\u8868\u7ea7\u9501\u3002<\/p>\n<h3>Intention Locks<\/h3>\n<p>\u5728 InnoDB \u4e2d\u662f\u652f\u6301\u591a\u7c92\u5ea6\u7684\u9501\u5171\u5b58\u7684\uff0c\u6bd4\u5982\u8868\u9501\u548c\u884c\u9501\u3002\u800c Intention Locks &#8211; \u610f\u5411\u9501\uff0c\u5c31\u662f\u8868\u7ea7\u9501\u3002\u548c\u884c\u7ea7\u9501\u4e00\u6837\uff0c\u610f\u5411\u9501\u5206\u4e3a<code>intention shared lock (IS)<\/code>\u548c<code>intention exclusive lock (IX)<\/code>\u3002\u4f46\u6709\u8da3\u7684\u662f\uff0cIS \u548c IX \u4e4b\u95f4\u5e76\u4e0d\u4e92\u65a5\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ef\u4ee5\u540c\u65f6\u7ed9\u4e0d\u540c\u7684\u4e8b\u52a1\u52a0\u4e0a IS \u548c IX\u3002\u517c\u5bb9\u6027\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL-InnoDB\u9501-\u610f\u5411\u9501\u517c\u5bb9\u6027.png\" alt=\"MySQL-InnoDB\u9501-\u610f\u5411\u9501\u517c\u5bb9\u6027\" \/><\/p>\n<p>\u8fd9\u65f6\u5c31\u4ea7\u751f\u7591\u95ee\u4e86\uff0c\u90a3\u5b83\u4fe9\u5b58\u5728\u7684\u610f\u4e49\u662f\u4ec0\u4e48\uff1f\u4f5c\u7528\u5c31\u662f\uff0c\u548c\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\u4e92\u65a5\u3002\u6ce8\u610f\u4e0b\uff0c<strong>\u8fd9\u91cc\u6307\u7684\u662f\u8868\u7ea7\u522b\u7684\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\uff0c\u548c\u884c\u7ea7\u522b\u6ca1\u6709\u5173\u7cfb\uff01<\/strong><\/p>\n<p>\u5b98\u7f51\u4e2d\u7ed9\u4e86\u8fd9\u6837\u4e00\u6bb5\u89e3\u91ca\uff1a<\/p>\n<pre><code>The main purpose of intention locks is to show that someone is locking a row, or going to lock a row in the table.<\/code><\/pre>\n<p>\u610f\u5411\u9501\u7684\u76ee\u7684\u5c31\u662f\u8868\u660e\u6709\u4e8b\u52a1\u6b63\u5728\u6216\u8005\u5c06\u8981\u9501\u4f4f\u67d0\u4e2a\u8868\u4e2d\u7684\u884c\u3002\u60f3\u8c61\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u6211\u4eec\u4f7f\u7528<code>select * from t where id=0 for update;<\/code>\u5c06 id=0 \u8fd9\u884c\u52a0\u4e0a\u4e86\u5199\u9501\u3002\u5047\u8bbe\u540c\u65f6\uff0c\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u60f3\u8981\u53d1\u8d77<code>LOCK TABLES ... WRITE<\/code>\u9501\u8868\u7684\u64cd\u4f5c\uff0c\u8fd9\u65f6\u5982\u679c\u6ca1\u6709\u610f\u5411\u9501\u7684\u8bdd\uff0c\u5c31\u9700\u8981\u53bb\u4e00\u884c\u884c\u68c0\u6d4b\u662f\u5426\u6240\u5728\u8868\u4e2d\u7684\u67d0\u884c\u662f\u5426\u5b58\u5728\u5199\u9501\uff0c\u4ece\u800c\u5f15\u53d1\u51b2\u7a81\uff0c\u6548\u7387\u592a\u4f4e\u3002\u76f8\u53cd\u6709\u610f\u5411\u9501\u7684\u8bdd\uff0c\u5728\u53d1\u8d77<code>lock in share mode<\/code>\u6216\u8005<code>for update<\/code>\u524d\u5c31\u4f1a\u81ea\u52a8\u52a0\u4e0a\u610f\u5411\u9501\uff0c\u8fd9\u6837\u68c0\u6d4b\u8d77\u6765\u5c31\u65b9\u4fbf\u591a\u4e86\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u4e2d\uff0c\u624b\u52a8\u9501\u8868\u7684\u60c5\u51b5\u5e76\u4e0d\u5e38\u89c1\uff0c\u6240\u4ee5\u610f\u5411\u9501\u5e76\u4e0d\u5e38\u7528\u3002\u7279\u522b\u662f\u4e4b\u540e MySQL \u5f15\u5165\u4e86 MDL \u9501\uff0c\u89e3\u51b3\u4e86 DML \u548c DDL \u51b2\u7a81\u7684\u95ee\u9898\uff0c\u610f\u5411\u9501\u5c31\u66f4\u4e0d\u88ab\u63d0\u8d77\u6765\u4e86\u3002<\/p>\n<h3>Record Locks<\/h3>\n<p>record lock \uff0c\u5c31\u662f\u5e38\u8bf4\u7684\u884c\u9501\u3002InnoDB \u4e2d\uff0c<strong>\u8868\u90fd\u4ee5\u7d22\u5f15\u7684\u5f62\u5f0f\u5b58\u5728<\/strong>\uff0c\u6bcf\u4e00\u4e2a\u7d22\u5f15\u5bf9\u5e94\u4e00\u9897 B+ \u6811\uff0c\u8fd9\u91cc\u7684\u884c\u9501\u9501\u7684\u5c31\u662f B+ \u4e2d\u7684\u7d22\u5f15\u8bb0\u5f55\u3002\u4e4b\u524d\u63d0\u5230\u7684\u5171\u4eab\u9501\u548c\u6392\u4ed6\u9501\uff0c\u5c31\u662f\u5c06\u9501\u52a0\u5728\u8fd9\u91cc\u3002<\/p>\n<pre><code class=\"language-sql\">RECORD LOCKS space id 251 page no 3 n bits 80 index PRIMARY of table `my_test`.`t` trx id 1339983 lock_mode X locks rec but not gap\nRecord lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0\n 0: len 4; hex 8000000f; asc     ;;\n 1: len 6; hex 0000001471fa; asc     q ;;\n 2: len 7; hex 5e00000a812aff; asc ^    * ;;\n 3: len 4; hex 8000000f; asc     ;;\n 4: len 4; hex 8000000f; asc     ;;<\/code><\/pre>\n<h3>Gap Locks<\/h3>\n<p>Gap Locks, \u95f4\u9699\u9501\u9501\u4f4f\u7684\u662f\u7d22\u5f15\u8bb0\u5f55\u95f4\u7684\u7a7a\u9699\uff0c\u662f\u4e3a\u4e86\u89e3\u51b3<strong>\u5e7b\u8bfb\u95ee\u9898<\/strong>\u88ab\u5f15\u5165\u7684\u3002\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\uff0c\u95f4\u9699\u9501\u548c\u95f4\u9699\u9501\u672c\u8eab\u4e4b\u95f4\u5e76\u4e0d\u51b2\u7a81\uff0c\u4ec5\u4ec5\u548c\u63d2\u5165\u8fd9\u4e2a\u64cd\u4f5c\u53d1\u751f\u51b2\u7a81\u3002<\/p>\n<pre><code class=\"language-sql\">TABLE LOCK table `my_test`.`t` trx id 1339983 lock mode IX\nRECORD LOCKS space id 251 page no 4 n bits 80 index c of table `my_test`.`t` trx id 1339983 lock_mode X locks gap before rec\nRecord lock, heap no 7 PHYSICAL RECORD: n_fields 2; compact format; info bits 0\n 0: len 4; hex 80000019; asc     ;;\n 1: len 4; hex 80000019; asc     ;;<\/code><\/pre>\n<h3>Next-Key lock<\/h3>\n<p>next-key lock \u662f<strong>\u884c\u9501\uff08Record\uff09\u548c\u95f4\u9699\u9501<\/strong>\u7684\u5e76\u96c6\u3002\u5728 RR \u7ea7\u522b\u4e0b\uff0cInnoDB \u4f7f\u7528 next-key \u9501\u8fdb\u884c\u6811\u641c\u7d22\u548c\u7d22\u5f15\u626b\u63cf\u3002\u8bb0\u4f4f\u8fd9\u53e5\u8bdd\uff0c\u52a0\u9501\u7684\u57fa\u672c\u5355\u4f4d\u662f next-key lock<\/p>\n<pre><code class=\"language-sql\">RECORD LOCKS space id 251 page no 4 n bits 80 index c of table `my_test`.`t` trx id 1339983 lock_mode X\nRecord lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0\n 0: len 4; hex 8000000a; asc     ;;\n 1: len 4; hex 8000000a; asc     ;;<\/code><\/pre>\n<h2>\u6253\u5f00 InnoDB \u9501\u65e5\u5fd7<\/h2>\n<p>\u6253\u5f00 InnoDB \u7684\u9501\u65e5\u5fd7\uff0c\u5728\u9047\u5230\u4e00\u4e9b\u5947\u8469\u7684\u73b0\u8c61\u65f6\uff0c\u4f1a\u5e2e\u52a9\u6211\u4eec\u786e\u5b9a\u9501\u8303\u56f4\u3002<\/p>\n<pre><code class=\"language-sql\">SET GLOBAL innodb_status_output=ON;\nSET GLOBAL innodb_status_output_locks=ON;<\/code><\/pre>\n<h2>\u52a0\u9501\u89c4\u5219<\/h2>\n<p>\u8be5\u52a0\u9501\u539f\u5219\u7531\u6797\u6653\u658c\u8001\u5e08\u5237\u4ee3\u7801\u540e\u603b\u7ed3\uff0c\u7b26\u5408\u7684\u7248\u672c\u5982\u4e0b\uff1a<\/p>\n<p>MySQL \u7248\u672c\uff1a5.x &#8211; 5.7.24\uff0c 8.0 &#8211; 8.0.13. \u6211\u662f 5.7.27 \u4e5f\u672a\u53d1\u73b0\u95ee\u9898\u3002<\/p>\n<p>\u89c4\u5219\u5305\u62ec\uff1a\u4e24\u4e2a\u201c\u539f\u5219\u201d\u3001\u4e24\u4e2a\u201c\u4f18\u5316\u201d\u548c\u4e00\u4e2a\u201cbug\u201d\u3002<\/p>\n<ul>\n<li>\u539f\u52191\uff1a\u52a0\u9501\u7684\u57fa\u672c\u5355\u4f4d\u662f next-key lock\u3002next-key lock \u662f\u524d\u5f00\u540e\u95ed\u533a\u95f4<\/li>\n<li>\u539f\u52192\uff1a\u67e5\u627e\u8fc7\u7a0b\u4e2d\u8bbf\u95ee\u5230\u7684\u5bf9\u8c61\u624d\u4f1a\u52a0\u9501<\/li>\n<li>\u4f18\u53161\uff1a\u7d22\u5f15\u4e0a\u7684\u7b49\u503c\u67e5\u8be2\uff0c\u7ed9\u552f\u4e00\u7d22\u5f15\u52a0\u9501\u7684\u65f6\u5019\uff0cnext-key lock \u9000\u5316\u4e3a\u884c\u9501<\/li>\n<li>\u4f18\u53162\uff1a\u7d22\u5f15\u4e0a\u7684\u7b49\u503c\u67e5\u8be2\uff0c\u5411\u53f3\u904d\u5386\u65f6\u4e14\u6700\u540e\u4e00\u4e2a\u503c\u4e0d\u6ee1\u8db3\u7b49\u503c\u6761\u4ef6\u7684\u65f6\u5019\uff0cnext-key lock \u9000\u5316\u4e3a\u95f4\u9699\u9501<\/li>\n<li>\u4e00\u4e2a bug\uff1a\u552f\u4e00\u7d22\u5f15\u4e0a\u7684\u8303\u56f4\u67e5\u8be2\u4f1a\u8bbf\u95ee\u5230\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684\u7b2c\u4e00\u4e2a\u503c\u4e3a\u6b62<\/li>\n<\/ul>\n<p>\u89e3\u91ca\u4e0b\u5bb9\u6613\u7406\u89e3\u9519\u8bef\u7684\u5730\u65b9\uff1a<\/p>\n<ol>\n<li>\u5bf9\u4f18\u5316 2 \u7684\u8bf4\u660e\uff1a<\/li>\n<\/ol>\n<p>\u4ece\u7b49\u503c\u67e5\u8be2\u7684\u503c\u5f00\u59cb\uff0c\u5411\u53f3\u904d\u5386\u5230\u7b2c\u4e00\u4e2a\u4e0d\u6ee1\u8db3\u7b49\u503c\u6761\u4ef6\u8bb0\u5f55\u7ed3\u675f\uff0c\u7136\u540e\u5c06\u4e0d\u6ee1\u8db3\u6761\u4ef6\u8bb0\u5f55\u7684 next-key \u9000\u5316\u4e3a\u95f4\u9699\u9501\u3002<\/p>\n<ol start=\"2\">\n<li>\u7b49\u503c\u67e5\u8be2\u548c\u904d\u5386\u6709\u4ec0\u4e48\u5173\u7cfb\uff1f<\/li>\n<\/ol>\n<p>\u5728\u5206\u6790\u52a0\u9501\u884c\u4e3a\u65f6\uff0c\u4e00\u5b9a\u8981\u4ece\u7d22\u5f15\u7684\u6570\u636e\u7ed3\u6784\u5f00\u59cb\u3002\u901a\u8fc7\u6811\u641c\u7d22\u7684\u65b9\u5f0f\u5b9a\u4f4d\u7d22\u5f15\u8bb0\u5f55\u65f6\uff0c\u7528\u7684\u662f&quot;\u7b49\u503c\u67e5\u8be2&quot;\uff0c\u800c\u904d\u5386\u5bf9\u5e94\u7684\u662f\u5728\u8bb0\u5f55\u4e0a\u5411\u524d\u6216\u5411\u540e\u626b\u63cf\u7684\u8fc7\u7a0b\u3002<\/p>\n<h2>\u5e94\u7528\u573a\u666f<\/h2>\n<p>\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE `t` (\n  `id` int(11) NOT NULL,\n  `c` int(11) DEFAULT NULL,\n  `d` int(11) DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  KEY `c` (`c`)\n) ENGINE=InnoDB;\n\ninsert into t values(0,0,0),(5,5,5),\n(10,10,10),(15,15,15),(20,20,20),(25,25,25);<\/code><\/pre>\n<h3>\u573a\u666f1\uff1a\u4e3b\u952e\u7d22\u5f15\u7b49\u503c\u95f4\u6b47\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/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 d=d+1 where id=7;<\/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;\">insert into t values(8,8,8);<\/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;\">\u88ab\u963b\u585e<\/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;\">update t set d=d+1 where id=10;<\/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;\">\u67e5\u8be2\u6b63\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d id \u5217\u4e3a\u4e3b\u952e\u7d22\u5f15\uff0c\u5e76\u4e14 id=7 \u7684\u884c\u5e76\u4e0d\u5b58\u5728\u3002<\/p>\n<p>\u5bf9\u4e8e Session A \u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u539f\u52191\uff0c\u52a0\u9501\u7684\u5355\u4f4d\u662f next-key, \u56e0\u4e3a id=7 \u5728 5 &#8211; 10 \u95f4\uff0cnext-key \u9ed8\u8ba4\u662f\u5de6\u5f00\u53f3\u95ed\u3002\u6240\u4ee5\u8303\u56f4\u662f (5,10]<\/li>\n<li>\u6839\u636e\u4f18\u53162\uff0c\u4f46\u56e0\u4e3a id=7 \u662f\u7b49\u503c\u67e5\u8be2\uff0c\u5230 id=10 \u7ed3\u675f\u3002next-key \u9000\u5316\u6210\u95f4\u9699\u9501 (5,10)<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e Session B \u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>\u63d2\u5165\u64cd\u4f5c\u4e0e\u95f4\u9699\u9501\u51b2\u7a81\uff0c\u6240\u4ee5\u5931\u8d25<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e Session C \u6765\u8bf4\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u539f\u52191\uff0cnext-key \u52a0\u9501 (5,10]<\/li>\n<li>\u6839\u636e\u4f18\u53161\uff1a\u7ed9\u552f\u4e00\u7d22\u5f15\u52a0\u9501\u65f6\uff0c\u9000\u5316\u6210\u884c\u9501\u3002\u8303\u56f4\u53d8\u4e3a\uff1aid=10 \u7684\u884c\u9501<\/li>\n<li>Session C \u548c Session A (5,10) \u5e76\u4e0d\u8d77\u51b2\u7a81\uff0c\u6240\u4ee5\u6210\u529f<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u770b\u51fa\uff0c\u884c\u9501\u548c\u95f4\u9699\u9501\u90fd\u662f\u6709 next-key \u9501\u6ee1\u8db3\u4e00\u5b9a\u540e\u6761\u4ef6\u540e\u8f6c\u6362\u7684\uff0c\u52a0\u9501\u7684\u9ed8\u8ba4\u5355\u4f4d\u662f next-key<\/p>\n<h3>\u573a\u666f2\uff1a\u975e\u552f\u4e00\u7d22\u5f15\u7b49\u503c\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/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;\">select id from t where c=5 lock in share mode;<\/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;\">update t set d=d+1 where id=5;<\/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;\">\u67e5\u8be2\u6b63\u5e38<\/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;\">insert into t values(7,7,7);<\/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;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u6ce8\u51e0\u70b9\uff1ac\u4e3a\u975e\u552f\u4e00\u7d22\u5f15\uff0c\u67e5\u8be2\u7684\u5b57\u6bb5\u4ec5\u6709 id\uff0c<code>lock in share mode<\/code>\u7ed9\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u52a0\u4e0a\u8bfb\u9501\u3002<\/p>\n<p>Session A\uff1a<\/p>\n<ul>\n<li>c=5\uff0c\u7b49\u503c\u67e5\u8be2\u4e14\u503c\u5b58\u5728\u3002\u5148\u52a0 next-key \u9501\uff0c\u8303\u56f4\u4e3a (0,5]<\/li>\n<li>\u7531\u4e8e c \u662f\u666e\u901a\u7d22\u5f15\uff0c\u56e0\u6b64\u4ec5\u8bbf\u95ee c=5 \u8fd9\u4e00\u6761\u8bb0\u5f55\u4e0d\u4f1a\u505c\u6b62\uff0c\u4f1a\u7ee7\u7eed\u5411\u53f3\u904d\u5386\uff0c\u5230 10 \u7ed3\u675f\u3002\u6839\u636e\u539f\u52192\uff0c\u8fd9\u65f6\u4f1a\u7ed9 id=10 \u52a0 next-key (5,10]<\/li>\n<li>\u4f46 id=10 \u540c\u65f6\u6ee1\u8db3\u4f18\u53162\uff0c\u9000\u5316\u6210\u95f4\u9699\u9501 (5,10)<\/li>\n<li>\u6839\u636e\u539f\u52192\uff0c\u8be5\u67e5\u8be2\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230 id \u7684\u503c\uff0c\u4e3b\u952e\u7d22\u5f15\u672a\u88ab\u8bbf\u95ee\u5230\uff0c\u4e0d\u52a0\u9501<\/li>\n<\/ul>\n<p>Session B\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u539f\u52191 \u548c\u4f18\u53161\uff0c\u7ed9 id=10 \u7684\u4e3b\u952e\u7d22\u5f15\u52a0\u884c\u9501\uff0c\u5e76\u4e0d\u51b2\u7a81\uff0c\u4fee\u6539\u6210\u529f<\/li>\n<\/ul>\n<p>Session C\uff1a<\/p>\n<ul>\n<li>\u7531\u4e8e Session A \u5df2\u7ecf\u5bf9\u7d22\u5f15 c \u4e2d (5,10) \u7684\u95f4\u9699\u52a0\u9501\uff0c\u4e0e\u63d2\u5165 c=7 \u51b2\u7a81\uff0c \u6240\u4ee5\u88ab\u963b\u585e<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u52a0\u9501\u5176\u5b9e\u662f\u5728\u7d22\u5f15\u4e0a\uff0c\u5e76\u4e14\u53ea\u52a0\u5728\u8bbf\u95ee\u5230\u7684\u8bb0\u5f55\u4e0a\uff0c\u5982\u679c\u60f3\u8981\u5728<code>lock in share mode<\/code>\u4e0b\u907f\u514d\u6570\u636e\u88ab\u66f4\u65b0\uff0c\u9700\u8981\u5f15\u5165\u8986\u76d6\u7d22\u5f15\u4e0d\u80fd\u5305\u542b\u7684\u5b57\u6bb5\u3002<\/p>\n<p>\u5047\u8bbe\u5c06 Session A \u7684\u8bed\u53e5\u6539\u6210<code>select id from t where c=5 for update;<\/code>\uff0c<code>for update<\/code> \u8868\u793a\u53ef\u80fd\u5f53\u524d\u4e8b\u52a1\u8981\u66f4\u65b0\u6570\u636e\uff0c\u6240\u4ee5\u4e5f\u4f1a\u7ed9\u6ee1\u8db3\u7684\u6761\u4ef6\u7684\u4e3b\u952e\u7d22\u5f15\u52a0\u9501\u3002\u8fd9\u65f6 Session B \u5c31\u4f1a\u88ab\u963b\u585e\u4e86\u3002<\/p>\n<h3>\u573a\u666f3\uff1a\u975e\u552f\u4e00\u7d22\u5f15\u7b49\u503c\u9501-\u9501\u4e3b\u952e<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select id from t where c=5 lock in share mode;<\/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;\">insert into t values(9,10,7);<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u548c\u573a\u666f 2 \u5f88\u76f8\u4f3c\uff0c\u8be5\u4f8b\u4e3b\u8981\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u8bf4\u660e\u95f4\u9699\u7684\u6982\u5ff5\u3002<\/p>\n<p>Session A \u7684\u52a0\u9501\u8303\u56f4\u4e0d\u53d8\uff0c\u7ed9\u7d22\u5f15 C \u52a0\u4e86 (0,5] \u548c (5,10) \u7684\u884c\u9501\u3002\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u975e\u552f\u4e00\u7d22\u5f15\u5f62\u6210\u7684 key\uff0c\u9700\u8981\u5305\u542b\u4e3b\u952e id\uff0c\u7528\u4e8e\u4fdd\u8bc1\u552f\u4e00\u6027\uff0c\u753b\u4e2a\u56fe\u5982\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/\u6570\u636e\u5e93\/MySQL-InnoDB\u9501-\u975e\u552f\u4e00\u7d22\u5f15\u7b49\u503c\u9501.png\" alt=\"MySQL-InnoDB\u9501-\u975e\u552f\u4e00\u7d22\u5f15\u7b49\u503c\u9501\" \/><\/p>\n<p>\u7531\u56fe\u4e2d\u53ef\u77e5\uff0c\u7531\u4e8e\u975e\u552f\u4e00\u7d22\u5f15\u5b58\u5728\u4e3b\u952eid\uff0c\u5e76\u4e14\u6309\u7167 B+ \u6811\u7684\u6392\u5e8f\u89c4\u5219\uff0c\u4e0d\u5149 c \u7684\u503c\u5728\u52a0\u9501\u8303\u56f4\u5185\u4e0d\u80fd\u88ab\u66f4\u6539\u548c\u63d2\u5165\uff0c\u5bf9\u5e94 id \u7684\u8303\u56f4\u4e5f\u4e0d\u80fd\u88ab\u66f4\u6539\u3002\u600e\u4e48\u7406\u89e3\u8fd9\u53e5\u8bdd\u5462\uff0c\u4e0a\u4e2a\u4f8b\u5b50\u4e0d\u662f\u8bf4\uff0c\u4e3b\u952e\u7d22\u5f15\u4e0d\u4f1a\u88ab\u52a0\u9501\uff0c\u600e\u4e48\u8fd9\u91cc\u7684\u4e3b\u952e id \u53c8\u88ab\u9501\u4e86\u5462\uff1f<\/p>\n<p>\u9996\u5148\u4e3b\u952e\u7d22\u5f15\u662f\u53e6\u5916\u4e00\u9897B+\u6811\u786e\u5b9e\u6ca1\u6709\u88ab\u9501\uff0c\u4f46\u8fd9\u91cc\u7531\u4e8e C \u662f\u975e\u552f\u4e00\u7d22\u5f15\uff0c\u5f62\u6210\u7684B+\u6811\u9700\u8981\u5c06\u4e3b\u952e\u7d22\u5f15\u7684 Id \u5305\u542b\u8fdb\u6765\uff0c\u5e76\u5728\u6309\u7167\u5148 c \u5b57\u6bb5\uff0c\u540e id \u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\u3002\u8fd9\u6837\uff0c\u5728\u7ed9 c \u5b57\u6bb5\u52a0\u884c\u9501\u65f6\uff0c\u5bf9\u5e94\u7684 id \u4e5f\u540c\u65f6\u52a0\u4e86\u884c\u9501\u3002<\/p>\n<p>\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0cSession2 \u66f4\u65b0\u6210\u529f\u662f\u56e0\u4e3a\u4fee\u6539\u7684\u662f d \u5730\u6bb5\uff0c\u5e76\u6ca1\u6709\u66f4\u65b0 id \u7684\u503c\uff0c\u6240\u4ee5\u6210\u529f\u4e86\u3002\u800c\u8fd9\u91cc\u60f3\u8981\u63d2\u5165\u7684 (id=6,c=10), \u867d\u7136 c=10 \u6ca1\u6709\u9501\uff0c\u4f46 id=6 \u5374\u5728\u9501\u7684\u8303\u56f4\u5185\uff0c\u6240\u4ee5\u8fd9\u91cc\u5c31\u88ab\u963b\u585e\u4e86\u3002\u540c\u6837\u63d2\u5165 (id=100,c=6) \u4e5f\u4f1a\u88ab\u963b\u585e\u3002<\/p>\n<p>\u4e5f\u5c31\u8bf4\uff0c\u5bf9\u4e8e\u975e\u552f\u4e00\u7d22\u5f15\uff0c\u8003\u8651\u52a0\u9501\u8303\u56f4\u65f6\u8981\u8003\u8651\u5230\u4e3b\u952e Id \u7684\u60c5\u51b5\u3002<\/p>\n<h3>\u573a\u666f4\uff1a\u4e3b\u952e\u7d22\u5f15\u8303\u56f4\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/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;\">select * from t where id&gt;=10 and id&lt;11 for update;<\/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;\">insert into t values(8,8,8);<\/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;\">\u6b63\u5e38<\/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;\">insert into t values(13,13,13);<\/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;\">\u88ab\u963b\u585e<\/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;\">update t set d=d+1 where id=15;<\/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;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A\uff1a<\/p>\n<ul>\n<li>\u5148\u627e\u5230 id=10 \u884c\uff0c\u5c5e\u4e8e\u7b49\u503c\u67e5\u8be2\u3002\u6839\u636e\u539f\u52191\uff0c\u4f18\u53161\uff0c\u8303\u56f4\u662f id=10 \u8fd9\u884c<\/li>\n<li>\u5411\u540e\u904d\u5386\uff0c\u5c5e\u4e8e\u8303\u56f4\u67e5\u8be2\uff0c\u627e\u5230 id=15 \u8fd9\u884c\uff0c\u6839\u636e\u539f\u52192\uff0c\u8303\u56f4\u662f (10,15]<\/li>\n<\/ul>\n<p>Session B\uff1a<\/p>\n<ul>\n<li>\u63d2\u5165 (8,8,8) \u53ef\u4ee5\uff0c(13,13,13) \u5c31\u4e0d\u884c\u4e86<\/li>\n<\/ul>\n<p>Session C\uff1a<\/p>\n<ul>\n<li>id=15 \u540c\u6837\u5728\u9501\u7684\u8303\u56f4\u5185\uff0c\u6240\u4ee5\u4e5f\u88ab\u963b\u585e<\/li>\n<\/ul>\n<h3>\u573a\u666f5\uff1a\u975e\u552f\u4e00\u7d22\u5f15\u8303\u56f4\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/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;\">select * from t where c&gt;=10 and c&lt;11 for update;<\/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;\">insert into t values(8,8,8);<\/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;\">\u88ab\u963b\u585e<\/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;\">insert into t values(13,13,13);<\/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;\">\u88ab\u963b\u585e<\/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;\">update t set d=d+1 where c=15;<\/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;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A\uff1a<\/p>\n<ul>\n<li>\u7531\u4e8e c \u662f\u975e\u552f\u4e00\u7d22\u5f15\uff0c\u7d22\u5f15\u5bf9\u4e8e c=10 \u7b49\u503c\u67e5\u8be2\u6765\u8bf4\uff0c\u6839\u636e\u539f\u52191\uff0c\u52a0\u9501\u8303\u56f4\u4e3a (5,10]<\/li>\n<li>\u5411\u53f3\u904d\u5386\uff0c\u8303\u56f4\u67e5\u8be2\uff0c\u52a0\u9501\u8303\u56f4\u4e3a (10,15]<\/li>\n<\/ul>\n<p>Session B:<\/p>\n<ul>\n<li>(8,8,8) \u548c (13,13,13) \u90fd\u51b2\u7a81\uff0c\u6240\u4ee5\u88ab\u963b\u585e<\/li>\n<\/ul>\n<p>Session C:<\/p>\n<ul>\n<li>c=5 \u4e5f\u88ab\u9501\u4f4f\uff0c\u4e5f\u4f1a\u88ab\u963b\u585e<\/li>\n<\/ul>\n<h3>\u573a\u666f6\uff1a\u552f\u4e00\u7d22\u5f15\u8303\u56f4\u9501 bug<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/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;\">select * from t where id&gt;10 and id &lt;=15 for update;<\/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;\">update t set d=d+1 where id=20;<\/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;\">\u88ab\u963b\u585e<\/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;\">insert into t values(16,16,16);<\/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;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A\uff1a<\/p>\n<ul>\n<li>\u7531\u4e8e\u5f00\u59cb\u627e\u5927\u4e8e 10 \u7684\u8fc7\u7a0b\u4e2d\u662f\u7b2c\u4e00\u6b21\u662f\u8303\u56f4\u67e5\u8be2\uff0c\u6240\u4ee5\u6ca1\u6709\u4f18\u5316\u539f\u5219\u3002\u52a0 (10,15]<\/li>\n<li>\u6709\u4e00\u4e2a\u7279\u6b8a\u7684\u5730\u65b9\uff0c\u7406\u8bba\u4e0a\u8bf4\u7531\u4e8e id \u662f\u552f\u4e00\u4e3b\u952e\uff0c\u627e\u5230 id=15 \u5c31\u5e94\u8be5\u505c\u4e0b\u6765\u4e86\uff0c\u4f46\u5b9e\u9645\u6ca1\u6709\u3002\u6839\u636e bug \u539f\u5219\uff0c\u4f1a\u7ee7\u7eed\u626b\u63cf\u7b2c\u4e00\u4e2a\u4e0d\u6ee1\u8db3\u7684\u503c\u4e3a\u6b62\uff0c\u63a5\u7740\u627e\u5230 id=20\uff0c\u56e0\u4e3a\u662f\u8303\u56f4\u67e5\u627e\uff0c\u6ca1\u6709\u4f18\u5316\u539f\u5219\uff0c\u7ee7\u7eed\u52a0\u9501 (15,20]<\/li>\n<li>\u8fd9\u4e2a bug \u5728 8.0.18 \u540e\u5df2\u7ecf\u4fee\u590d\u4e86<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e Session B \u548c Session C \u5747\u548c\u52a0\u9501\u7684\u8303\u56f4\u51b2\u7a81\u3002<\/p>\n<h3>\u573a\u666f7\uff1a\u975e\u552f\u4e00\u7d22\u5f15 delete<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/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;\">delete from t where c=10;<\/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;\">insert into t values(12,12,12)<\/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;\">\u88ab\u963b\u585e<\/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;\">update t set d=d+1 where c=15;<\/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;\">\u6210\u529f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>delete \u540e\u52a0 where \u8bed\u53e5\u548c<code>select * for update<\/code>\u8bed\u53e5\u7684\u52a0\u9501\u903b\u8f91\u7c7b\u4f3c\u3002<\/p>\n<p>Session A:<\/p>\n<ul>\n<li>\u6839\u636e\u539f\u52191\uff0c\u52a0 (5,10] \u7684 next-key<\/li>\n<li>\u5411\u53f3\u904d\u5386\uff0c\u6839\u636e\u4f18\u53162\uff0c\u52a0 (10,15) \u7684\u95f4\u6b47\u9501<\/li>\n<\/ul>\n<h3>\u573a\u666f8\uff1a\u975e\u552f\u4e00\u7d22\u5f15 limit<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">delete from t where c=10 limit 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;\">insert into t values(12,12,12)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u6b63\u5e38<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u867d\u7136 c=10 \u53ea\u6709\u4e00\u6761\u8bb0\u5f55\uff0c\u4f46\u548c\u573a\u666f7 \u7684\u52a0\u9501\u8303\u56f4\u4e0d\u540c\u3002<\/p>\n<p>Session A:<\/p>\n<ul>\n<li>\u6839\u636e\u539f\u52191\uff0c\u52a0 (5,10] \u7684 next-key<\/li>\n<li>\u56e0\u4e3a\u52a0\u4e86 limit 1\uff0c\u6240\u4ee5\u627e\u5230\u4e00\u6761\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e0d\u9700\u8981\u7ee7\u7eed\u904d\u5386\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u5728\u52a0\u9501<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u5bf9\u4e8e session B \u6765\u8bf4\uff0c\u5c31\u4e0d\u5728\u963b\u585e\u3002<\/p>\n<h3>\u573a\u666f9\uff1anext-key \u5f15\u53d1\u6b7b\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select id from t where c=10 lock in share mode;<\/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 d=d+1 where c=10;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">insert into t values(8,8,8)<\/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;\">ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>Session A \u7b2c\u4e00\u53e5 \u52a0 (5,10] next-key \u548c (10,15) \u7684 \u95f4\u9699\u9501<\/li>\n<li>Session B\uff0c\u548c Session A \u60f3\u8981\u7684\u52a0\u9501\u8303\u56f4\u76f8\u540c\uff0c\u5148\u52a0 (5,10] next-key \u53d1\u73b0\u88ab\u963b\u585e\uff0c\u540e\u9762 (10,15) \u6ca1\u6709\u88ab\u52a0\u4e0a\uff0c\u6682\u65f6\u7b49\u5f85<\/li>\n<li>Session A\uff0c\u52a0\u5165 (8,8,8) \u548c Session B \u7684\u52a0\u9501\u8303\u56f4 (5,10] \u51b2\u7a81\uff0c\u88ab\u963b\u585e\u3002\u5f62\u6210 A,B \u76f8\u4e92\u7b49\u5f85\u7684\u60c5\u51b5\u3002\u5f15\u53d1\u6b7b\u9501\u68c0\u6d4b\uff0c\u91ca\u653e Session B<\/li>\n<\/ul>\n<blockquote>\n<p>\u5047\u5982\u628a<code>insert into t values(8,8,8)<\/code>\u6539\u6210<code>insert into t values(11,11,11)<\/code>\u662f\u53ef\u4ee5\u7684\uff0c\u56e0\u4e3a Session B \u7684\u95f4\u6b47\u9501 (10,15) \u6ca1\u6709\u88ab\u52a0\u4e0a\u3002<\/p>\n<\/blockquote>\n<p>\u5206\u6790\u4e0b\u6b7b\u9501\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; show engine innodb status\\G;\n------------------------\nLATEST DETECTED DEADLOCK\n------------------------\n2020-03-08 17:04:10 0x7f9be0057700\n*** (1) TRANSACTION:\nTRANSACTION 836108, ACTIVE 16 sec starting index read\nmysql tables in use 1, locked 1\nLOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)\nMySQL thread id 1653, OS thread handle 140307320846080, query id 1564409 localhost cisco updating\nupdate t set d=d+1 where c=10\n*** (1) WAITING FOR THIS LOCK TO BE GRANTED:\nRECORD LOCKS space id 251 page no 4 n bits 80 index c of table `my_test`.`t` trx id 836108 lock_mode X waiting\nRecord lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0\n 0: len 4; hex 8000000a; asc     ;;\n 1: len 4; hex 8000000a; asc     ;;\n\n*** (2) TRANSACTION:\nTRANSACTION 836109, ACTIVE 22 sec inserting\nmysql tables in use 1, locked 1\n5 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1\nMySQL thread id 1655, OS thread handle 140307455112960, query id 1564410 localhost cisco update\ninsert into t values(8,8,8)\n*** (2) HOLDS THE LOCK(S):\nRECORD LOCKS space id 251 page no 4 n bits 80 index c of table `my_test`.`t` trx id 836109 lock mode S\nRecord lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0\n 0: len 4; hex 8000000a; asc     ;;\n 1: len 4; hex 8000000a; asc     ;;\n\n*** (2) WAITING FOR THIS LOCK TO BE GRANTED:\nRECORD LOCKS space id 251 page no 4 n bits 80 index c of table `my_test`.`t` trx id 836109 lock_mode X locks gap before rec insert intention waiting\nRecord lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0\n 0: len 4; hex 8000000a; asc     ;;\n 1: len 4; hex 8000000a; asc     ;;\n\n*** WE ROLL BACK TRANSACTION (1)<\/code><\/pre>\n<ol>\n<li><code>(1) TRANSACTION<\/code>\u8868\u660e\u53d1\u751f\u6b7b\u9501\u7684\u7b2c\u4e00\u4e2a\u4e8b\u52a1\u4fe1\u606f<\/li>\n<li><code>(2) TRANSACTION<\/code>\u8868\u660e\u53d1\u751f\u6b7b\u9501\u7684\u7b2c\u4e8c\u4e2a\u4e8b\u52a1\u4fe1\u606f<\/li>\n<li><code>WE ROLL BACK TRANSACTION (1)<\/code>\u8868\u660e\u6b7b\u9501\u7684\u5904\u7406\u65b9\u6848<\/li>\n<\/ol>\n<p>\u9488\u5bf9<code>(1) TRANSACTION<\/code><\/p>\n<ol>\n<li><code>(1) WAITING FOR THIS LOCK TO BE GRANTED<\/code>\u8868\u793a<code>update t set d=d+1 where c=10<\/code>\u8981\u7533\u8bf7\u5199\u9501\uff0c\u5e76\u5904\u4e8e\u9501\u7b49\u5f85\u7684\u60c5\u51b5<\/li>\n<li>\u7533\u8bf7\u7684\u5bf9\u8c61\u662f<code>n_fields 2<\/code>\uff0c<code>hex 8000000a;<\/code>\u548c<code>hex 8000000a;<\/code>\uff0c\u4e5f\u5c31\u662f id=10 \u548c c=10 \u7684\u8bb0\u5f55<\/li>\n<\/ol>\n<p>\u9488\u5bf9<code>(2) TRANSACTION<\/code><\/p>\n<ol>\n<li><code>HOLDS THE LOCK(S)<\/code>: \u8868\u793a\u5f53\u524d\u4e8b\u52a12\u6301\u6709\u7684\u9501\u662f: <code>hex 8000000a;<\/code>\u548c<code>hex 8000000a;<\/code><\/li>\n<li><code>WAITING FOR THIS LOCK TO BE GRANTED<\/code>: \u8868\u793a\u5bf9\u4e8e<code>insert into t values(8,8,8)<\/code>\u8fdb\u884c\u6240\u7b49\u5f85<\/li>\n<li><code>lock_mode X locks gap before rec insert intention waiting<\/code>: \u8868\u660e\u5728\u63d2\u5165\u610f\u5411\u9501\u65f6\uff0c\u7b49\u5f85\u4e00\u4e2a\u95f4\u9699\u9501( gap before rec)<br \/>\n\u6240\u4ee5\u6700\u540e\u9009\u62e9\uff0c\u56de\u6eda\u4e8b\u52a1 (1)<\/li>\n<\/ol>\n<h3>\u573a\u666f10\uff1a\u975e\u552f\u4e00\u7d22\u5f15 order by<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where c&gt;=15 and c&lt;=20 order by c desc lock in share mode;<\/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;\">insert into t values(6,6,6);<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u5206\u6790\u5177\u4f53\u7684\u52a0\u9501\u8fc7\u7a0b\u65f6\uff0c\u5148\u8981\u5206\u6790\u8bed\u53e5\u7684\u6267\u884c\u987a\u5e8f\u3002\u5982 Session A \u4e2d\u4f7f\u7528\u4e86<code>ordery by c desc<\/code>\u6309\u7167\u964d\u5e8f\u6392\u5217\u7684\u8bed\u53e5\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u9700\u8981\u5728\u7d22\u5f15\u6811 C \u4e0a\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a 20 \u7684\u503c\uff0c\u7136\u540e\u5411\u5de6\u904d\u5386\u3002\u5e76\u4e14\u7531\u4e8e C \u662f\u975e\u552f\u4e00\u7d22\u5f15 20 \u7684\u503c\u5e94\u8be5\u662f\u8bb0\u5f55\u4e2d\u6700\u53f3\u8fb9\u7684\u503c\u3002<\/p>\n<p>Session A \u7684\u52a0\u9501\u8fc7\u7a0b\uff1a<\/p>\n<ul>\n<li>\u5728\u627e\u5230\u7b2c\u4e00\u4e2a c=20 \u7684\u503c\u540e\uff0c\u52a0 next-key (15,20]<\/li>\n<li>\u4f46\u4e0d\u4f1a\u505c\u4e0b\uff0c\u56e0\u4e3a\u65e0\u6cd5\u786e\u5b9a\u5f53\u524d c=20 \u662f\u6700\u53f3\u9762\u7684\u503c\uff0c\u7ee7\u7eed\u904d\u5386\u5230 c=25\uff0c\u53d1\u73b0\u4e0d\u6ee1\u8db3\uff0c\u6839\u636e\u4f18\u53162\uff0c\u52a0 (20,25) \u7684\u95f4\u9699\u9501<\/li>\n<li>\u7136\u540e\u4ece\u6700\u5de6\u9762\u7684 c=20 \u5411\u5de6\u904d\u5386\uff0c\u627e\u5230 c=15\uff0c\u52a0\u9501 next-key (10,15]<\/li>\n<li>\u548c\u4e4b\u524d\u662f\u4e00\u6837\uff0c\u65e0\u6cd5\u786e\u5b9a c=15 \u662f\u6700\u5de6\u9762\u7684\u503c\uff0c\u7ee7\u7eed\u904d\u5386\u5230 c=10\uff0c\u6839\u636e\u4f18\u53162\uff0c\u52a0(5,10)\u7684\u95f4\u9699\u9501<\/li>\n<li>\u6700\u540e\u7531\u4e8e\u662f<code>select *<\/code>\u5bf9\u5e94\u4e3b\u952e\u7d22\u5f15 id=10,15,20 \u52a0\u884c\u9501<\/li>\n<\/ul>\n<h3>\u573a\u666f 11\uff1aINSERT INTO &#8230;. SELECT &#8230;<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\">begin;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">insert into t values(1,1,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;\">insert into t (id,c,d) select 1,1,1 from t where id=1;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\uff0c\u5bf9\u4e8e<code>INSERT INTO .... SELECT ...<\/code>\u4e2d select \u90e8\u5206\u4f1a\u52a0 next-key \u7684\u8bfb\u9501\u3002<\/p>\n<p>\u5bf9\u4e8e Session A\uff0c\u5728\u63d2\u5165\u6570\u636e\u540e\uff0c\u6709\u4e86 id=1 \u7684\u884c\u9501\u3002\u800c Session B \u4e2d\u7684 select \u867d\u7136\u662f\u4e00\u81f4\u6027\u8bfb\uff0c\u4f46\u4f1a\u52a0\u4e0a id=1 \u7684\u8bfb\u9501\u3002\u4e0e Session A \u51b2\u7a81\uff0c\u6240\u4ee5\u88ab\u963b\u585e\u3002<\/p>\n<h3>\u573a\u666f12\uff1a\u4e0d\u7b49\u53f7\u6761\u4ef6\u91cc\u7684\u7b49\u503c\u67e5\u8be2<\/h3>\n<pre><code class=\"language-sql\">begin;\nselect * from t where id&gt;9 and id&lt;12 order by id desc for update;<\/code><\/pre>\n<ul>\n<li>\u8fd9\u91cc\u7531\u4e8e\u662f order by \u8bed\u53e5\uff0c\u4f18\u5316\u5668\u4f1a\u5148\u627e\u5230\u7b2c\u4e00\u4e2a\u6bd4 12 \u5c0f\u7684\u503c\u3002\u5728\u7d22\u5f15\u6811\u641c\u7d22\u8fc7\u7a0b\u540e\uff0c\u5176\u5b9e\u8981\u627e\u5230 id=12 \u7684\u503c\uff0c\u4f46\u6ca1\u6709\u627e\u5230\uff0c\u5411\u53f3\u904d\u5386\u627e\u5230 id=15\uff0c\u6240\u4ee5\u52a0\u9501 (10,15]<\/li>\n<li>\u4f46\u7531\u4e8e\u7b2c\u4e00\u6b21\u67e5\u627e\u662f\u7b49\u503c\u67e5\u627e\uff08\u5728\u7d22\u5f15\u6811\u4e0a\u641c\u7d22\uff09\uff0c\u6839\u636e\u4f18\u53162\uff0c\u53d8\u4e3a\u95f4\u9699\u9501 (10,15)<\/li>\n<li>\u7136\u540e\u5411\u5de6\u904d\u5386\uff0c\u53d8\u4e3a\u8303\u56f4\u67e5\u8be2\uff0c\u627e\u5230 id=5 \u8fd9\u884c\uff0c\u52a0 (0,5] \u7684 next-key<\/li>\n<\/ul>\n<h3>\u573a\u666f 13\uff1a\u7b49\u503c\u67e5\u8be2 in<\/h3>\n<pre><code class=\"language-sql\">begin;\nselect id from t where c in(5,20,10) lock in share mode;<\/code><\/pre>\n<pre><code class=\"language-sql\">mysql&gt; explain select id from t where c in (5,20,10) lock in share mode\\G;\n*************************** 1. row ***************************\n           id: 1\n  select_type: SIMPLE\n        table: t\n   partitions: NULL\n         type: range\npossible_keys: c\n          key: c\n      key_len: 5\n          ref: NULL\n         rows: 3\n     filtered: 100.00\n        Extra: Using where; Using index\n1 row in set, 1 warning (0.00 sec)\n\nERROR:\nNo query specified<\/code><\/pre>\n<p>rows=3 \u5e76\u4e14\u4f7f\u7528\u7d22\u5f15 c\uff0c\u8bf4\u660e\u4e09\u4e2a\u503c\u90fd\u662f\u901a\u8fc7 B+ \u6811\u641c\u7d22\u5b9a\u4f4d\u7684\u3002<\/p>\n<ul>\n<li>\u5148\u67e5\u627e c=5\uff0c\u9501\u4f4f (0,5]\u3002\u7531\u4e8e c \u4e0d\u662f\u552f\u4e00\u7d22\u5f15\uff0c\u5411\u53f3\u904d\u5386\u5230 c=10\uff0c\u5f00\u59cb\u662f\u7b49\u503c\u67e5\u627e\uff0c\u52a0 (5,10)<\/li>\n<li>\u67e5\u627e c=15\uff0c\u9501\u4f4f (10,15]\uff0c\u518d\u52a0 (15,20)<\/li>\n<li>\u6700\u540e\u67e5\u627e c=20\uff0c\u9501\u4f4f (15,20]\uff0c\u518d\u52a0 (20,25)<\/li>\n<\/ul>\n<p>\u53ef\u89c1\uff0c<strong>\u5728 MySQL \u4e2d\uff0c\u9501\u662f\u4e00\u4e2a\u4e2a\u9010\u6b65\u52a0\u7684<\/strong><\/p>\n<p>\u5047\u8bbe\u8fd8\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u8bed\u53e5\uff1a<\/p>\n<pre><code class=\"language-sql\">select id from t where c in(5,20,10) order by c desc for update;<\/code><\/pre>\n<p>\u7531\u4e8e\u662f<code>order by c desc<\/code>\uff0c\u867d\u7136\u8fd9\u91cc\u7684\u52a0\u9501\u8303\u56f4\u6ca1\u6709\u53d8\uff0c\u4f46\u662f\u52a0\u9501\u7684\u987a\u5e8f\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u4f1a\u6309\u7167 c=20\uff0cc=10\uff0cc=5 \u7684\u987a\u5e8f\u52a0\u9501\u3002\u867d\u7136\u8bf4\u95f4\u9699\u9501\u672c\u8eab\u5e76\u4e0d\u51b2\u7a81\uff0c\u4f46\u8bb0\u5f55\u9501\u5374\u4f1a\u3002\u8fd9\u6837\u5982\u679c\u662f\u4e24\u4e2a\u8bed\u53e5\u5e76\u53d1\u7684\u60c5\u51b5\uff0c\u5c31\u53ef\u80fd\u53d1\u751f\u6b7b\u9501\uff0c\u7b2c\u4e00\u4e2a\u8bed\u53e5\u62e5\u6709\u4e86 c5 \u7684\u884c\u9501\uff0c\u8bf7\u6c42c=10 \u7684\u884c\u9501\u3002\u5f53\u7b2c\u4e8c\u4e2a\u8bed\u53e5\uff0c\u62e5\u6709\u4e86 c=10 \u7684\u884c\u9501\uff0c\u8bf7\u6c42 c=5 \u7684\u884c\u9501\u3002<\/p>\n<h3>\u573a\u666f14\uff1aGAP \u52a8\u6001\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where id&gt;10 and id&lt;=15 for update;<\/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;\">delete from t where id=10;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u6210\u529f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">insert into t values(10,10,10);<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc insert \u88ab\u963b\u585e\uff0c\u5c31\u662f\u56e0\u4e3a\u95f4\u9699\u9501\u662f\u4e2a\u52a8\u6001\u7684\u6982\u5ff5\uff0cSession B \u5728\u5220\u9664 id=10 \u7684\u8bb0\u5f55\u540e\uff0cSession A \u6301\u6709\u7684\u95f4\u9699\u53d8\u5927\u3002<\/p>\n<p>\u5bf9\u4e8e Session A \u539f\u6765\u6301\u6709\uff0c(10,15] \u548c (15,20] \u7684 next-key \u9501\u3002 Session B \u5220\u9664 id=10 \u7684\u8bb0\u5f55\u540e\uff0c(10,15] \u53d8\u6210\u4e86 (5,15] \u7684\u95f4\u9699\u3002\u6240\u4ee5\u4e4b\u540e\u5c31\u63d2\u5165\u4e0d\u56de\u53bb\u4e86\u3002<\/p>\n<h3>\u573a\u666f15\uff1aupdate Gap \u52a8\u6001\u9501<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where id&gt;5 lock in share mode;<\/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 c=1 where c=5;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u6210\u529f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">update t set c=5 where c=1;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">\u88ab\u963b\u585e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A \u52a0\u9501\uff1a(5,10], (10,15], (15,20], (20,25], (25,supermum]<\/p>\n<p>c&gt;5 \u7b2c\u4e00\u4e2a\u627e\u5230\u7684\u662f c=10\uff0c\u5e76\u4e14\u662f\u8303\u56f4\u67e5\u627e\uff0c\u6ca1\u6709\u4f18\u5316\u539f\u5219\u3002<\/p>\n<p>Session B \u7684 update \u53ef\u4ee5\u62c6\u6210\u4e24\u6b65\uff1a<\/p>\n<ul>\n<li>\u63d2\u5165 (c=1,id=5).<\/li>\n<li>\u5220\u9664 (c=5,id=5).<\/li>\n<\/ul>\n<p>\u6216\u8005\u7406\u89e3\u6210\uff0c\u52a0(0,5] next-key \u548c (5,10) \u7684\u95f4\u9699\u9501\uff0c\u4f46\u95f4\u9699\u9501\u4e0d\u51b2\u7a81<\/p>\n<p>\u4fee\u6539\u540e Session A \u7684\u9501\u53d8\u4e3a\uff1a(c=1, 10], (10,15], (15,20], (20,25], (25,supermum]<\/p>\n<p>\u63a5\u4e0b\u6765\uff1a<code>update t set c=5 where c=1<\/code><\/p>\n<ul>\n<li>\u63d2\u5165 (c=5,id=5)<\/li>\n<li>\u5220\u9664 (c=1,id=5)<\/li>\n<\/ul>\n<p>\u7b2c\u4e00\u6b65\u63d2\u5165\u610f\u5411\u9501\u548c\u95f4\u9699\u9501\u51b2\u7a81\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/innodb-locking.html\" title=\"InnoDB-locking\">InnoDB-locking<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/innodb-locks-set.html\" title=\"\u52a0\u9501\u8fc7\u7a0b\">\u52a0\u9501\u8fc7\u7a0b<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/explain-output.html#explain_rows\" title=\"explain rows\">explain rows<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/innodb-enabling-monitors.html\" title=\"enable monitors\">enable monitors<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u51e0\u4e2a\u6708\u4e4b\u524d\uff0c\u5f00\u59cb\u6df1\u5165\u5b66\u4e60 MySQL \u3002\u8bf4\u8d77\u6570\u636e\u5e93\uff0c\u5e76\u53d1\u63a7\u5236\u662f\u5176\u4e2d\u5f88\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u4e8e\u662f\uff0c\u5c31\u8fd9\u6837\u5f00\u8d77\u4e86 MySQ [&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":[526],"class_list":["post-2090","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-innodb"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2090","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=2090"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2090\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}