{"id":3664,"date":"2019-01-02T19:11:43","date_gmt":"2019-01-02T11:11:43","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3664"},"modified":"2019-01-02T19:11:43","modified_gmt":"2019-01-02T11:11:43","slug":"%e5%86%8d%e8%ae%ae-innodb-%e6%98%af%e8%a1%8c%e7%ba%a7%e9%94%81%ef%bc%9f%e4%bb%8e%e4%b8%80%e6%9d%a1-update-%e8%af%ad%e5%8f%a5%e5%bc%95%e8%b5%b7%e7%9a%84%e6%ad%bb%e9%94%81%e9%97%ae%e9%a2%98%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3664","title":{"rendered":"\u518d\u8bae InnoDB \u662f\u884c\u7ea7\u9501\uff1f\u4ece\u4e00\u6761 update \u8bed\u53e5\u5f15\u8d77\u7684\u6b7b\u9501\u95ee\u9898\u6765\u62ef\u6551\u4f60\uff01"},"content":{"rendered":"<p>\n\t90% \u7684\u7a0b\u5e8f\u5458\u90fd\u8ba4\u4e3a innodb \u662f\u884c\u7ea7\u9501\uff0c\u4f46\u5b9e\u9645\u4e0a\u4f7f\u7528\u4e0d\u5f53\uff0c\u5b83\u4e5f\u662f\u8868\u7ea7\u9501\uff01\n<\/p>\n<p>\n\t\u770b\u8fc7\u6211\u535a\u5ba2\u7684\u7f51\u53cb\u90fd\u77e5\u9053\uff0c\u6211\u4e4b\u524d\u5199\u8fc7\u4e00\u7bc7\u6587\u7ae0\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3047\">MySQL InnoDB\u5b58\u50a8\u5f15\u64ceselect\u548cupdate\u67e5\u8be2\u963b\u585einsert\u8bed\u53e5,\u5f62\u6210\u8868\u7ea7\u9501<\/a>\u300b\u3002\u62ef\u6551\u8fc7\u4e0d\u5c11\u4eba\uff0c\u4eca\u5929\u6211\u4eec\u518d\u6765\u4e00\u6b21\u5927\u62ef\u6551\uff01\n<\/p>\n<p>\n\t\u6700\u8fd1\u751f\u4ea7\u4e0a\u7684 MySQL \u6570\u636e\u5e93\uff0c\u662f\u4e0d\u662f\u7684\u5c31\u6765\u4e00\u6b21 DeadLock\uff0c\u5176\u4e2d\u6211\u505a\u4e86\u6545\u969c\u6392\u67e5\uff0c\u6628\u5929\u505a\u4e86\u76f8\u5173\u7684\u5347\u7ea7\uff0c\u5bfc\u81f4\u6628\u5929\u975e\u5e38\u7684\u5fd9\uff0c\u5f88\u591a\u7f51\u53cb\u52a0\u6211\u597d\u53cb\uff0c\u90fd\u6ca1\u6709\u53ca\u65f6\u56de\u5e94\uff0c\u76f4\u5230\u665a\u4e0a\u5347\u7ea7\u7ed3\u675f\uff0c\u6211\u5728\u7fa4\u91cc\u505a\u4e86\u76f8\u5173\u7684\u89e3\u91ca\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"MySQL \u6b7b\u9501\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1fysg57wekoj20hu0dyjvy.jpg\" style=\"width: 642px;height: 502px\" \/>\n<\/p>\n<p>\n\t\u622a\u4e86\u4e00\u6bb5\u9519\u8bef\u65e5\u5fd7\u4fe1\u606f\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:plain;\">\r\n21:02:02.563 ERROR dao.CommonDao        [pool-15-thread-19] [jbc.trade.xttblog.com] [703c9ddbe4b143609035365ca46bff35] - db error , tableId=jbc.trade.xttblog.com, sql=update i_pay_record set checktime = now() where order_id in (62818) , par\r\nams=null\r\ncom.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction<\/pre>\n<p>\n\t\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u66f4\u65b0\u8bed\u53e5\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:sql;\">\r\nupdate i_pay_record set checktime = now() where order_id in (62818)<\/pre>\n<p>\n\t\u5f88\u5947\u5999\u5427\uff0c\u6267\u884c\u4e00\u6761 update sql \u7adf\u7136\u4f1a\u6709\u6b7b\u9501\uff1f\n<\/p>\n<p>\n\t\u5177\u4f53\u6b7b\u9501\u7684\u63d0\u524d\u662f&nbsp;i_pay_record \u8868\u4e2d\u7684 order_id \u5b57\u6bb5\u6709\u7d22\u5f15\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u65b0\u5efa\u4e00\u5f20\u8868 xttblog\uff0c\u6765\u8bf4\u660e\u95ee\u9898\u3002\n<\/p>\n<pre class=\"brush:sql;\">\r\nCREATE TABLE `xttblog` (\r\n  `id` BIGINT(20) NOT NULL,\r\n  `user_id` BIGINT(20) NOT NULL,\r\n  `item_id` BIGINT(20) NOT NULL,\r\n  `status` TINYINT(4) NOT NULL,\r\n  PRIMARY KEY (`id`),\r\n  KEY `idx_1` (`user_id`,`item_id`,`status`)\r\n) ENGINE=INNODB DEFAULT CHARSET=utf-8<\/pre>\n<p>\n\t\u5f53\u6211\u4eec\u6267\u884c\u4e0b\u9762\u7684 update \u8bed\u53e5\u65f6\uff0c\u5c31\u6709\u53ef\u80fd\u53d1\u751f\u6b7b\u9501\uff01\n<\/p>\n<pre class=\"brush:sql;\">\r\nupdate xttblog set status=1 where user_id=? and item_id=?<\/pre>\n<p>\n\tmysql \u7684\u4e8b\u52a1\u652f\u6301\u4e0e\u5b58\u50a8\u5f15\u64ce\u6709\u5173\uff0cMyISAM \u4e0d\u652f\u6301\u4e8b\u52a1\uff0cINNODB \u652f\u6301\u4e8b\u52a1\uff0c\u66f4\u65b0\u65f6\u53ef\u80fd\u91c7\u7528\u7684\u662f\u884c\u7ea7\u9501\uff0c\u4e5f\u53ef\u80fd\u662f\u8868\u7ea7\u9501\u3002\u6211\u4eec\u8fd9\u91cc\u91c7\u7528\u7684\u662f INNODB \u505a\u5b58\u50a8\u5f15\u64ce\uff0c\u610f\u5473\u7740\u4f1a\u5c06 update \u8bed\u53e5\u505a\u4e3a\u4e00\u4e2a\u4e8b\u52a1\u6765\u5904\u7406\u3002\u524d\u9762\u7684\u6587\u7ae0\u4e2d\u6211\u63d0\u5230\u4e86\u884c\u7ea7\u9501\u5fc5\u987b\u5efa\u7acb\u5728\u7d22\u5f15\u7684\u57fa\u7840\u4e0a\uff0c\u4e0a\u9762\u7684\u66f4\u65b0\u8bed\u53e5\u7528\u5230\u4e86\u7d22\u5f15 idx_1\uff0c\u6240\u4ee5\u8fd9\u91cc\u80af\u5b9a\u4f1a\u52a0\u4e0a\u884c\u7ea7\u9501\u3002\n<\/p>\n<p>\n\t\u884c\u7ea7\u9501\u5e76\u4e0d\u662f\u76f4\u63a5\u9501\u8bb0\u5f55\uff0c\u800c\u662f\u9501\u7d22\u5f15\uff08\u524d\u9762\u7684\u6587\u7ae0\u4e5f\u89e3\u91ca\u8fc7\uff09\u3002\n<\/p>\n<p>\n\t\u5982\u679c\u4e00\u6761 SQL \u8bed\u53e5\u7528\u5230\u4e86\u4e3b\u952e\u7d22\u5f15\uff0cmysql \u4f1a\u9501\u4f4f\u4e3b\u952e\u7d22\u5f15\uff1b\u5982\u679c\u4e00\u6761\u8bed\u53e5\u64cd\u4f5c\u4e86\u975e\u4e3b\u952e\u7d22\u5f15\uff0cmysql \u4f1a\u5148\u9501\u4f4f\u975e\u4e3b\u952e\u7d22\u5f15\uff0c\u518d\u9501\u5b9a\u4e3b\u952e\u7d22\u5f15\u3002\n<\/p>\n<p>\n\t\u8fd9\u4e2a update \u8bed\u53e5\u4f1a\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u7531\u4e8e\u7528\u5230\u4e86\u975e\u4e3b\u952e\u7d22\u5f15\uff0c\u9996\u5148\u9700\u8981\u83b7\u53d6 idx_1 \u4e0a\u7684\u884c\u7ea7\u9501\n\t<\/li>\n<li>\n\t\t\u7d27\u63a5\u7740\u6839\u636e\u4e3b\u952e\u8fdb\u884c\u66f4\u65b0\uff0c\u6240\u4ee5\u9700\u8981\u83b7\u53d6\u4e3b\u952e\u4e0a\u7684\u884c\u7ea7\u9501\n\t<\/li>\n<li>\n\t\t\u66f4\u65b0\u5b8c\u6bd5\u540e\uff0c\u63d0\u4ea4\uff0c\u5e76\u91ca\u653e\u6240\u6709\u9501\n\t<\/li>\n<\/ol>\n<p>\n\t\u5982\u679c\u5728\u6b65\u9aa4 1 \u548c 2 \u4e4b\u95f4\u7a81\u7136\u63d2\u5165\u4e00\u6761\u8bed\u53e5\uff1aupdate xttblog &#8230;..where id=? and user_id=? \u8fd9\u6761\u8bed\u53e5\uff0c\u90a3\u4e48\u4f1a\u5148\u9501\u4f4f\u4e3b\u952e\u7d22\u5f15\uff0c\u7136\u540e\u9501\u4f4f idx_1\u3002\n<\/p>\n<p>\n\t\u8fd9\u65f6\uff0c\u60b2\u5267\u5c31\u53d1\u751f\u4e86\uff01\n<\/p>\n<p>\n\t\u4e00\u6761\u8bed\u53e5\u83b7\u53d6\u4e86 idx_1 \u4e0a\u7684\u9501\uff0c\u7b49\u5f85\u4e3b\u952e\u7d22\u5f15\u4e0a\u7684\u9501\uff1b\u53e6\u4e00\u6761\u8bed\u53e5\u83b7\u53d6\u4e86\u4e3b\u952e\u4e0a\u7684\u9501\uff0c\u7b49\u5f85 idx_1 \u4e0a\u7684\u9501\uff0c\u8fd9\u6837\u5c31\u51fa\u73b0\u4e86\u6b7b\u9501\u3002\n<\/p>\n<p>\n\t\u5f88\u60ca\u5947\u5427\uff0c\u5176\u5b9e\u4e00\u70b9\u4e5f\u4e0d\u5947\u602a\uff0c\u53ea\u8981\u4f60\u4e86\u89e3\u4e86 MySQL \u7684\u4e00\u4e9b\u5e95\u5c42\u8bbe\u8ba1\u539f\u7406\uff01\n<\/p>\n<p>\n\t\u90a3\u4e48\u53d1\u751f\u8fd9\u79cd\u95ee\u9898\uff0c\u6709\u89e3\u51b3\u65b9\u6848\u5417\uff1f\n<\/p>\n<p>\n\t\u5f53\u7136\u6709\u4e86\uff0c\u8981\u4e0d\u7136\u6211\u5199\u8fd9\u7bc7\u6587\u7ae0\u5e72\u4ec0\u4e48\uff1f\n<\/p>\n<p>\n\t\u89e3\u51b3\u65b9\u6848\uff0c\u6700\u7b28\u6700\u9760\u8c31\u7684\u505a\u6cd5\u5c31\u662f\uff1a\u5148\u83b7\u53d6\u9700\u8981\u66f4\u65b0\u7684\u8bb0\u5f55\u7684\u4e3b\u952e\uff0c\u7136\u540e\u518d\u9010\u6761\u66f4\u65b0\uff01\n<\/p>\n<pre class=\"brush:sql;\">\r\n-- \u5148\u83b7\u53d6\u9700\u8981\u66f4\u65b0\u7684\u8bb0\u5f55\u7684\u4e3b\u952e\r\nselect id from user_item where user_id=? and item_id=?\r\n-- \u9010\u6761\u66f4\u65b0\r\nselect id from user_item where user_id=? and item_id=?  \r\nfor (Long id : idList) {  \r\n    userItemDAO.updateStatus(id, userId, 1);  \r\n}  \r\nupdate user_item set status=? where id=? and user_id=?<\/pre>\n<p>\n\t\u8fd9\u6837\u5c31\u53ef\u4ee5\u89e3\u51b3\u95ee\u9898\u4e86\uff0c\u4f46\u662f\u8fd9\u4e2a\u89e3\u51b3\u65b9\u6848\u4e0e\u5148\u524d\u7684\u66f4\u65b0\u8bed\u53e5\u4e0d\u4e00\u6837\uff0c\u5148\u524d\u7684\u66f4\u65b0\u8bed\u53e5\u5bf9\u6240\u6709\u8bb0\u5f55\u7684\u66f4\u65b0\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u91c7\u7528\u5faa\u73af\u66f4\u65b0\u540e\u5e76\u4e0d\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u6240\u4ee5\u5728 for \u5faa\u73af\u5916\u9762\u8fd8\u5f97\u5f00\u4e00\u4e2a\u4e8b\u52a1\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nException e = (Exception)getDbfeelTransactionTemplate().execute(new TransactionCallback() {\r\n    public Object doInTransaction(TransactionStatus status) {\r\n        try {\r\n              for(Long id:idList) {\r\n                userItemDAO.updateStatus(id,userId,1);\r\n            }\r\n            return null;\r\n        }catch(DAOException e) {\r\n            status.setRollbackOnly();\r\n            return e;\r\n        }\r\n        catch (Exception e) {\r\n            status.setRollbackOnly();\r\n            return e;\r\n        }\r\n    }\r\n});<\/pre>\n<p>\n\t\u5728\u91c7\u7528 INNODB \u7684 MySQL \u4e2d\uff0c\u66f4\u65b0\u64cd\u4f5c\u9ed8\u8ba4\u4f1a\u52a0\u884c\u7ea7\u9501\uff0c\u884c\u7ea7\u9501\u662f\u57fa\u4e8e\u7d22\u5f15\u7684\uff0c\u5728\u5206\u6790\u6b7b\u9501\u4e4b\u524d\u9700\u8981\u67e5\u8be2\u4e00\u4e0b mysql \u7684\u6267\u884c\u8ba1\u5212\uff0c\u770b\u770b\u662f\u5426\u7528\u5230\u4e86\u7d22\u5f15\uff0c\u7528\u5230\u4e86\u54ea\u4e2a\u7d22\u5f15\uff0c\u5bf9\u4e8e\u6ca1\u6709\u7528\u7d22\u5f15\u7684\u64cd\u4f5c\u4f1a\u91c7\u7528\u8868\u7ea7\u9501\u3002\u5982\u679c\u64cd\u4f5c\u7528\u5230\u4e86\u4e3b\u952e\u7d22\u5f15\u4f1a\u5148\u5728\u4e3b\u952e\u7d22\u5f15\u4e0a\u52a0\u9501\uff0c\u7136\u540e\u5728\u5176\u4ed6\u7d22\u5f15\u4e0a\u52a0\u9501\uff0c\u5426\u5219\u52a0\u9501\u987a\u5e8f\u76f8\u53cd\u3002\u5728\u5e76\u53d1\u5ea6\u9ad8\u7684\u5e94\u7528\u4e2d\uff0c\u6279\u91cf\u66f4\u65b0\u4e00\u5b9a\u8981\u5e26\u4e0a\u8bb0\u5f55\u7684\u4e3b\u952e\uff0c\u4f18\u5148\u83b7\u53d6\u4e3b\u952e\u4e0a\u7684\u9501\uff0c\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u6b7b\u9501\u7684\u53d1\u751f\u3002\n<\/p>\n<p>\n\t\u4e0d\u662f\u8bf4 update \u4e0d\u4f1a\u53d1\u751f\u6b7b\u9501\uff0c\u800c\u662f\u4f60\u7684\u7a0b\u5e8f\u6ca1\u9047\u5230\u9ad8\u5e76\u53d1\u800c\u5df2\uff01\u5173\u4e8e\u6b7b\u9501\u7684\u6545\u969c\u5206\u6790\u6392\u67e5\uff0c\u6211\u4eec\u4ee5\u540e\u7ee7\u7eed\uff01<\/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=3664\">\u518d\u8bae InnoDB \u662f\u884c\u7ea7\u9501\uff1f\u4ece\u4e00\u6761 update \u8bed\u53e5\u5f15\u8d77\u7684\u6b7b\u9501\u95ee\u9898\u6765\u62ef\u6551\u4f60\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>90% \u7684\u7a0b\u5e8f\u5458\u90fd\u8ba4\u4e3a innodb \u662f\u884c\u7ea7\u9501\uff0c\u4f46\u5b9e\u9645\u4e0a\u4f7f\u7528\u4e0d\u5f53\uff0c\u5b83\u4e5f\u662f\u8868\u7ea7\u9501\uff01 \u770b\u8fc7\u6211\u535a\u5ba2\u7684\u7f51\u53cb\u90fd\u77e5\u9053\uff0c\u6211\u4e4b [&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":[1674,83,157,155,1866],"class_list":["post-3664","post","type-post","status-publish","format-standard","hentry","category-sql","tag-innodb","tag-mysql","tag-sql","tag-update"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3664","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=3664"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3664\/revisions"}],"predecessor-version":[{"id":3665,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3664\/revisions\/3665"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}