{"id":6999,"date":"2024-11-14T10:48:59","date_gmt":"2024-11-14T02:48:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6999\/"},"modified":"2024-11-14T10:48:59","modified_gmt":"2024-11-14T02:48:59","slug":"springboot-mybatis-mysql-%e6%89%b9%e9%87%8f%e6%96%b0%e5%a2%9e%e6%95%b0%e6%8d%ae%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d-oom%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6999\/","title":{"rendered":"SpringBoot + Mybatis + MySQL \u6279\u91cf\u65b0\u589e\u6570\u636e\u5982\u4f55\u907f\u514d OOM\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173026379245453.jpg\" class=\"aligncenter\" title=\"SpringBoot + Mybatis + MySQL \u6279\u91cf\u65b0\u589e\u6570\u636e\u5982\u4f55\u907f\u514d OOM\uff1f\u63d2\u56fe\" alt=\"SpringBoot + Mybatis + MySQL \u6279\u91cf\u65b0\u589e\u6570\u636e\u5982\u4f55\u907f\u514d OOM\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>springboot + mybatis +  \u6279\u91cf\u65b0\u589e\u6570\u636e\u907f\u514d oom<\/strong><\/p>\n<p>\u5728\u6279\u91cf\u63d2\u5165\u5927\u91cf\u6570\u636e\u5230 mysql \u6570\u636e\u5e93\u65f6\uff0c\u5f88\u5bb9\u6613\u51fa\u73b0 oom\uff08outofmemory\uff09\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u7b56\u7565\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5bf9\u4f20\u8fdb\u6765\u7684\u6570\u636e\u8fdb\u884c\u7ec6\u5206\u5904\u7406\u3002\u4f8b\u5982\uff0c\u6bcf 1w \u6761\u6570\u636e\u63d2\u5165\u4e00\u6b21\u3002\u8fd9\u53ef\u4ee5\u663e\u7740\u51cf\u5c11\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684\u6570\u636e\u91cf\u3002 <\/p>\n<p>\u5176\u6b21\uff0c\u4f18\u5316\u4ee3\u7801\u3002\u5728\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u5bf9temp \u53d8\u91cf\u7684\u5904\u7406\u8fdb\u884c\u7b80\u5316\uff1a<\/p>\n<pre>for (int i = 0; i &lt; max; i += count) {\n    list&lt;integer&gt; list1 = list.sublist(i, math.min(i + count, max));\n    usermapper.insert(list1);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53e6\u5916\uff0c\u66f4\u597d\u7684\u5904\u7406\u65b9\u6cd5\u662f\u4f7f\u7528\u5206\u6279\u4fdd\u5b58\u7b56\u7565\uff0c\u5373\u5c06\u6570\u636e\u5206\u6279\u5b58\u50a8\u5728\u4e34\u65f6\u8868\u4e2d\uff0c\u7136\u540e\u5c06\u4e34\u65f6\u8868\u7684\u6570\u636e\u6279\u91cf\u5bfc\u5165\u5230\u76ee\u6807\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d oom \u9519\u8bef\uff0c\u5e76\u4e14\u6548\u7387\u66f4\u9ad8\u3002 <\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ \u521b\u5efa\u4e34\u65f6\u8868\nCREATE TABLE tmp_user (\n    id INT NOT NULL,\n    name VARCHAR(255) NOT NULL\n);\n\n\/\/ \u6279\u91cf\u63d2\u5165\u6570\u636e\u5230\u4e34\u65f6\u8868\nList&lt;Integer&gt; idList = new ArrayList&lt;&gt;();\nList&lt;String&gt; nameList = new ArrayList&lt;&gt;();\nfor (User user : list) {\n    idList.add(user.getId());\n    nameList.add(user.getName());\n}\nString sql = \"INSERT INTO tmp_user (id, name) VALUES (?, ?)\";\njdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {\n    @Override\n    public void setValues(PreparedStatement ps, int i) throws SQLException {\n        ps.setInt(1, idList.get(i));\n        ps.setString(2, nameList.get(i));\n    }\n\n    @Override\n    public int getBatchSize() {\n        return idList.size();\n    }\n});\n\n\/\/ \u5c06\u4e34\u65f6\u8868\u6570\u636e\u5bfc\u5165\u76ee\u6807\u8868\nsql = \"INSERT INTO user (id, name) SELECT id, name FROM tmp_user\";\njdbcTemplate.execute(sql);\n\n\/\/ \u5220\u9664\u4e34\u65f6\u8868\nDROP TABLE tmp_user;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u5206\u6279\u4fdd\u5b58\u7b56\u7565\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d oom \u9519\u8bef\uff0c\u5e76\u4e14\u63d0\u9ad8\u6279\u91cf\u63d2\u5165\u6570\u636e\u7684\u6548\u7387\u3002 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSpringBoot + Mybatis + MySQL \u6279\u91cf\u65b0\u589e\u6570\u636e\u5982\u4f55\u907f\u514d OOM\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>springboot + mybatis + \u6279\u91cf\u65b0\u589e\u6570\u636e\u907f\u514d oom \u5728\u6279\u91cf\u63d2\u5165\u5927\u91cf\u6570\u636e\u5230 mysql \u6570\u636e\u5e93\u65f6\uff0c\u5f88\u5bb9\u6613\u51fa\u73b0 oom\uff08outofmemory\uff09\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u7b56\u7565\uff1a \u9996\u5148\uff0c\u5bf9\u4f20\u8fdb\u6765\u7684\u6570\u636e\u8fdb\u884c\u7ec6\u5206\u5904\u7406\u3002\u4f8b\u5982\uff0c\u6bcf 1w \u6761\u6570\u636e\u63d2\u5165\u4e00\u6b21\u3002\u8fd9\u53ef\u4ee5\u663e\u7740\u51cf\u5c11\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684\u6570\u636e\u91cf\u3002 \u5176\u6b21\uff0c\u4f18\u5316\u4ee3\u7801\u3002\u5728\u7ed9\u51fa\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u5bf9temp \u53d8\u91cf\u7684\u5904\u7406\u8fdb\u884c\u7b80\u5316\uff1a for (int i = 0; i &lt; max; i += count) { list&lt;integer&gt; list1 = list.sublist(i, math.min(i + count, max)); usermapper.insert(list1); } \u767b\u5f55\u540e\u590d\u5236 \u53e6\u5916\uff0c\u66f4\u597d\u7684\u5904\u7406\u65b9\u6cd5\u662f\u4f7f\u7528\u5206\u6279\u4fdd\u5b58\u7b56\u7565\uff0c\u5373\u5c06\u6570\u636e\u5206\u6279\u5b58\u50a8\u5728\u4e34\u65f6\u8868\u4e2d\uff0c\u7136\u540e\u5c06\u4e34\u65f6\u8868\u7684\u6570\u636e\u6279\u91cf\u5bfc\u5165\u5230\u76ee\u6807\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d oom \u9519\u8bef\uff0c\u5e76\u4e14\u6548\u7387\u66f4\u9ad8\u3002 \u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a \/\/ \u521b\u5efa\u4e34\u65f6\u8868 CREATE TABLE tmp_user ( id INT NOT NULL, name VARCHAR(255) NOT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6999","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6999"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6999\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}