{"id":10006050,"date":"2022-07-29T05:43:13","date_gmt":"2022-07-28T21:43:13","guid":{"rendered":"https:\/\/ityet.com\/?p=10006050"},"modified":"2022-07-29T05:43:13","modified_gmt":"2022-07-28T21:43:13","slug":"polardb-x%e6%ba%90%e7%a0%81%e8%a7%a3%e8%af%bb%e7%b3%bb%e5%88%97%ef%bc%9adml%e4%b9%8binsert%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006050","title":{"rendered":"PolarDB-X\u6e90\u7801\u89e3\u8bfb\u7cfb\u5217\uff1aDML\u4e4bInsert\u6d41\u7a0b"},"content":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0Insert\u7c7b\u7684SQL\u8bed\u53e5\u7684\u6d41\u7a0b\u53ef\u521d\u7565\u5206\u4e3a\uff1a\u89e3\u6790\u3001\u6821\u9a8c\u3001\u4f18\u5316\u5668\u3001\u6267\u884c\u5668\u3001\u7269\u7406\u6267\u884c(GalaxyEngine\u6267\u884c)\u3002\u672c\u6587\u5c06\u4ee5\u4e00\u6761\u7b80\u5355\u7684Insert\u8bed\u53e5\u901a\u8fc7\u8c03\u8bd5\u7684\u65b9\u5f0f\u8fdb\u884c\u89e3\u8bfb\u3002\u5728\u9605\u8bfb\u672c\u6587\u4e4b\u524d\uff0c\u5f3a\u70c8\u5efa\u8bae\u5148\u9605\u8bfb\u300aPolarDB-X\u6e90\u7801\u89e3\u8bfb\u7cfb\u5217\uff1aSQL \u7684\u4e00\u751f\u300b\uff0c\u80fd\u591f\u4e86\u89e3\u4e00\u6761SQL\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u4e5f\u80fd\u77e5\u9053GalaxySQL(CN)\u7684\u5404\u4e2a\u7ec4\u4ef6\uff0c\u7136\u540e\u518d\u9605\u8bfb\u672c\u6587\uff0c\u4e86\u89e3Insert\u7684\u5177\u4f53\u5b9e\u73b0\u8fc7\u7a0b\uff0c\u52a0\u6df1\u5404\u4e2a\u7ec4\u4ef6\u7684\u7406\u89e3\u3002Insert\u7c7b\u7684SQL\u8bed\u53e5\u7684\u6d41\u7a0b\u53ef\u521d\u7565\u5206\u4e3a\uff1a\u89e3\u6790\u3001\u6821\u9a8c\u3001\u4f18\u5316\u5668\u3001\u6267\u884c\u5668\u3001\u7269\u7406\u6267\u884c(GalaxyEngine\u6267\u884c)\u3002\u672c\u6587\u5c06\u4ee5\u4e00\u6761\u7b80\u5355\u7684Insert\u8bed\u53e5\u901a\u8fc7\u8c03\u8bd5\u7684\u65b9\u5f0f\u8fdb\u884c\u89e3\u8bfb\u3002\u5efa\u8868\u8bed\u53e5\uff1a\u00a0#\u4e00\u4e2a\u7b80\u5355\u7684PolarDB-X\u4e2d\u7684\u5206\u5e93\u5206\u8868sbtestCREATE TABLE <code>sbtest<\/code> (<code>id<\/code> int(11) NOT NULL AUTO_INCREMENT,<code>k<\/code> int(11) NOT NULL DEFAULT &#8216;0&#8217;,<code>c<\/code> char(120) NOT NULL DEFAULT &#8221;,<code>pad<\/code> char(60) NOT NULL DEFAULT &#8221;,PRIMARY KEY (<code>id<\/code>))dbpartition by hash(<code>id<\/code>) tbpartition by hash(<code>id<\/code>) tbpartitions 2;\u00a0#\u8c03\u8bd5\u8bed\u53e5insert into sbtest(id) values(100);\u00a0\u89e3\u6790\u00a0\u8fde\u63a5\u4e0aPolarDB-X\u540e\uff0c\u6267\u884c\u4e00\u6761Insert\u8bed\u53e5insert into sbtest(id) values(100);PolarDB-X\u63a5\u6536\u5230\u8be5\u5b57\u7b26\u4e32\u8bed\u53e5\u540e\uff0c\u5f00\u59cb\u6267\u884c\u8be5SQL\uff0c\u53ef\u89c1TConnection#executeSQL\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237338\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u51c6\u5907\u6267\u884c\u8be5SQL\u8bed\u53e5\uff0cExecutionContext\u4f1a\u4fdd\u7559\u8be5Sql\u6267\u884c\u7684\u53c2\u6570\u3001\u914d\u7f6e\u3001\u7b49\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u8be5\u53d8\u91cf\u4f1a\u4e00\u76f4\u966a\u4f34\u8be5Sql\u7ecf\u8fc7\u89e3\u6790\u3001\u6821\u9a8c\u3001\u4f18\u5316\u5668\u3001\u6267\u884c\u5668\uff0c\u76f4\u5230\u4e0b\u53d1\u7ed9GalaxyEngine(DN)\u3002PolarDB-X\u6267\u884c\u8be5SQL\u65f6\uff0c\u9700\u8981\u5148\u83b7\u53d6\u6267\u884c\u8ba1\u5212\uff0c\u53ef\u89c1\u4ee3\u7801TConnection#executeQuery\uff1a\u00a0ExecutionPlan plan = Planner.getInstance().plan(sql, executionContext);\u00a0\u4e3a\u4e86\u907f\u514d\u6267\u884c\u540c\u4e00\u6761SQL\u6bcf\u6b21\u90fd\u8981\u89e3\u6790\u3001\u6821\u9a8c\u3001\u4f18\u5316\u5668\u7b49\u64cd\u4f5c\uff0cPolarDB-X\u5185\u7f6e\u4e86PlanCache\uff0c\u4f1a\u5728PlanCache\u4e2d\u83b7\u53d6\u8be5SQL\u7684\u6267\u884c\u8ba1\u5212\uff0c\u5f53\u7136\uff0c\u5e76\u4e0d\u662f\u6839\u636e\u7eaf\u5b57\u7b26\u4e32SQL\u6765\u8fdb\u884c\u7f13\u5b58\uff0c\u800c\u662f\u751f\u6210SqlParameterized\uff0c\u5982\u4e0b\u56fe\u6240\u793a(Planner#plan)\uff0c\u771f\u6b63\u7f13\u5b58\u7684\u662fsql\u6a21\u677f\uff0c\u8be5\u7c7b\u4e2d\u7684sql\u53d8\u91cf\uff1aINSERT INTO sbtest (id)\/nVALUES (?)\uff0c\u53ef\u9002\u7528\u4e8e\u7c7b\u4f3c\u7684\u8bed\u53e5\uff0c\uff1f\u4ee3\u8868\u53ef\u586b\u5165\u7684\u503c\uff0c\u4e0d\u540c\u7684\u503c\u90fd\u662f\u540c\u4e00\u7c7bSQL\u8bed\u53e5\u3002<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237339\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u5982\u679cPlanCache\u627e\u4e0d\u5230\u7684\u8bdd\uff0c\u9700\u8981\u751f\u6210\u65b0\u7684\u6267\u884c\u8ba1\u5212\uff0c\u5177\u4f53\u4ee3\u7801\u89c1PlanCache#getFromCache\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237340\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u5148\u5c06\u5b57\u7b26\u4e32\u901a\u8fc7FastsqlParser\u89e3\u6790\u6210\u62bd\u8c61\u8bed\u6cd5\u6811\uff0c\u68c0\u67e5\u6709\u6ca1\u6709\u8bed\u6cd5\u9519\u8bef\u7b49\uff0c\u751f\u6210SqlNode\uff0c\u672c\u6761SQL\u662fInsert\u8bed\u53e5\uff0c\u89e3\u6790\u6210SqlInsert\u7c7b\uff0c\u7136\u540e\u7ee7\u7eed\u6839\u636e\u62bd\u8c61\u8bed\u6cd5\u6811\u83b7\u53d6\u6267\u884c\u8ba1\u5212\uff0c\u5177\u4f53SqlInsert\u5185\u5bb9\u4e3a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237341\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u7b80\u5355\u89e3\u91ca\u51e0\u4e2a\u53d8\u91cf\uff1a\u00a0keywords\uff1a\u5173\u952e\u5b57\uff0c\u4f8b\u5982\uff1aInsert Ignore\u8bed\u53e5\u4f1a\u52a0Ignore\u5173\u952e\u5b57\uff0c\u4ee3\u8868\u8be5\u8bed\u53e5\u7279\u5f81\uff1bsource\uff1a\u6570\u636e\u6765\u6e90\uff0c\u63d2\u5165\u6570\u636e\u7684\u6765\u6e90\uff0c\u8fd9\u91cc\u662fvalues\uff0c\u5982\u679c\u662f Insert &#8230; Select\u8bed\u53e5\uff0c\u5219\u662fselect\u8bed\u53e5\uff1bupdateList\uff1a\u4fee\u6539\u4fe1\u606f\uff0c\u4f8b\u5982\uff1aInsert &#8230; ON DUPLICATE KEY \u8bed\u53e5\u4f1a\u628a\u4fee\u6539\u4fe1\u606f\u4fdd\u5b58\u5728\u8be5\u53d8\u91cf\uff1b\u00a0\u81f3\u6b64\uff0c\u5b8c\u6210\u4e86\u5b57\u7b26\u4e32SQL\u8bed\u53e5\u5230SqlNode\u7684\u8f6c\u53d8\uff0c\u5373\u5b8c\u6210\u4e86\u89e3\u6790\u90e8\u5206\u3002\u00a0\u6821\u9a8c\u00a0\u6821\u9a8c\u8fc7\u7a0b\u5373\u68c0\u67e5SqlNode\u7684\u8bed\u4e49\u662f\u5426\u6b63\u786e\uff0c\u4f8b\u5982\u8868\u662f\u5426\u5b58\u5728\u3001\u5217\u662f\u5426\u5b58\u5728\u3001\u7c7b\u578b\u662f\u5426\u6b63\u786e\u7b49\u7b49\uff0c\u5177\u4f53\u5165\u53e3\u5728Planner#getPlan\u51fd\u6570\u4e2d\uff1a\u00a0SqlNode validatedNode = converter.validate(ast);\u00a0\u4fbf\u662f\u9a8c\u8bc1\u8be5SQL\u7684\u6709\u6548\u6027\uff0cPolarDB-X\u6cbf\u7528\u4e86Apache Calcite\u6846\u67b6\uff0cvalidate\u7684\u5b9e\u73b0\u4e5f\u662f\u7c7b\u4f3c\u7684\u5927\u6846\u67b6\uff0c\u5305\u542bScope\u548cNamespace\u4e24\u4e2a\u6982\u5ff5\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u8fdb\u884c\u9a8c\u8bc1\uff0cSqlInsert\u7c7b\u578b\u7684\u9a8c\u8bc1\u5165\u53e3\u5728SqlValidatorImpl#validateInsert(SqlInsert insert)\u4e2d:\u00a0&#8230;final SqlValidatorNamespace targetNamespace = getNamespace(insert);validateNamespace(targetNamespace, unknownType);&#8230;final SqlNode source = insert.getSource();if (source instanceof SqlSelect) {\u00a0 \u00a0final SqlSelect sqlSelect = (SqlSelect) source;\u00a0 \u00a0validateSelect(sqlSelect, targetRowType);} else {\u00a0 \u00a0final SqlValidatorScope scope = scopes.get(source);\u00a0 \u00a0validateQuery(source, scope, targetRowType);}&#8230;\u00a0\u5927\u4f53\u6d41\u7a0b\u68c0\u67e5\u4e24\u4e2a\u90e8\u5206\uff1a\u9996\u5148\uff0c\u68c0\u67e5insert into sbtest\u8bed\u53e5\u662f\u5426\u6b63\u786e\uff1b\u7136\u540e\u68c0\u67e5SqlInsert.source\u90e8\u5206\u662f\u5426\u6709\u6548\u3002\u672c\u6761SQL\u662fValues\uff0c\u6240\u4ee5\u68c0\u67e5Values\u662f\u5426\u6709\u6548\uff0c\u5982\u679c\u662fInsert &#8230;Select\u8bed\u53e5\uff0csource\u662fSqlSelect\uff0c\u9700\u8981\u68c0\u67e5Select\u8bed\u53e5\u662f\u5426\u6709\u6548\u3002\u6ca1\u6709\u62a5\u9519\uff0c\u5219\u8bf4\u660eSQL\u8bed\u53e5\u8bed\u4e49\u6ca1\u6709\u9519\u8bef\uff0c\u6821\u9a8c\u901a\u8fc7\uff0c\u53ef\u4ee5\u53d1\u73b0\u8fd8\u662fSqlInsert\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237342\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u00a0\u4f18\u5316\u5668\u00a0\u5728\u7ecf\u8fc7\u4f18\u5316\u5668\u4e4b\u524d\uff0c\u8fd8\u9700\u8981\u5c06SqlNode(SqlInsert)\u8f6c\u6210RelNode\uff0c\u5927\u4f53\u542b\u4e49\u5c31\u662f\u5c06sql\u8bed\u6cd5\u6811\u8f6c\u6210\u5173\u7cfb\u8868\u8fbe\u5f0f\uff0c\u5165\u53e3\u5728Planner#getPlan\uff1a\u00a0RelNode relNode = converter.toRel(validatedNode, plannerContext);\u00a0\u5177\u4f53\u8f6c\u6362\u8fc7\u7a0b\u5728SqlConverter#toRel\uff1a\u00a0&#8230;final SqlToRelConverter sqlToRelConverter = new TddlSqlToRelConverter(&#8230;);RelRoot root = sqlToRelConverter.convertQuery(validatedNode, false, true);&#8230;\u00a0TddlSqlToRelConverter\u7c7b\u662fPolarDB-X\u7684\u8f6c\u6362\u5668\uff0c\u7ee7\u627fCalcite\u7684SqlToRelConverter\u7c7b\uff0c\u8f6c\u6362SqlInsert\u7684\u6267\u884c\u8fc7\u7a0b\u5728TddlSqlToRelConverter#convertInsert(SqlInsert call)\uff1a\u00a0RelNode relNode = super.convertInsert(call);if (relNode instanceof TableModify) {\u00a0 \u00a0&#8230;}\u00a0\u53ef\u4ee5\u53d1\u73b0\uff0c \u4f1a\u8c03\u7528SqlToRelConverter#convertInsert\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u4f1a\u5c06SqlInsert\u8f6c\u6210LogicalTableModify\uff0c\u8be5\u7c7b\u7684\u5185\u5bb9\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237343\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u53ef\u4ee5\u6ce8\u610f\u5230\u51e0\u4e2a\u53d8\u91cf\uff1aoperation\uff1a\u64cd\u4f5c\u7c7b\u578b\uff1binput\uff1a\u8f93\u5165\u6765\u6e90\uff0c\u672c\u6761sql\u662fvalues\uff1bPolarDB-X\u5185\u90e8\u8fd8\u6709\u65b0\u7684\u81ea\u5df1\u7684RelNode\uff0c\u6240\u4ee5\u8fd8\u4f1a\u628aRelNode\u518d\u8f6c\u6210\u81ea\u5df1\u5b9a\u4e49\u7684RelNode\uff0c\u5165\u53e3\u5728Planner#getPlan\uff1a\u00a0ToDrdsRelVisitor toDrdsRelVisitor = new ToDrdsRelVisitor(validatedNode, plannerContext);RelNode drdsRelNode = relNode.accept(toDrdsRelVisitor);\u00a0\u8f6c\u6362\u8fc7\u7a0b\u5728ToDrdsRelVisitor#visit(RelNode other)\uff1a\u00a0if ((other instanceof LogicalTableModify)) {&#8230;\u00a0 \u00a0 if (operation == TableModify.Operation.INSERT || &#8230;) {\u00a0 \u00a0 \u00a0 \u00a0 LogicalInsert logicalInsert = new LogicalInsert(modify);\u00a0 \u00a0 \u00a0 \u00a0 &#8230;\u00a0 \u00a0 }}\u00a0Insert\u7c7b\u578b\u4f1a\u8f6c\u6210LogicalInsert\uff0c\u5c31\u662fPolarDB-X\u5185\u90e8\u7684RelNode\uff0c\u6267\u884c\u4e5f\u662f\u57fa\u4e8e\u8be5\u7c7b\uff0cLogicalInsert\u7684\u5185\u5bb9\u5982\u4e0b\uff08\u8fd8\u6709\u90e8\u5206\u53d8\u91cf\u4e0d\u5728\u622a\u56fe\u4e2d\uff09\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237344\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u5927\u591a\u6570\u53d8\u91cf\u548cLogicalTableModify\u4e00\u6837\uff0c\u65b0\u589e\u4e86\u50cfPolarDB-X\u7279\u6709\u7684gsi\u76f8\u5173\u53d8\u91cf\u7b49\u7b49\u3002\u7136\u540e\u4fbf\u662f\u7ecf\u8fc7\u4f18\u5316\u5668\u9636\u6bb5\uff0c\u4f18\u5316\u5668\u6267\u884c\u8fc7\u7a0b\u4ee3\u7801\u5728Planner#sqlRewriteAndPlanEnumerate\uff1a\u00a0private RelNode sqlRewriteAndPlanEnumerate(RelNode input, PlannerContext plannerContext) {\u00a0 \u00a0CalcitePlanOptimizerTrace.getOptimizerTracer().get().addSnapshot(&#8220;Start&#8221;, input, plannerContext);\/\/RBO\u4f18\u5316\u00a0 \u00a0RelNode logicalOutput = optimizeBySqlWriter(input, plannerContext);\u00a0 \u00a0CalcitePlanOptimizerTrace.getOptimizerTracer().get()\u00a0 \u00a0 \u00a0 \u00a0.addSnapshot(&#8220;PlanEnumerate&#8221;, logicalOutput, plannerContext);\u00a0\u00a0 \u00a0\/\/CBO\u4f18\u5316\u00a0 \u00a0RelNode bestPlan = optimizeByPlanEnumerator(logicalOutput, plannerContext);\u00a0\u00a0 \u00a0\/\/ finally we should clear the planner to release memory\u00a0 \u00a0bestPlan.getCluster().getPlanner().clear();\u00a0 \u00a0bestPlan.getCluster().invalidateMetadataQuery();\u00a0 \u00a0return bestPlan;}\u00a0Insert\u7684\u4f18\u5316\u5668\u4e3b\u8981\u5728RBO\u8fc7\u7a0b\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e9b\u89c4\u5219\uff0cCBO\u89c4\u5219\u5bf9Insert\u51e0\u4e4e\u6ca1\u6709\u6539\u53d8\u3002\u53ef\u4ee5\u91cd\u70b9\u5173\u6ce8RBO\u7684OptimizeLogicalInsertRule\u89c4\u5219\uff0c\u4f1a\u6839\u636eGMS\uff08PolarDB-X\u7684\u5143\u6570\u636e\u7ba1\u7406\uff09\u7684\u4fe1\u606f\u6765\u5224\u65ad\u8be5SQL\u7684\u6267\u884c\u8ba1\u5212\uff0c\u53ef\u80fd\u4f1a\u5c06LogicalInsert\u8f6c\u53d8\u6210\u5176\u5b83\u7684RelNode\u53bb\u6267\u884c\uff0c\u65b9\u4fbf\u533a\u5206\u4e0d\u540c\u7684SQL\u6267\u884c\u65b9\u5f0f\uff0c\u9996\u5148\u4f1a\u786e\u5b9a\u8be5SQL\u7684\u6267\u884c\u7b56\u7565\uff0c\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd\uff1a\u00a0public enum ExecutionStrategy {\/<strong>\u00a0 \u00a0 <em> Foreach row, exists only one target partition.\u00a0 \u00a0 <\/em> Pushdown origin statement, with function call not pushable (like sequence call) replaced by RexCallParam.\u00a0 \u00a0 <em> Typical for single table and partitioned table without gsi.\u00a0 \u00a0 <\/em>\/\u00a0 \u00a0PUSHDOWN,\u00a0 \u00a0\/<\/strong>\u00a0 \u00a0 <em> Foreach row, might exists more than one target partition.\u00a0 \u00a0 <\/em> Pushdown origin statement, with nondeterministic function call replaced by RexCallParam.\u00a0 \u00a0 <em> Typical for broadcast table.\u00a0 \u00a0 <\/em>\/\u00a0 \u00a0DETERMINISTIC_PUSHDOWN,\u00a0 \u00a0\/<em>*\u00a0 \u00a0 <\/em> Foreach row, might exists more than one target partition, and data in different target partitions might be different.\u00a0 \u00a0 <em> Select then execute, with all function call replaced by RexCallParam.\u00a0 \u00a0 <\/em> Typical for table with gsi or table are doing scale out.\u00a0 \u00a0 *\/\u00a0 \u00a0LOGICAL;};\u00a0\u7531\u4e8e\u672c\u6761SQL\u8f83\u4e3a\u7b80\u5355\uff0c\u7b56\u7565\u662fPUSHDOWN\uff0c\u5904\u7406\u8fc7\u7a0b\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u7136\u540e\u751f\u6210InsertWriter\uff0c\u8be5\u7c7b\u8d1f\u8d23\u751f\u6210\u4e0b\u53d1\u5230DN\u7684SQL\u8bed\u53e5\uff0c\u4fdd\u5b58\u5728LogicalInsert\u4e2d\uff0cOptimizeLogicalInsertRule\u5904\u7406\u89c4\u5219\u8f83\u4e3a\u7ec6\u8282\uff0c\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u884c\u67e5\u770bonMatch\u65b9\u6cd5\u3002\u7ecf\u8fc7\u4f18\u5316\u5668\u540e\uff0c\u8fd8\u662fLogicalInsert\u7c7b\u7684RelNode\uff0c\u81f3\u6b64\uff0c\u610f\u5473\u7740\u4f18\u5316\u5668\u6267\u884c\u5b8c\u6bd5\u3002\u6700\u7ec8\u4f1a\u751f\u6210\u6267\u884c\u8ba1\u5212\uff0c\u5728PlanCache#getFromCache\uff0c\u89c1\u4e0b\u56fe\uff08\u56fe\u4e2d\u975e\u5168\u90e8\u53d8\u91cf\uff09\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237345\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>ExecutionPlan.plan\u5c31\u662f\u6267\u884c\u8ba1\u5212\uff0c\u53ef\u4ee5\u53d1\u73b0\u662fLogicalInsert\uff0c\u5bf9\u4e8e\u7b80\u5355\u7684Insert\uff0cPolarDB-X\u8fd8\u4f1a\u6539\u5199\u6267\u884c\u8ba1\u5212\uff0c\u4ee3\u7801\u5728PlanCache#getFromCache\uff1a\u00a0BuildFinalPlanVisitor visitor = new BuildFinalPlanVisitor(executionPlan.getAst(), plannerContext);executionPlan = executionPlan.copy(executionPlan.getPlan().accept(visitor));\u00a0insert into sbtest(id) values(100);\u8bed\u53e5\u6267\u884cBuildFinalPlanVisitor#buildNewPlanForInsert(LogicalInsert logicalInsert, ExecutionContext ec)\uff0c\u56e0\u4e3a\u8be5Insert\u8bed\u53e5\u6bd4\u8f83\u7b80\u5355\uff0c\u53ea\u6709\u4e00\u4e2avalues\uff0c\u5305\u542b\u62c6\u5206\u952e\u548cauto_increment\u5217\uff0c\u53ea\u9700\u8981\u6839\u636e\u62c6\u5206\u952e\u5c31\u80fd\u786e\u5b9a\u4e0b\u53d1\u5230DN\u7684\u54ea\u4e00\u4e2a\u5206\u7247\uff0c\u5728CN\u7aef\u65e0\u9700\u66f4\u591a\u64cd\u4f5c\uff0c\u6240\u4ee5\u4f1a\u7b80\u5316\u6267\u884c\u8ba1\u5212\uff0c\u5728BuildFinalPlanVisitor#buildSingleTableInsert\u8f6c\u6210SingleTableOperation\uff0c\u5e76\u4fdd\u5b58\u4e86\u5206\u5e93\u5206\u8868\u89c4\u5219\uff0c\u6700\u7ec8\u7684\u6267\u884c\u8ba1\u5212\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237346\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u6267\u884c\u8ba1\u5212\u53d8\u6210SingleTableOperation\uff0c\u81f3\u6b64\uff0c\u6267\u884c\u8ba1\u5212\u751f\u6210\u5b8c\u6bd5\u3002\u00a0\u6267\u884c\u5668\u00a0SQL\u8bed\u53e5\u751f\u6210\u6267\u884c\u8ba1\u5212\u540e\uff0c\u5c06\u7531\u6267\u884c\u5668\u8fdb\u884c\u6267\u884c\uff0c\u6267\u884c\u5165\u53e3\u5728TConnection#executeQuery\uff1a\u00a0ResultCursor resultCursor = executor.execute(plan, executionContext);\u00a0\u7136\u540e\u4f1a\u7531ExecutorHelper#execute\u65b9\u6cd5\u6267\u884cExecutionPlan.plan\uff0c\u4e5f\u5c31\u662f\u524d\u9762\u7684SingleTableOperation\uff0c\u6267\u884c\u7b56\u7565\u6709CURSOR\u3001TP_LOCAL\u3001AP_LOCAL\u3001MPP\uff0cInsert\u7c7b\u578b\u57fa\u672c\u90fd\u662f\u8d70CURSOR\uff0c\u63a5\u7740\u6839\u636e\u6267\u884c\u8ba1\u5212\u62ff\u5bf9\u5e94\u7684Handler\u8fdb\u884c\u5904\u7406\uff0c\u5177\u4f53\u53ef\u67e5\u770bCommandHandlerFactoryMyImp\u7c7b\uff0c\u4f8b\u5982\uff1aSingleTableOperation\u662fMySingleTableModifyHandler\uff0cLogicalInsert\u662fLogicalInsertHandler\u3002\u4f1a\u5728\u5bf9\u5e94\u7684Handler\u91cc\u9762\u8fdb\u884c\u6267\u884c\uff0c\u4e00\u822c\u4f1a\u8fd4\u56de\u4e00\u4e2aCursor\uff0cCursor\u91cc\u9762\u4f1a\u8c03\u7528\u771f\u6b63\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u8c03\u7528Cursor.next\u4fbf\u4f1a\u83b7\u53d6\u7ed3\u679c\uff0cInsert\u8bed\u53e5\u7684\u7ed3\u679c\u662faffect Rows\uff0c\u672c\u6761SQL\u4f1a\u521b\u5efa\u4e00\u4e2aMyPhyTableModifyCursor\uff0c\u5165\u53e3\u5728MySingleTableModifyHandler#handleInner\uff1a\u00a0&#8230;MyPhyTableModifyCursor modifyCursor = (MyPhyTableModifyCursor) repo.getCursorFactory().repoCursor(executionContext, logicalPlan);&#8230;affectRows = modifyCursor.batchUpdate();&#8230;\u00a0\u6839\u636eExecutionContext\u548cSingleTableOperation\u521b\u5efa\u4e00\u4e2aMyPhyTableModifyCursor\uff0c\u7136\u540e\u76f4\u63a5\u6267\u884c\uff1a\u00a0public int[] batchUpdate() {\u00a0 \u00a0try {\u00a0 \u00a0 \u00a0 \u00a0return handler.executeUpdate(this.plan);\u00a0 \u00a0} catch (SQLException e) {\u00a0 \u00a0 \u00a0 \u00a0throw GeneralUtil.nestedException(e);\u00a0 \u00a0}}\u00a0\u8fd9\u91cc\u7684this.plan\u5c31\u662fSingleTableOperation\uff0chandler\u662fPolarDB-X\u7684CN\u4e0eDN\u95f4\u4ea4\u4e92\u7684MyJdbcHandler\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u662f\u6267\u884c\u7269\u7406\u8ba1\u5212\u7684handler\uff0c\u4f1a\u6839\u636eplan\u751f\u6210\u771f\u6b63\u7684\u7269\u7406SQL\uff0c\u4e0b\u53d1\u5230DN\u6267\u884c\u3002\u7531\u4e8e\u8fd9\u6761SQL\u8f83\u4e3a\u7b80\u5355\uff0cCN\u4e0d\u9700\u8981\u8fc7\u591a\u5904\u7406\uff0c\u518d\u4e3e\u4e00\u4f8bInsert\u8bed\u53e5\uff1ainsert into sbtest(k) values(101),(102);\u7ecf\u8fc7\u4f18\u5316\u5668\u540e\uff0c\u8be5\u8bed\u53e5\u7684\u6267\u884c\u8ba1\u5212\u662fLogicalInsert\uff0c\u5982\u4e0b\u56fe\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237347\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u53ef\u4ee5\u53d1\u73b0sqlTemplate\u4e3aINSERT\/nINTO ? (id,k)\/nVALUES(?, ?)\uff0c\u8868\u540d\u53ef\u80fd\u8981\u6362\u6210\u7269\u7406\u8868\u540d\uff0c\u540c\u65f6\u589e\u52a0\u4e86\u4e00\u5217id\uff0c\u56e0\u4e3a\u8be5\u5217\u662fauto_increment\uff0c\u4f1a\u6709\u4e00\u4e2a\u5168\u5c40\u7684sequence\u8868\u6765\u8bb0\u5f55\u8be5\u5217\u7684\u503c\uff0c\u624d\u80fd\u4fdd\u8bc1\u5168\u5c40\u552f\u4e00\uff0c\u63d2\u5165\u7684values\u7684\u53c2\u6570\u4fdd\u7559\u5728ExecutionContext\u7684params\u4e2d\uff0c\u5982\u4e0b\u56fe\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237348\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>id\u5217\u7684\u503c\u4f1a\u5728\u771f\u6b63\u751f\u6210\u7269\u7406\u6267\u884c\u8ba1\u5212\u7684\u65f6\u5019\u624d\u4f1a\u53bb\u83b7\u53d6\uff0cLogicalInsert\u8ba1\u5212\u9002\u7528LogicalInsertHandler\u6765\u6267\u884c\uff0c\u6267\u884c\u8fc7\u7a0b\uff1a\u00a0public Cursor handle(RelNode logicalPlan, ExecutionContext executionContext){\u00a0 \u00a0&#8230;\u00a0 \u00a0LogicalInsert logicalInsert = (LogicalInsert) logicalPlan;\u00a0 \u00a0&#8230;\u00a0 \u00a0if (!logicalInsert.isSourceSelect()) {\u00a0 \u00a0 \u00a0 \u00a0affectRows = doExecute(logicalInsert, executionContext, handlerParams);\u00a0 \u00a0} else {\u00a0 \u00a0 \u00a0 \u00a0affectRows = selectForInsert(logicalInsert, executionContext, handlerParams);\u00a0 \u00a0}\u00a0 \u00a0&#8230;}\u00a0\u4f1a\u6839\u636e\u6765\u6e90\u662f\u5426\u662fSelect\u8bed\u53e5\u9009\u62e9\u4e0d\u540c\u7684\u6267\u884c\u65b9\u5f0f\uff0c\u5177\u4f53\u6267\u884c\u8fc7\u7a0b\u5728LogicalInsertHandler#executeInsert\uff0c\u5982\u4e0b\uff1a\u00a0&#8230;\/\/\u751f\u6210\u4e3b\u8868\u7684\u7269\u7406\u6267\u884c\u8ba1\u5212final InsertWriter primaryWriter = logicalInsert.getPrimaryInsertWriter();List inputs = primaryWriter.getInput(executionContext);&#8230;\/\/\u5982\u679c\u6709GSI\uff0c\u751f\u6210GSI\u8868\u7684\u7269\u7406\u6267\u884c\u8ba1\u5212final List gsiWriters = logicalInsert.getGsiInsertWriters();gsiWriters.stream().map(gsiWriter -&gt; gsiWriter.getInput(executionContext))&#8230;;&#8230;\/\/\u6267\u884c\u6240\u6709\u7269\u7406\u6267\u884c\u8ba1\u5212final int totalAffectRows = executePhysicalPlan(allPhyPlan, executionContext, schemaName, isBroadcast);&#8230;\u00a0\u4e3b\u8868\u751f\u6210\u7269\u7406\u6267\u884c\u8ba1\u5212\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u5148\u83b7\u53d6id\u7684\u503c\uff0c\u7531\u4e8eid\u4e5f\u662f\u62c6\u5206\u952e\uff0c\u6240\u4ee5\u4e24\u4e2avalues\u4f1a\u6839\u636e\u62c6\u5206\u952e\u5b9a\u4f4d\u5230\u4e0d\u540c\u7684\u7269\u7406\u5206\u5e93\u5206\u8868\u4e0a\uff0c\u4f1a\u751f\u6210\u6709\u4e24\u4e2a\u7269\u7406\u6267\u884c\u8ba1\u5212\uff0c\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237349\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042237350\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u5176\u4e2ddbIndex\u662f\u7269\u7406\u5e93\u540d\uff0ctableNames\u662f\u7269\u7406\u8868\u540d\uff0cparam\u4fdd\u5b58\u4e86\u8fd9\u6761slqTemplate\u7684\u53c2\u6570\u503c\uff0c\u586b\u5145\u4e0a\u5c31\u662f\u5b8c\u6574\u7684SQL\uff0c\u7136\u540e\u6267\u884c\u6240\u6709\u7269\u7406\u6267\u884c\u8ba1\u5212\uff0c\u5c31\u5b8c\u6210\u4e86\u8be5SQL\u7684\u6267\u884c\u3002\u00a0\u7269\u7406\u6267\u884c\u00a0PolarDB-X\u4e2dCN\u4e0eDN\u7684\u4ea4\u4e92\u90fd\u5728MyJdbcHandler\u4e2d\uff0c\u4ee5SingleTableOperation\u4e3a\u4f8b\uff0c\u770b\u770b\u5177\u4f53\u4ea4\u4e92\u8fc7\u7a0b\uff1a\u00a0public int[] executeUpdate(BaseQueryOperation phyTableModify) throws SQLException {&#8230;\u00a0 \u00a0\/\/\u83b7\u53d6\u7269\u7406\u6267\u884c\u8ba1\u5212\u7684\u5e93\u540d\u548c\u53c2\u6570\u00a0 \u00a0Pair&gt; dbIndexAndParam =\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0phyTableModify.getDbIndexAndParam(executionContext.getParams() == null ? null : executionContext.getParams()\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0.getCurrentParameter(), executionContext);&#8230;\u00a0 \u00a0\/\/\u6839\u636e\u5e93\u540d\u83b7\u53d6\u8fde\u63a5\u00a0 \u00a0connection = getPhyConnection(transaction, rw, groupName);&#8230;\u00a0 \u00a0 \/\/\u6839\u636e\u53c2\u6570\u7ec4\u6210\u5b57\u7b26\u4e32SQL\u00a0 \u00a0 String sql = buildSql(sqlAndParam.sql, executionContext);&#8230;\u00a0 \u00a0 \/\/\u6839\u636e\u8fde\u63a5\u521b\u5efaprepareStatement\u00a0 \u00a0 ps = prepareStatement(sql, connection, executionContext, isInsert, false);&#8230;\u00a0 \u00a0 \/\/\u8bbe\u7f6e\u53c2\u6570\u00a0 \u00a0 ParameterMethod.setParameters(ps, sqlAndParam.param);&#8230;\u00a0 \u00a0 \/\/\u6267\u884c\u00a0 \u00a0 affectRow = ((PreparedStatement) ps).executeUpdate();&#8230;}\u00a0\u5c06\u7269\u7406\u6267\u884c\u8ba1\u5212\u53d1\u9001\u5230DN\u6267\u884c\uff0c\u6267\u884c\u5b8c\u6210\u540e\uff0c\u6839\u636eaffectRow\u8fd4\u56de\u5230\u6267\u884c\u5668\uff0c\u6700\u7ec8\u4f1a\u628a\u7ed3\u679c\u8fd4\u56de\u7ed9\u7528\u6237\uff0c\u81f3\u6b64\uff0c\u4e00\u6761\u5b8c\u6574SQL\u5c31\u6267\u884c\u5b8c\u6210\u3002\u00a0\u5c0f\u7ed3\u00a0\u672c\u6587\u901a\u8fc7\u8c03\u8bd5\u7b80\u5355\u7684Insert\u8bed\u53e5\uff0c\u4ecb\u7ecd\u4e86PolarDB-X\u5728\u89e3\u6790\u3001\u6821\u9a8c\u3001\u4f18\u5316\u5668\u3001\u6267\u884c\u5668\u5bf9Insert\u8bed\u53e5\u7684\u5904\u7406\uff0c\u5f53\u7136\uff0cInsert\u8bed\u53e5\u4e5f\u6709\u5f88\u591a\u7279\u6b8a\u7684\u7528\u6cd5\uff0c\u672c\u6587\u5e76\u6ca1\u6709\u4e00\u4e00\u6982\u8ff0\uff0c\u611f\u5174\u8da3\u7684\u540c\u5b66\u53ef\u4ee5\u5728\u76f8\u5e94\u4ee3\u7801\u5904\u8fdb\u884c\u67e5\u770b\u3002\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=48QlGJupfu%2FzG7KmTpVctw%3D%3D.9pjcTm2NE7Uk0D%2Bbg7mSo7JSmaVdwAFBAF56SLEIMVsjtIoR%2FVuxYGIidPsji8mA\" rel=\"nofollow\">http:\/\/click.aliyun.com\/m\/100&#8230;<\/a>\u672c\u6587\u4e3a\u963f\u91cc\u4e91\u539f\u521b\u5185\u5bb9\uff0c\u672a\u7ecf\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042237336\">https:\/\/segmentfault.com\/a\/1190000042237336<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0Insert\u7c7b\u7684SQL\u8bed\u53e5\u7684\u6d41\u7a0b\u53ef\u521d\u7565\u5206\u4e3a\uff1a\u89e3\u6790\u3001\u6821\u9a8c\u3001\u4f18\u5316\u5668\u3001\u6267\u884c\u5668\u3001\u7269\u7406\u6267\u884c(GalaxyEngi [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006050","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006050"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006050\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}