{"id":17797,"date":"2025-03-30T11:26:00","date_gmt":"2025-03-30T03:26:00","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=17797"},"modified":"2025-04-01T08:46:58","modified_gmt":"2025-04-01T00:46:58","slug":"mysql-innodb-record-lock-primary-key-lock","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2025\/03\/mysql-innodb-record-lock-primary-key-lock\/","title":{"rendered":"MySQL\/InnoDB \u9501\u8bca\u65ad\uff1a\u4e3b\u952e\u8bb0\u5f55\u9501"},"content":{"rendered":"<div id=\"tocer-1\" class=\"tocer tocer-pos-flt-e\"><div class=\"tocer-toc\"><header class=\"tocer-header\"><h2 class=\"tocer-title\">\u76ee\u5f55<\/h2>\n<\/header>\n<ul class=\"tocer-list tocer-adv tocer-sp-os-bf tocer-st-ctrs-dec tocer-lvl1\"><li class=\"tocer-has-sub\"><div><a href=\"#tocer-heading-1\">\u6784\u9020\u4e3b\u952e\u9501\u7ade\u4e89<\/a><\/div>\n<ul class=\"tocer-sublist tocer-lvl2\"><li><div><a href=\"#tocer-heading-2\">\u67e5\u770b\u5f53\u524d\u7684\u9694\u79bb\u7ea7\u522b<\/a><\/div><\/li>\n<li><div><a href=\"#tocer-heading-3\">\u51c6\u5907\u8868\u7ed3\u6784<\/a><\/div><\/li>\n<li><div><a href=\"#tocer-heading-4\">\u6784\u5efa\u9501\u7b49\u5f85<\/a><\/div><\/li>\n<\/ul>\n<\/li>\n<li class=\"tocer-has-sub\"><div><a href=\"#tocer-heading-5\">\u89c2\u5bdf\u9501\u4fe1\u606f<\/a><\/div>\n<ul class=\"tocer-sublist tocer-lvl2\"><li><div><a href=\"#tocer-heading-6\">\u901a\u8fc7 SHOW INNODB STATUS \u89c2\u5bdf<\/a><\/div><\/li>\n<li><div><a href=\"#tocer-heading-7\">\u8be6\u89e3\u9501\u4fe1\u606f<\/a><\/div><\/li>\n<li><div><a href=\"#tocer-heading-8\">\u5728\u5185\u7f6e\u89c6\u56fe\u4e2d\u67e5\u770b\u9501\u4fe1\u606f<\/a><\/div><\/li>\n<\/ul>\n<\/li>\n<li><div><a href=\"#tocer-heading-9\">\u8865\u5145\u8bf4\u660e<\/a><\/div><\/li>\n<li><div><a href=\"#tocer-heading-10\">\u76f8\u5173\u53c2\u8003\u8d44\u6e90<\/a><\/div><\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<p>InnoDB \u7684\u9501\u8bca\u65ad\u662f\u4e00\u4e2a\u6bd4\u8f83\u56f0\u96be\u7684\u4e8b\u60c5\u3002\u9996\u5148\uff0c\u9501\u673a\u5236\u662f\u4e00\u79cd\u8f83\u4e3a\u590d\u6742\u7684\u8d44\u6e90\u7ade\u4e89\u7ba1\u7406\u673a\u5236\uff0c\u5982\u679c\u6d89\u53ca\u7684\u8d44\u6e90\u7c7b\u578b\u6bd4\u8f83\u591a\uff0c\u9501\u7c7b\u578b\u53c8\u6bd4\u8f83\u591a\uff0c\u90a3\u4e48\u9501\u673a\u5236\u5c31\u4f1a\u770b\u8d77\u6765\u6bd4\u8f83\u590d\u6742\u3002\u5177\u4f53\u5230\uff0cMySQL\/InnoDB \u4e0a\uff0c\u786e\u5b9e\u4e5f\u5c31\u975e\u5e38\u590d\u6742\u4e86\u3002\u8fd9\u91cc\u4e3b\u8981\u5173\u6ce8 InnoDB \u5c42\u9762\u7684\u9501\uff0c\u6d89\u53ca\u7684\u5185\u5bb9\u5219\u5305\u62ec\u4e86\u8bb0\u5f55\u9501(record)\u3001\u95f4\u9699\u9501(gap)\u3001\u7d22\u5f15\u7f1d\u9699\u9501(next-key)\uff0c\u800c\u4e3a\u4ec0\u4e48\u8981\u52a0\u9501\uff0c\u5219\u53c8\u6d89\u53ca\u5230\u9694\u79bb\u7ea7\u522b\u3001MVCC\u7684\u5b9e\u73b0\uff0c\u800c\u9501\u7684\u5b9e\u73b0\uff0c\u5219\u53c8\u4e0e InnoDB \u5e95\u5c42\u7684\u6570\u636e\u5b58\u50a8\u7ed3\u6784\u6709\u6709\u4e00\u5b9a\u7684\u5173\u7cfb\uff0c\u603b\u5f97\u6765\u8bf4\u6d89\u53ca\u7684\u9762\u6bd4\u8f83\u591a\uff0c\u5982\u679c\u5bf9\u4e8e\u8fd9\u4e9b\u6982\u5ff5\u6ca1\u6709\u4e86\u89e3\uff0c\u5219\u6bd4\u8f83\u96be\u7406\u89e3 InnoDB \u7684\u9501\u673a\u5236\uff0c\u4e5f\u5c31\u6bd4\u8f83\u96be\u53bb\u6392\u67e5 InnoDB \u9501\u51fa\u73b0\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u5c42\u9762\u662f\u6392\u67e5\u624b\u6bb5\u3002MySQL\/InnoDB\u5728\u65e9\u671f\u7684\u7248\u672c\u4e2d\uff0c\u5bf9\u4e8e\u9501\u95ee\u9898\u7684\u6392\u67e5\u624b\u6bb5\u662f\u6bd4\u8f83\u6709\u9650\u7684\uff0c\u800c\u4e14\u4e0e\u5f88\u591a\u7684\u914d\u7f6e\u53c2\u6570\u6709\u5173\uff0c\u6240\u4ee5\u4e86\u89e3\u8fd9\u4e9b\u53c2\u6570\uff0c\u719f\u6089MySQL\/InnoDB\u9501\u4fe1\u606f\u67e5\u770b\u7684\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5219\u662f\u53e6\u4e00\u4e2a\u9700\u8981\u4e86\u89e3\u7684\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5173\u4e8e InnoDB \u9501\u95ee\u9898\u7684\u4e5f\u5e76\u4e0d\u662f\u4e00\u4e24\u4e2a\u8bdd\u9898\u80fd\u591f\u8bf4\u6e05\u695a\u7684\u3002\u672c\u6587\u53ef\u80fd\u662f\u4e00\u4e2a\u7cfb\u5217\uff08\u7ed9\u81ea\u5df1\u6316\u5751\uff09\uff0c\u4e00\u4e2a\u81ea\u5df1\u5b66\u4e60\u4ee5\u53ca\u9501\u95ee\u9898\u6392\u67e5\u7ecf\u9a8c\u7684\u5206\u4eab\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u9020\u4e3b\u952e\u9501\u7ade\u4e89<\/h3>\n\n\n\n<p>\u8bb0\u5f55\u9501\uff0c\u5e94\u8be5\u662f InnoDB \u9501\u7c7b\u578b\u4e2d\u8f83\u4e3a\u5e38\u89c1\uff0c\u4e5f\u662f\u5728<code>READ-COMMITTED<\/code>\u4e8b\u52a1\u7ea7\u522b\u4e0b\uff0c\u6bd4\u8f83\u5bb9\u6613\u9047\u5230\u7684\u6b7b\u9501\u7c7b\u578b\uff08\u5982\u679c\u6709\u6b7b\u9501\u7684\u8bdd\uff09\u3002\u8fd9\u91cc\u901a\u8fc7\u89c2\u5bdf\u4e3b\u952e\u6b7b\u9501\u3001\u552f\u4e00\u952e\u6b7b\u9501\uff0c\u521d\u6b65\u4e86\u89e3 <code>InnoDB<\/code> \u9501\u4fe1\u606f\u5185\u5bb9\uff0c\u4ee5\u53ca\u7ed3\u6784\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u770b\u5f53\u524d\u7684\u9694\u79bb\u7ea7\u522b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; show global variables like '%iso%';\n+-----------------------+----------------+\n| Variable_name         | Value          |\n+-----------------------+----------------+\n| transaction_isolation | READ-COMMITTED |\n+-----------------------+----------------+\n1 row in set (0.01 sec)\n\nmysql&gt; show session variables like '%iso%';\n+-----------------------+----------------+\n| Variable_name         | Value          |\n+-----------------------+----------------+\n| transaction_isolation | READ-COMMITTED |\n+-----------------------+----------------+\n1 row in set (0.00 sec)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u51c6\u5907\u8868\u7ed3\u6784<\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33%\">\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">DROP TABLE IF EXISTS t1;\n\nCREATE TABLE t1 ( \n  id int unsigned, \n  nick varchar(32),\n  age int,\n  primary key (id)\n)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66%\">\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; desc t1;\n+-------+--------------+------+-----+---------+-------+\n| Field | Type         | Null | Key | Default | Extra |\n+-------+--------------+------+-----+---------+-------+\n| id    | int unsigned | NO   | PRI | NULL    |       |\n| nick  | varchar(32)  | YES  |     | NULL    |       |\n| age   | int          | YES  |     | NULL    |       |\n+-------+--------------+------+-----+---------+-------+<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u6784\u5efa\u9501\u7b49\u5f85<\/h4>\n\n\n\n<p>\u5728\u4e24\u4e2a\u4f1a\u8bdd\u4e2d\uff0c\u6309\u4ece\u4e0a\u5230\u4e0b\uff0c\u6267\u884c\u4e0b\u8ff0\u7684SQL\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">\u65f6\u95f4\u987a\u5e8f<\/td><td><code>Session A<\/code><\/td><td><code>Session B<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td><code>START TRANSACTION;<\/code><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td><code>INSERT INTO t1 VALUES ( 1, \"a\",12 );<\/code><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td><\/td><td><code>START TRANSACTION;<\/code><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td><\/td><td><code>INSERT INTO t1 VALUES ( 1, \"x\",23 );<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u65f6\u5019\uff0c<code>Session B<\/code>\u4f1a\u9677\u5165\u7b49\u5f85\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89c2\u5bdf\u9501\u4fe1\u606f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u901a\u8fc7 SHOW INNODB STATUS \u89c2\u5bdf<\/h4>\n\n\n\n<p>\u6b64\u65f6\u67e5\u770b InnoDB \u9501\u4fe1\u606f\uff0c\u5219\u6709\u5982\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">---TRANSACTION 10094, ACTIVE 14 sec inserting\nmysql tables in use 1, locked 1\nLOCK WAIT 2 lock struct(s), heap size 1128, 1 row lock(s)\nMySQL thread id 84, OS thread handle 140453961758272, query id 9262 10.88.0.1 sysb update\nINSERT INTO t1 VALUES ( 1, \"x\",23 )\n------- TRX HAS BEEN WAITING 14 SEC FOR THIS LOCK TO BE GRANTED:\nRECORD LOCKS space id 27 page no 4 n bits 72 index PRIMARY of table `sysbenchdb`.`t1` trx id 10094 lock mode S locks rec but not gap waiting\nRecord lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0\n 0: len 4; hex 00000001; asc     ;;\n 1: len 6; hex 00000000276d; asc     'm;;\n 2: len 7; hex 810000008d0110; asc        ;;\n 3: len 1; hex 61; asc a;;\n 4: len 4; hex 8000000c; asc     ;;\n\n------------------\n---TRANSACTION 10093, ACTIVE 23 sec\n2 lock struct(s), heap size 1128, 1 row lock(s), undo log entries 1\nMySQL thread id 83, OS thread handle 140454159017536, query id 9260 10.88.0.1 sysb<\/code><\/pre>\n\n\n\n<p>\u5728 <code>SHOW ENGINE INNODB STATUS\\G<\/code>\u4e2d\u4ec5\u6253\u5370\u4e86\u5904\u4e8e\u7b49\u5f85\u6388\u4e88\u72b6\u6001\u7684\u9501\u4fe1\u606f\uff0c\u5373\u8fd9\u91cc\u4ec5\u6253\u5370\u4e86\u4e8b\u52a110094\u7684\u7b49\u5f85\u7684\u9501\u8be6\u60c5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8be6\u89e3\u9501\u4fe1\u606f<\/h4>\n\n\n\n<p>\u8fd9\u91cc\u8be6\u7ec6\u770b\u770b\u5176\u4e2d\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">RECORD LOCKS space id 27 page no 4 n bits 72 index PRIMARY of table `sysbenchdb`.`t1` trx id 10094 lock mode S locks rec but not gap waiting<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&#8220;RECORD LOCKS&#8221;<\/td><td>\u8fd9\u662f\u4e00\u4e2a\u8bb0\u5f55\u9501<\/td><\/tr><tr><td><code>space id 27 page no 4<\/code><\/td><td>\u8be5\u8bb0\u5f55\u5904\u4e8e\u7269\u7406\u4f4d\u7f6e\uff0c\u5305\u62ec\u9875\u9762\u7f16\u53f7\uff0c\u4ee5\u53ca\u9875\u9762\u6240\u5904\u7684\u7269\u7406\u6587\u4ef6\u7f16\u53f7<\/td><\/tr><tr><td><code>n bits 72<\/code><\/td><td>\u8be5\u9875\u9762\u670972\u4e2a\u8bb0\u5f55\u5bf9\u5e94\u6807\u8bb0\u4f4d<\/td><\/tr><tr><td><code>index PRIMARY of table sysbenchdb.t1 <\/code><\/td><td>\u5bf9\u5e94\u8868<\/td><\/tr><tr><td><code>trx id 10094<\/code><\/td><td>\u6240\u5728\u7684\u4e8b\u52a1 ID <\/td><\/tr><tr><td><code>lock mode S<\/code><\/td><td>\u9501\u7c7b\u578b\uff0c\u4e3a <code>S<\/code> \uff0c\u5373\u5171\u4eab\u9501<\/td><\/tr><tr><td><code>locks rec but not gap<\/code><\/td><td>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u8bb0\u5f55\u9501\uff0c\u65e0\u9700\u5bf9\u8bb0\u5f55\u524d\u7684\u201c\u95f4\u9699\u201d\u52a0\u9501<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0\n 0: len 4; hex 00000001; asc     ;;\n 1: len 6; hex 00000000276d; asc     'm;;\n 2: len 7; hex 810000008d0110; asc        ;;\n 3: len 1; hex 61; asc a;;\n 4: len 4; hex 8000000c; asc     ;;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>Record lock<\/code><\/td><td>\uff08\u7b49\u5f85\u7684\uff09\u8bb0\u5f55\u9501<\/td><\/tr><tr><td><code>heap no 2<\/code><\/td><td>\u5728\u9875\u9762\u4e2d\uff0c\u8bb0\u5f55\u4ee5\u5806\u7684\u65b9\u5f0f\u5b58\u653e\uff0c\u8be5\u8bb0\u5f55\u7684\u5806\u7f16\u53f7<\/td><\/tr><tr><td><code>PHYSICAL RECORD: n_fields 5;<\/code><\/td><td>\u8bb0\u5f55\u5171\u4e94\u4e2a\u5b57\u6bb5<\/td><\/tr><tr><td><code>0: len 4; hex 00000001; asc     ;;<\/code><\/td><td><code>id<\/code> \u5b57\u6bb5\u503c\uff0c\u4e3a1<\/td><\/tr><tr><td><code>1: len 6; hex 00000000276d; asc     'm;;<\/code><\/td><td>\u8be5\u8bb0\u5f55\u7684<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-multi-versioning.html\"><code>DB_TRX_ID<\/code><\/a>,\u5373\u4e3a<code>0x276d<\/code>\uff0c<code>10093<\/code><\/td><\/tr><tr><td><code>2: len 7; hex 810000008d0110; asc        ;;<\/code><\/td><td><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-multi-versioning.html\"><code>DB_ROLL_PTR<\/code><\/a><\/td><\/tr><tr><td><code>3: len 1; hex 61; asc a;;<\/code><\/td><td>\u5b57\u6bb5<code>nick<\/code>\u53d6\u503c <code>a<\/code><\/td><\/tr><tr><td><code>4: len 4; hex 8000000c; asc     ;;<\/code><\/td><td>\u5b57\u6bb5<code>age<\/code>\u53d6\u503c <code>0x8000000c<\/code>\uff0c\u5373<code>12<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u4e00\u5171\u6709\u4e94\u4e2a\u5b57\u6bb5\uff08<code>n_fields 5; <\/code>\uff09\u3002\u90a3\u5b9e\u9645\u8fd9\u4e2a\u8868\uff0c\u53ea\u6709\u4e09\u4e2a\u5b57\u6bb5\uff0c\u4e3a\u4ec0\u4e48\u8fd9\u91cc\u4f1a\u6709\u4e94\u4e2a\u5b57\u6bb5\uff1f\u539f\u56e0\u5728\u4e8e\uff0cInnoDB \u5728\u5b58\u50a8\u6570\u636e\u4fe1\u606f\u7684\u65f6\u5019\uff0c\u4f1a\u989d\u5916\u7684\u5b58\u50a8\u4e24\u4e2a\u4fe1\u606f\uff1a<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-multi-versioning.html\">DB_TRX_ID<\/a>\u3001<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-multi-versioning.html\">DB_ROLL_PTR<\/a>\u3002\u8fd9\u662f\u4e24\u4e2aInnoDB\u7684\u8f83\u4e3a\u5e95\u5c42\u7684\u6982\u5ff5\uff0c\u5177\u4f53\u7684\u53ef\u4ee5\u53c2\u8003\u8be5\u6587\u6863\uff1a<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-multi-versioning.html\">17.3 InnoDB Multi-Versioning<\/a>\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\u5230\uff0c\u8fd9\u91cc\u7684 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-multi-versioning.html\">DB_TRX_ID<\/a> \u53d6\u503c\u4e3a <code><code>0x276d<\/code><\/code>\uff0c\u8f6c\u5316\u4e3a10\u8fdb\u5236\u5219\u4e3a\uff1a<code><code>10093<\/code><\/code>\u3002\u5373\uff0c\u8be5\u6761\u8bb0\u5f55\u6700\u540e\u4e00\u6b21\u88ab\u4fee\u6539\u662f\u88ab\u4e8b\u52a1<code><code>10093<\/code><\/code>\u6240\u4fee\u6539\uff0c\u5373\u4e0a\u8ff0\u8868\u683c\u4e2d\u7684<code>Session A<\/code>\u6240\u6267\u884c\u7684<code>SQL<\/code>\u6240\u4fee\u6539\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5728\u5185\u7f6e\u89c6\u56fe\u4e2d\u67e5\u770b\u9501\u4fe1\u606f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; SELECT\n    -&gt;     ENGINE_TRANSACTION_ID AS TRX_ID,\n    -&gt;     OBJECT_NAME,\n    -&gt;     INDEX_NAME,\n    -&gt;     LOCK_TYPE,\n    -&gt;     LOCK_MODE,\n    -&gt;     LOCK_STATUS,\n    -&gt;     LOCK_DATA\n    -&gt;   FROM performance_schema.data_locks;\n+--------+-------------+------------+-----------+---------------+-------------+-----------+\n| TRX_ID | OBJECT_NAME | INDEX_NAME | LOCK_TYPE | LOCK_MODE     | LOCK_STATUS | LOCK_DATA |\n+--------+-------------+------------+-----------+---------------+-------------+-----------+\n|  10224 | t1          | NULL       | TABLE     | IX            | GRANTED     | NULL      |\n|  10225 | t1          | NULL       | TABLE     | IX            | GRANTED     | NULL      |\n|  10224 | t1          | PRIMARY    | RECORD    | X,REC_NOT_GAP | GRANTED     | 1         |\n|  10225 | t1          | PRIMARY    | RECORD    | S,REC_NOT_GAP | WAITING     | 1         |\n+--------+-------------+------------+-----------+---------------+-------------+-----------+<\/code><\/pre>\n\n\n\n<p>\uff08\u6ce8\uff1a\u8fd9\u91cc\u7684<code>TRX_ID<\/code>\u4e0e\u4e0a\u8ff0\u4e0d\u540c\uff0c\u56e0\u4e3a\u8fd9\u662f\u91cd\u65b0\u6267\u884c\u4e86\u4e0a\u8ff0\u7684\u51b2\u7a81\u4e8b\u52a1\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8865\u5145\u8bf4\u660e<\/h3>\n\n\n\n<p>\u5728\u672c\u793a\u4f8b\u4e2d\uff0c\u5199\u5165\u7684\u6570\u636e\u4ec5\u4e3a\u4e3b\u952e\uff08\u4e5f\u662f\u4e00\u79cd\u552f\u4e00\u952e\uff09\uff0c\u6545\u65e0\u8bba\u662f<code>READ-COMMITTED<\/code>\u3001\u8fd8\u662f<code>REPEATABLE-READ<\/code>\uff0c\u5176\u6240\u9700\u8981\u7684\u9501\u90fd\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f8\u5173\u53c2\u8003\u8d44\u6e90<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/innodb-locking-transaction-model.html\">InnoDB Locking and Transaction Model@MySQL 8.4 Reference Manual<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/blog-archive\/innodb-data-locking-part-1-introduction\/\">InnoDB Data Locking &#8211; Part 1 &#8220;Introduction&#8221;@MySQL Blog Archive<\/a> <\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/blog-archive\/innodb-data-locking-part-2-locks\/\">InnoDB Data Locking &#8211; Part 2 &#8220;Locks&#8221;@MySQL Blog Archive<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/blog-archive\/innodb-data-locking-part-2-5-locks-deeper-dive\/\">InnoDB Data Locking &#8211; Part 2.5 &#8220;Locks&#8221; (Deeper dive)@MySQL Blog Archive<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/blog-archive\/innodb-data-locking-part-3-deadlocks\/\">InnoDB Data Locking \u2013 Part 3 &#8220;Deadlocks&#8221;@MySQL Blog Archive<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.huihoo.com\/mysql\/percona\/live\/mysql-conference-2015\/Understanding-InnoDB-locks-and-deadlocks.pdf\">Understanding InnoDB Locks and Deadlocks@2015 Percona Live<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/planet.mysql.com\/entry\/?id=79629\">Introduction to Transaction Locks in InnoDB Storage Engine@2013<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>InnoDB \u7684\u9501\u8bca\u65ad\u662f\u4e00\u4e2a\u6bd4\u8f83\u56f0\u96be\u7684\u4e8b\u60c5\u3002\u9996\u5148\uff0c\u9501\u673a\u5236\u662f\u4e00\u79cd\u8f83\u4e3a\u590d\u6742\u7684\u8d44\u6e90\u7ade\u4e89\u7ba1\u7406\u673a\u5236\uff0c\u5982\u679c\u6d89\u53ca\u7684\u8d44\u6e90\u7c7b\u578b\u6bd4\u8f83\u591a\uff0c\u9501\u7c7b\u578b\u53c8\u6bd4\u8f83\u591a\uff0c\u90a3\u4e48\u9501\u673a\u5236\u5c31\u4f1a\u770b\u8d77\u6765\u6bd4\u8f83\u590d\u6742\u3002\u5177\u4f53\u5230\uff0cMySQL\/InnoDB \u4e0a\uff0c\u786e\u5b9e\u4e5f\u5c31\u975e\u5e38\u590d\u6742\u4e86\u3002\u8fd9\u91cc\u4e3b\u8981\u5173\u6ce8 InnoDB \u5c42\u9762\u7684\u9501\uff0c\u6d89\u53ca\u7684\u5185\u5bb9\u5219\u5305\u62ec\u4e86\u8bb0\u5f55\u9501(record)\u3001\u95f4\u9699\u9501(gap)\u3001\u7d22\u5f15\u7f1d\u9699\u9501(next-key)\uff0c\u800c\u4e3a\u4ec0\u4e48\u8981\u52a0\u9501\uff0c\u5219\u53c8\u6d89\u53ca\u5230\u9694\u79bb\u7ea7\u522b\u3001MVCC\u7684\u5b9e\u73b0\uff0c\u800c\u9501\u7684\u5b9e\u73b0\uff0c\u5219\u53c8\u4e0e InnoDB \u5e95\u5c42\u7684\u6570\u636e\u5b58\u50a8\u7ed3\u6784\u6709\u6709\u4e00\u5b9a\u7684\u5173\u7cfb\uff0c\u603b\u5f97\u6765\u8bf4\u6d89\u53ca\u7684\u9762\u6bd4\u8f83\u591a\uff0c\u5982\u679c\u5bf9\u4e8e\u8fd9\u4e9b\u6982\u5ff5\u6ca1\u6709\u4e86\u89e3\uff0c\u5219\u6bd4\u8f83\u96be\u7406\u89e3 InnoDB \u7684\u9501\u673a\u5236\uff0c\u4e5f\u5c31\u6bd4\u8f83\u96be\u53bb\u6392\u67e5 InnoDB \u9501\u51fa\u73b0\u7684\u95ee\u9898\u3002 \u53e6\u4e00\u4e2a\u5c42\u9762\u662f\u6392\u67e5\u624b\u6bb5\u3002MySQL\/InnoDB\u5728\u65e9\u671f\u7684\u7248\u672c\u4e2d\uff0c\u5bf9\u4e8e\u9501\u95ee\u9898\u7684\u6392\u67e5\u624b\u6bb5\u662f\u6bd4\u8f83\u6709\u9650\u7684\uff0c\u800c\u4e14\u4e0e\u5f88\u591a\u7684\u914d\u7f6e\u53c2\u6570\u6709\u5173\uff0c\u6240\u4ee5\u4e86\u89e3\u8fd9\u4e9b\u53c2\u6570\uff0c\u719f\u6089MySQL\/InnoDB\u9501\u4fe1\u606f\u67e5\u770b\u7684\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5219\u662f\u53e6\u4e00\u4e2a\u9700\u8981\u4e86\u89e3\u7684\u3002 \u6240\u4ee5\uff0c\u5173\u4e8e InnoDB \u9501\u95ee\u9898\u7684\u4e5f\u5e76\u4e0d\u662f\u4e00\u4e24\u4e2a\u8bdd\u9898\u80fd\u591f\u8bf4\u6e05\u695a\u7684\u3002\u672c\u6587\u53ef\u80fd\u662f\u4e00\u4e2a\u7cfb\u5217\uff08\u7ed9\u81ea\u5df1\u6316\u5751\uff09\uff0c\u4e00\u4e2a\u81ea\u5df1\u5b66\u4e60\u4ee5\u53ca\u9501\u95ee\u9898\u6392\u67e5\u7ecf\u9a8c\u7684\u5206\u4eab\u3002 \u6784\u9020\u4e3b\u952e\u9501\u7ade\u4e89 \u8bb0\u5f55\u9501\uff0c\u5e94\u8be5\u662f InnoDB \u9501\u7c7b\u578b\u4e2d\u8f83\u4e3a\u5e38\u89c1\uff0c\u4e5f\u662f\u5728READ-COMMITTED\u4e8b\u52a1\u7ea7\u522b\u4e0b\uff0c\u6bd4\u8f83\u5bb9\u6613\u9047\u5230\u7684\u6b7b\u9501\u7c7b\u578b\uff08\u5982\u679c\u6709\u6b7b\u9501\u7684\u8bdd\uff09\u3002\u8fd9\u91cc\u901a\u8fc7\u89c2\u5bdf\u4e3b\u952e\u6b7b\u9501\u3001\u552f\u4e00\u952e\u6b7b\u9501\uff0c\u521d\u6b65\u4e86\u89e3 InnoDB \u9501\u4fe1\u606f\u5185\u5bb9\uff0c\u4ee5\u53ca\u7ed3\u6784\u3002 \u67e5\u770b\u5f53\u524d\u7684\u9694\u79bb\u7ea7\u522b \u51c6\u5907\u8868\u7ed3\u6784 \u6784\u5efa\u9501\u7b49\u5f85 \u5728\u4e24\u4e2a\u4f1a\u8bdd\u4e2d\uff0c\u6309\u4ece\u4e0a\u5230\u4e0b\uff0c\u6267\u884c\u4e0b\u8ff0\u7684SQL\uff1a \u65f6\u95f4\u987a\u5e8f Session A Session B 1 START TRANSACTION; 2 INSERT INTO t1 VALUES ( 1, &#8220;a&#8221;,12 ); 3 START TRANSACTION; 4 INSERT INTO t1 VALUES ( 1, &#8220;x&#8221;,23 ); \u8fd9\u65f6\u5019\uff0cSession [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"wp-custom-template-a-1440-px-width-template","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6,137],"tags":[],"class_list":["post-17797","post","type-post","status-publish","format-standard","hentry","category-mysql","category-learning-more"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/17797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=17797"}],"version-history":[{"count":33,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/17797\/revisions"}],"predecessor-version":[{"id":17882,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/17797\/revisions\/17882"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=17797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=17797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=17797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}