{"id":5368,"date":"2020-07-29T23:49:02","date_gmt":"2020-07-29T15:49:02","guid":{"rendered":"http:\/\/39.97.231.10\/?p=5368"},"modified":"2020-07-29T23:49:02","modified_gmt":"2020-07-29T15:49:02","slug":"mysql-update-%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92%e4%bd%bf%e7%94%a8-index_merge-%e5%af%bc%e8%87%b4%e6%ad%bb%e9%94%81","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=5368","title":{"rendered":"MySQL update \u6267\u884c\u8ba1\u5212\u4f7f\u7528 index_merge \u5bfc\u81f4\u6b7b\u9501"},"content":{"rendered":"<div>\n<div>\n<div>\nMySQL\u6b7b\u9501\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u603b\u7684\u6765\u8bf4\uff0c\u8fd8\u662f\u56e0\u4e3a\u52a0\u9501\u7684\u987a\u5e8f\u4e0d\u4e00\u81f4\uff0c\u4e0b\u9762\u6765\u770b\u4e00\u4e2aindex_merge\u5bfc\u81f4\u7684\u6b7b\u9501\u3002<\/p>\n<h4>\u6b7b\u9501\u6848\u4f8b\uff1a<\/h4>\n<ul>\n<li>MySQL\u7248\u672c\uff1a5.7.19<\/li>\n<li>\u9694\u79bb\u7ea7\u522b\uff1a\u63d0\u4ea4\u8bfb\uff08RC\uff09<\/li>\n<\/ul>\n<p>\u5148\u770b\u8868\u7ed3\u6784\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE TABLE `t` (\n\u00a0 `id` bigint(20) NOT NULL AUTO_INCREMENT,\n\u00a0 `c1` varchar(50) DEFAULT NULL,\n\u00a0 `c2` varchar(50) DEFAULT NULL,\n\u00a0 `ts` datetime DEFAULT NULL,\n\u00a0 PRIMARY KEY (`id`),\n\u00a0 KEY `idx_c1` (`c1`),\n\u00a0 KEY `idx_c2` (`c2`),\n\u00a0 KEY `idx_ts` (`ts`)\n) ENGINE=InnoDB AUTO_INCREMENT=3279 DEFAULT CHARSET=utf8mb4\n<\/pre>\n<p>\u4e3b\u952eid\uff0c\u5b57\u6bb5c1,c2,ts \u90fd\u5efa\u6709\u4e8c\u7ea7\u7d22\u5f15\u3002<br \/>\n\u770b\u4e24\u4e2aSQL\uff1a<br \/>\nupdate t set ts=now() where c1=\\&#8217;100\\&#8217; and c2=\\&#8217;100\\&#8217;;<br \/>\nupdate t set ts=now() where c1=\\&#8217;100\\&#8217; and c2=\\&#8217;200\\&#8217;;<br \/>\n\u8fd9\u4e24\u4e2aSQL\u5728\u591a\u4e2a\u5e76\u53d1\u540c\u65f6\u6267\u884c\u7684\u60c5\u51b5\u4e0b\uff0c\u5f88\u9ad8\u7684\u6982\u7387\u4f1a\u53d1\u751f\u6b7b\u9501\u3002\u6b7b\u9501\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre>------------------------\nLATEST DETECTED DEADLOCK\n------------------------\n2020-01-18 11:26:16 0x7f375216a700\n*** (1) TRANSACTION:\nTRANSACTION 24049054, ACTIVE 0 sec starting index read\nmysql tables in use 3, locked 3\nLOCK WAIT 5 lock struct(s), heap size 1136, 4 row lock(s)\nMySQL thread id 1021, OS thread handle 139876759136000, query id 6147815 10.49.2.92 sndsadmin updating\nupdate db.t set ts=now() where c1=\\'100\\' and c2=\\'200\\'\n*** (1) WAITING FOR THIS LOCK TO BE GRANTED:\nRECORD LOCKS space id 23 page no 29 n bits 104 index PRIMARY of table `db`.`t` trx id 24049054 lock_mode X locks rec but not gap waiting\n*** (2) TRANSACTION:\nTRANSACTION 24049053, ACTIVE 0 sec starting index read, thread declared inside InnoDB 4999\nmysql tables in use 3, locked 3\n4 lock struct(s), heap size 1136, 3 row lock(s)\nMySQL thread id 244, OS thread handle 139875577145088, query id 6147814 10.49.2.92 sndsadmin updating\nupdate db.t set ts=now() where c1=\\'100\\' and c2=\\'100\\'\n*** (2) HOLDS THE LOCK(S):\nRECORD LOCKS space id 23 page no 29 n bits 104 index PRIMARY of table `db`.`t` trx id 24049053 lock_mode X locks rec but not gap\n*** (2) WAITING FOR THIS LOCK TO BE GRANTED:\nRECORD LOCKS space id 23 page no 12 n bits 952 index idx_c1 of table `db`.`t` trx id 24049053 lock_mode X locks rec but not gap waiting\n*** WE ROLL BACK TRANSACTION (2)\n<\/pre>\n<p>\u770b\u4e0b\u8868\u4e2d\u6570\u636e\u5206\u5e03\uff0c\u8868t\u4e2d\u4e00\u5171\u67092181\u6761\u8bb0\u5f55\uff0cc1\u503c\u4e3a100\u7684\u67094\u6761\u8bb0\u5f55\uff0cc2\u503c\u4e3a100\u7684\u67092\u6761\u8bb0\u5f55\uff0cc2\u503c\u4e3a200\u7684\u67093\u6761\u8bb0\u5f55\u3002\u5982\u4e0b\uff1a<\/p>\n<pre>mysql&gt; select count(*) from t;\n+----------+\n| count(*) |\n+----------+\n|\u00a0\u00a0\u00a0\u00a0 2181 |\n+----------+\nmysql&gt; select * from t where c1=\\'100\\';\n+------+------+------+---------------------+\n| id   | c1   | c2   | ts                  |\n+------+------+------+---------------------+\n| 3274 | 100  | 100  | 2020-01-18 11:26:16 |\n| 3275 | 100  | 200  | 2020-01-18 11:29:53 |\n| 3277 | 100  | 200  | 2020-01-18 11:29:53 |\n| 3278 | 100  | 100  | 2020-01-18 11:26:16 |\n+------+------+------+---------------------+\n4 rows in set (0.00 sec)\nmysql&gt; select * from t where c2=\\'100\\';\n+------+------+------+---------------------+\n| id   | c1   | c2   | ts                  |\n+------+------+------+---------------------+\n| 3274 | 100  | 100  | 2020-01-18 11:26:16 |\n| 3278 | 100  | 100  | 2020-01-18 11:26:16 |\n+------+------+------+---------------------+\n2 rows in set (0.00 sec)\nmysql&gt; select * from t where c2=\\'200\\';\n+------+------+------+---------------------+\n| id   | c1   | c2   | ts                  |\n+------+------+------+---------------------+\n| 3275 | 100  | 200  | 2020-01-18 11:30:07 |\n| 3276 | 200  | 200  | NULL                |\n| 3277 | 100  | 200  | 2020-01-18 11:30:07 |\n+------+------+------+---------------------+\n3 rows in set (0.00 sec)\n<\/pre>\n<p>c1,c2\u5b57\u6bb5\u4e0a\u5206\u522b\u5efa\u6709\u975e\u552f\u4e00\u7684\u4e8c\u7ea7\u7d22\u5f15\uff0cc1,c2\u6709\u503c\u76f8\u540c\u7684\u60c5\u51b5\u3002\u67e5\u770b\u4e0a\u8ff0\u4e24\u4e2aSQL\u7684\u6267\u884c\u8ba1\u5212\uff0c\u53d1\u73b0\u90fd\u4f7f\u7528\u4e86index_merge\uff0c\u5728Extra\u5217\u4f7f\u7528\u4e86Using intersect(idx_c2,idx_c1);<br \/>\nexplain update t set ts=now() where c1=\\&#8217;100\\&#8217; and c2=\\&#8217;100\\&#8217;;<br \/>\nexplain update t set ts=now() where c1=\\&#8217;100\\&#8217; and c2=\\&#8217;200\\&#8217;;<br \/>\n\u6267\u884c\u8ba1\u5212\u5982\u4e0b\uff1a<\/p>\n<pre>mysql&gt; explain update t set ts=now() where c1=\\'100\\' and c2=\\'100\\';\n+----+-------------+-------+------------+-------------+---------------+---------------+---------+------+------+----------+---------------------------------------------+\n| id | select_type | table | partitions | type        | possible_keys | key           | key_len | ref  | rows | filtered | Extra                                       |\n+----+-------------+-------+------------+-------------+---------------+---------------+---------+------+------+----------+---------------------------------------------+\n|  1 | UPDATE      | t     | NULL       | index_merge | idx_c1,idx_c2 | idx_c2,idx_c1 | 203,203 | NULL |    1 |   100.00 | Using intersect(idx_c2,idx_c1); Using where |\n+----+-------------+-------+------------+-------------+---------------+---------------+---------+------+------+----------+---------------------------------------------+\n<\/pre>\n<h4>\u6b7b\u9501\u5206\u6790\uff1a<\/h4>\n<p>\u672c\u6b21\u6b7b\u9501\u6848\u4f8b\u4e3b\u8981\u7531index_merge\u5bfc\u81f4\uff0c\u7531\u4e8e\u8868\u4e2d\u7d22\u5f15\u4e0d\u5408\u7406\uff0cMySQL\u4f18\u5316\u5668\u9009\u62e9\u4e86\u7d22\u5f15\u5408\u5e76\uff0c\u5373\u6839\u636ewhere\u6761\u4ef6\u5206\u522b\u8d70idx_c1, idx_c2\u4e24\u4e2a\u7d22\u5f15\uff0c\u518d\u5c06\u7ed3\u679c\u8fdb\u884c\u5408\u5e76\u3002update\u64cd\u4f5c\uff0c\u8d70\u4e86\u4e24\u4e2a\u4e8c\u7ea7\u7d22\u5f15\uff0c\u6839\u636e\u52a0\u9501\u987a\u5e8f\uff0c\u9664\u4e86\u5bf9\u4e8c\u7ea7\u7d22\u5f15\u52a0\u9501\u5916\uff0c\u8fd8\u8981\u5bf9\u4e3b\u952e\u7d22\u5f15\u8fdb\u884c\u52a0\u9501\uff0c\u591a\u5e76\u53d1\u6267\u884c\u6761\u4ef6\u4e0b\uff0c\u83b7\u53d6\u4e8c\u7ea7\u7d22\u5f15\u7684\u884c\u9501\u4e0e\u83b7\u53d6\u4e3b\u952e\u7d22\u5f15\u7684\u884c\u9501\u4e0d\u518d\u6709\u987a\uff0c\u6700\u7ec8\u5bfc\u81f4\u6b7b\u9501\u53d1\u751f\u3002<\/p>\n<h4>\u89e3\u51b3\u65b9\u6848\uff1a<\/h4>\n<ol>\n<li>\u589e\u52a0\u8054\u5408\u7d22\u5f15 idx_c1_c2(c1,c2)\uff0c\u4f18\u5316\u5668\u76f4\u63a5\u4f7f\u7528\u8054\u5408\u7d22\u5f15\uff0c\u4e0d\u518d\u4f7f\u7528index_merge\uff0c\u52a0\u9501\u987a\u5e8f\u4e00\u81f4\uff0c\u4e0d\u518d\u51fa\u73b0\u6b7b\u9501\u3002<\/li>\n<li>SQL\u8bed\u53e5\u4e2d\u5f3a\u5236\u8d70\u67d0\u4e2a\u7d22\u5f15\uff0c\u6bd4\u5982 idx_c1\u7d22\u5f15\u7684\u533a\u5206\u5ea6\u5df2\u7ecf\u975e\u5e38\u597d\u4e86\uff0c\u90a3\u4e48\u4f7f\u7528 force index(idx_c1)\uff0c\u8ba9\u4f18\u5316\u5668\u5f3a\u5236\u8d70idx_c1\uff0c\u4e5f\u4e0d\u518d\u4f1a\u51fa\u73b0\u6b7b\u9501\u3002<\/li>\n<li>\u5173\u6389\u53c2\u6570index_merge_intersection=off\uff0c\u7981\u7528index_merge\u529f\u80fd\u3002<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u6b7b\u9501\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u603b\u7684\u6765\u8bf4\uff0c\u8fd8\u662f\u56e0\u4e3a\u52a0\u9501\u7684\u987a\u5e8f\u4e0d\u4e00\u81f4\uff0c\u4e0b\u9762\u6765\u770b\u4e00\u4e2aindex_merge\u5bfc\u81f4\u7684\u6b7b\u9501\u3002 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=5368\"> <span class=\"screen-reader-text\">MySQL update \u6267\u884c\u8ba1\u5212\u4f7f\u7528 index_merge \u5bfc\u81f4\u6b7b\u9501<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"MySQL\u6b7b\u9501\u7684\u539f\u56e0\u6709\u5f88\u591a\uff0c\u603b\u7684\u6765\u8bf4\uff0c\u8fd8\u662f\u56e0\u4e3a\u52a0\u9501\u7684\u987a\u5e8f\u4e0d\u4e00\u81f4\uff0c\u4e0b\u9762\u6765\u770b\u4e00\u4e2aindex_merge\u5bfc\u81f4\u7684\u6b7b\u9501\u3002&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5368"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5368"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/5368\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5368"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}