{"id":10006793,"date":"2022-11-03T21:49:48","date_gmt":"2022-11-03T13:49:48","guid":{"rendered":"https:\/\/ityet.com\/?p=10006793"},"modified":"2022-11-03T21:49:48","modified_gmt":"2022-11-03T13:49:48","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%8b-insert-ignore-%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006793","title":{"rendered":"PolarDB-X \u6e90\u7801\u89e3\u8bfb\u7cfb\u5217\uff1aDML \u4e4b INSERT IGNORE \u6d41\u7a0b"},"content":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u672c\u6587\u5c06\u8fdb\u4e00\u6b65\u4ecb\u7ecd PolarDB-X \u4e2d INSERT IGNORE \u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5176\u6839\u636e\u63d2\u5165\u7684\u8868\u662f\u5426\u6709 GSI \u4e5f\u6709\u6240\u53d8\u5316\u3002\u4f5c\u8005\uff1a\u6f5c\u749f\u5728\u4e0a\u4e00\u7bc7\u6e90\u7801\u9605\u8bfb\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 INSERT \u7684\u6267\u884c\u6d41\u7a0b\u3002\u800c INSERT IGNORE \u4e0e INSERT \u4e0d\u540c\uff0c\u9700\u8981\u5bf9\u63d2\u5165\u503c\u5224\u65ad\u662f\u5426\u6709 Unique Key \u7684\u51b2\u7a81\uff0c\u5e76\u5ffd\u7565\u6709\u51b2\u7a81\u7684\u63d2\u5165\u503c\u3002\u56e0\u6b64\u672c\u6587\u5c06\u8fdb\u4e00\u6b65\u4ecb\u7ecd PolarDB-X \u4e2d INSERT IGNORE \u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5176\u6839\u636e\u63d2\u5165\u7684\u8868\u662f\u5426\u6709 GSI \u4e5f\u6709\u6240\u53d8\u5316\u3002\u4e0b\u63a8\u6267\u884c\u5982\u679c\u63d2\u5165\u7684\u8868\u53ea\u6709\u4e00\u5f20\u4e3b\u8868\uff0c\u6ca1\u6709 GSI\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u5c06 INSERT IGNORE \u76f4\u63a5\u53d1\u9001\u5230\u5bf9\u5e94\u7684\u7269\u7406\u8868\u4e0a\uff0c\u7531 DN \u81ea\u884c\u5ffd\u7565\u5b58\u5728\u51b2\u7a81\u7684\u503c\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cINSERT IGNORE \u7684\u6267\u884c\u8fc7\u7a0b\u548c INSERT \u57fa\u672c\u4e0a\u76f8\u540c\uff0c\u8bfb\u8005\u53ef\u4ee5\u53c2\u8003\u4e4b\u524d\u7684\u6e90\u7801\u9605\u8bfb\u6587\u7ae0\u3002\u903b\u8f91\u6267\u884c\u800c\u5728\u6709 GSI \u7684\u60c5\u51b5\u4e0b\uff0c\u5c31\u4e0d\u80fd\u7b80\u5355\u5730\u5c06 INSERT IGNORE \u5206\u522b\u4e0b\u53d1\u5230\u4e3b\u8868\u548c GSI \u5bf9\u5e94\u7684\u7269\u7406\u5206\u8868\u4e0a\uff0c\u5426\u5219\u6709\u53ef\u80fd\u51fa\u73b0\u4e3b\u8868\u548c GSI \u6570\u636e\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1acreate table t1 (a int primary key, b int, global index g1(b) dbpartition by hash(b)) dbpartition by hash(a);<br \/>insert ignore into t1 values (1,1),(1,2);\u5bf9\u4e8e\u63d2\u5165\u7684\u4e24\u6761\u8bb0\u5f55\uff0c\u5b83\u4eec\u5728\u4e3b\u8868\u4e0a\u4f4d\u4e8e\u540c\u4e00\u4e2a\u7269\u7406\u8868\uff08a \u76f8\u540c\uff09\uff0c\u4f46\u662f\u5728 GSI \u4e0a\u4f4d\u4e8e\u4e0d\u540c\u7684\u7269\u7406\u8868\uff08b \u4e0d\u76f8\u540c\uff09\uff0c\u5982\u679c\u76f4\u63a5\u4e0b\u53d1 INSERT IGNORE \u7684\u8bdd\uff0c\u4e3b\u8868\u4e0a\u53ea\u6709 (1,1) \u80fd\u591f\u6210\u529f\u63d2\u5165\uff08\u4e3b\u952e\u51b2\u7a81\uff09\uff0c\u800c\u5728 GSI \u4e0a (1,1) \u548c (1,2) \u90fd\u80fd\u6210\u529f\u63d2\u5165\uff0c\u4e8e\u662f GSI \u6bd4\u4e3b\u8868\u591a\u4e86\u4e00\u6761\u6570\u636e\u3002\u9488\u5bf9\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u662f\u6839\u636e\u63d2\u5165\u503c\u4e2d\u7684 Unique Key\uff0c\u5148\u5230\u6570\u636e\u5e93\u4e2d SELECT \u51fa\u6709\u53ef\u80fd\u51b2\u7a81\u7684\u6570\u636e\u5230 CN\uff0c\u7136\u540e\u5728 CN \u5224\u65ad\u51b2\u7a81\u7684\u503c\u5e76\u5220\u9664\u3002\u8fdb\u884c SELECT \u7684\u65f6\u5019\uff0c\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u5c06\u6240\u6709\u7684 SELECT \u76f4\u63a5\u53d1\u9001\u5230\u4e3b\u8868\u4e0a\uff0c\u4f46\u662f\u4e3b\u8868\u4e0a\u53ef\u80fd\u6ca1\u6709\u5bf9\u5e94\u7684 Unique Key\uff0c\u8fd9\u5c31\u5bfc\u81f4 SELECT \u7684\u65f6\u5019\u4f1a\u8fdb\u884c\u5168\u8868\u626b\u63cf\uff0c\u5f71\u54cd\u6027\u80fd\u3002\u6240\u4ee5\u5728\u4f18\u5316\u5668\u9636\u6bb5\uff0c\u6211\u4eec\u4f1a\u6839\u636e Unique Key \u662f\u5728\u4e3b\u8868\u8fd8\u662f GSI \u4e0a\u5b9a\u4e49\u7684\u6765\u786e\u5b9a\u76f8\u5e94\u7684 SELECT \u9700\u8981\u53d1\u9001\u5230\u4e3b\u8868\u8fd8\u662f GSI\uff0c\u5177\u4f53\u4ee3\u7801\u4f4d\u7f6e\uff1acom.alibaba.polardbx.optimizer.core.planner.rule.OptimizeLogicalInsertRule#groupUkByTableprotected Map&lt;String, List&lt;List&lt;String&gt;&gt;&gt; groupUkByTable(LogicalInsertIgnore insertIgnore,<\/p>\n<pre><code>                                                         ExecutionContext executionContext) {\n    \/\/ \u627e\u5230\u6bcf\u4e2a Unique Key \u5728\u4e3b\u8868\u548c\u54ea\u4e9b GSI \u4e2d\u5b58\u5728\n    Map&lt;Integer, List&lt;String&gt;&gt; ukAllTableMap = new HashMap&lt;&gt;();\n    for (int i = 0; i &lt; uniqueKeys.size(); i++) {\n        List&lt;String&gt; uniqueKey = uniqueKeys.get(i);\n        for (Map.Entry&lt;String, Map&lt;String, Set&lt;String&gt;&gt;&gt; e : writableTableUkMap.entrySet()) {\n            String currentTableName = e.getKey().toUpperCase();\n            Map&lt;String, Set&lt;String&gt;&gt; currentUniqueKeys = e.getValue();\n            boolean found = false;\n            for (Set&lt;String&gt; currentUniqueKey : currentUniqueKeys.values()) {\n                if (currentUniqueKey.size() != uniqueKey.size()) {\n                    continue;\n                }\n                boolean match = currentUniqueKey.containsAll(uniqueKey);\n                if (match) {\n                    found = true;\n                    break;\n                }\n            }\n            if (found) {\n                ukAllTableMap.computeIfAbsent(i, k -&gt; new ArrayList&lt;&gt;()).add(currentTableName);\n            }\n        }\n    }\n\n    \/\/ \u786e\u5b9a\u662f\u5728\u54ea\u4e00\u4e2a\u8868\u4e0a\u8fdb\u884c SELECT\n    for (Map.Entry&lt;Integer, List&lt;String&gt;&gt; e : ukAllTableMap.entrySet()) {\n        List&lt;String&gt; tableNames = e.getValue();\n\n        if (tableNames.contains(primaryTableName.toUpperCase())) {\n            tableUkMap.computeIfAbsent(primaryTableName.toUpperCase(), k -&gt; new ArrayList&lt;&gt;())\n                .add(uniqueKeys.get(e.getKey()));\n        } else {\n            final boolean onlyNonPublicGsi =\n                tableNames.stream().noneMatch(tn -&gt; GlobalIndexMeta.isPublished(executionContext, sm.getTable(tn)));\n\n            boolean found = false;\n            for (String tableName : tableNames) {\n                if (!onlyNonPublicGsi &amp;&amp; GlobalIndexMeta.isPublished(executionContext, sm.getTable(tableName))) {\n                    tableUkMap.computeIfAbsent(tableName, k -&gt; new ArrayList&lt;&gt;()).add(uniqueKeys.get(e.getKey()));\n                    found = true;\n                    break;\n                } else if (onlyNonPublicGsi &amp;&amp; GlobalIndexMeta.canWrite(executionContext, sm.getTable(tableName))) {\n                    tableUkMap.computeIfAbsent(tableName, k -&gt; new ArrayList&lt;&gt;()).add(uniqueKeys.get(e.getKey()));\n                    found = true;\n                    break;\n                }\n            }\n        }\n    }\n\n    return tableUkMap;\n}\u800c\u5230\u4e86\u6267\u884c\u9636\u6bb5\uff0c\u6211\u4eec\u5728 LogicalInsertIgnoreHandler \u4e2d\u5904\u7406 INSERT IGNORE\u3002\u6211\u4eec\u9996\u5148\u4f1a\u8fdb\u5165 getDuplicatedValues \u51fd\u6570\uff0c\u5176\u901a\u8fc7\u4e0b\u53d1 SELECT \u7684\u65b9\u5f0f\u67e5\u627e\u8868\u4e2d\u5df2\u6709\u7684\u51b2\u7a81\u7684 Unique Key \u7684\u8bb0\u5f55\u3002\u6211\u4eec\u5c06\u4e0b\u53d1\u7684 SELECT \u8bed\u53e5\u4e2d\u9009\u62e9\u7684\u5217\u8bbe\u7f6e\u4e3a (value_index, uk_index, pk)\u3002\u5176\u4e2d value_index \u548c uk_index \u5747\u4e3a\u7684\u5e38\u91cf\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6709\u8868\uff1aCREATE TABLE `t` (\n`id` int(11) NOT NULL,\n`a` int(11) NOT NULL,\n`b` int(11) NOT NULL,\nPRIMARY KEY (`id`),\nUNIQUE GLOBAL KEY `g_i_a` (`a`) COVERING (`id`) DBPARTITION BY HASH(`a`)<\/code><\/pre>\n<p>) DBPARTITION BY HASH(<code>id<\/code>)\u4ee5\u53ca\u4e00\u6761 INSERT IGNORE \u8bed\u53e5\uff1aINSERT IGNORE INTO t VALUES (1,2,3),(2,3,4),(3,4,5);\u5047\u8bbe\u5728 PolarDB-X \u4e2d\u6267\u884c\u65f6\uff0c\u5176\u4f1a\u5c06 Unique Key \u7f16\u53f7\u4e3a0: id<br \/>1: g_i_aINSERT IGNORE \u8bed\u53e5\u4e2d\u63d2\u5165\u7684\u6bcf\u4e2a\u503c\u5206\u522b\u7f16\u53f7\u4e3a0: (1,2,3)<br \/>1: (2,3,4)<br \/>2: (3,4,5)\u90a3\u4e48\u5bf9\u4e8e (2,3,4) \u7684 UNIQUE KEY \u6784\u9020\u7684 GSI \u4e0a\u7684 SELECT \u5373\u4e3a\uff1a\u67e5\u8be2 GSISELECT 1 as <code>value_index<\/code>, 1 as <code>uk_index<\/code>, <code>id<\/code><br \/>FROM <code>g_i_a_xxxx<\/code><br \/>WHERE <code>a<\/code> in 3;\u5047\u8bbe\u8868\u4e2d\u5df2\u7ecf\u5b58\u5728 (5,3,6)\uff0c\u90a3\u4e48\u8fd9\u6761 SELECT \u7684\u8fd4\u56de\u7ed3\u679c\u5373\u4e3a (1,1,5)\u3002\u6b64\u5916\uff0c\u7531\u4e8e\u4e0d\u540c\u7684 Unique Key \u7684 SELECT \u8fd4\u56de\u683c\u5f0f\u662f\u76f8\u540c\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u4f1a\u5c06\u540c\u4e00\u4e2a\u7269\u7406\u5e93\u4e0a\u4e0d\u540c\u7684SELECT \u67e5\u8be2 UNION \u8d77\u6765\u53d1\u9001\uff0c\u4ee5\u4e00\u6b21\u6027\u5f97\u5230\u591a\u4e2a\u7ed3\u679c\uff0c\u51cf\u5c11 CN \u548c DN \u4e4b\u95f4\u7684\u4ea4\u4e92\u6b21\u6570\u3002\u53ea\u8981\u67d0\u4e2a Unique Key \u6709\u91cd\u590d\u503c\uff0c\u6211\u4eec\u5c31\u80fd\u6839\u636e value_index \u548c uk_index \u786e\u5b9a\u662f\u63d2\u5165\u503c\u7684\u54ea\u4e00\u884c\u7684\u54ea\u4e2a Unique Key \u662f\u91cd\u590d\u7684\u3002\u5f53\u5f97\u5230\u6240\u6709\u7684\u8fd4\u56de\u7ed3\u679c\u4e4b\u540e\uff0c\u6211\u4eec\u5bf9\u6570\u636e\u8fdb\u884c\u53bb\u91cd\u3002\u6211\u4eec\u5c06\u4e0a\u4e00\u6b65\u5f97\u5230\u7684\u51b2\u7a81\u7684\u7684\u503c\u653e\u5165\u4e00\u4e2a SET \u4e2d\uff0c\u7136\u540e\u987a\u5e8f\u626b\u63cf\u6240\u6709\u7684\u6bcf\u4e00\u884c\u63d2\u5165\u503c\uff0c\u5982\u679c\u53d1\u73b0\u6709\u91cd\u590d\u7684\u5c31\u8df3\u8fc7\u8be5\u884c\uff0c\u5426\u5219\u5c31\u5c06\u8be5\u884c\u4e5f\u52a0\u5165\u5230 SET \u4e2d\uff08\u56e0\u4e3a\u63d2\u5165\u503c\u4e4b\u95f4\u4e5f\u6709\u53ef\u80fd\u5b58\u5728\u76f8\u4e92\u51b2\u7a81\uff09\u3002\u53bb\u91cd\u5b8c\u6bd5\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u6240\u6709\u4e0d\u5b58\u5728\u51b2\u7a81\u7684\u503c\uff0c\u5c06\u8fd9\u4e9b\u503c\u63d2\u5165\u5230\u8868\u4e2d\u4e4b\u540e\u5c31\u5b8c\u6210\u4e86\u4e00\u6761 INSERT IGNORE \u7684\u6267\u884c\u3002\u903b\u8f91\u6267\u884c\u7684\u6267\u884c\u6d41\u7a0b\uff1acom.alibaba.polardbx.repo.mysql.handler.LogicalInsertIgnoreHandler#doExecuteprotected int doExecute(LogicalInsert insert, ExecutionContext executionContext,<\/p>\n<pre><code>                        LogicalInsert.HandlerParams handlerParams) {\n    \/\/ ...\n\n    try {\n        Map&lt;String, List&lt;List&lt;String&gt;&gt;&gt; ukGroupByTable = insertIgnore.getUkGroupByTable();\n        List&lt;Map&lt;Integer, ParameterContext&gt;&gt; deduplicated;\n        List&lt;List&lt;Object&gt;&gt; duplicateValues;\n        \/\/ \u83b7\u53d6\u8868\u4e2d\u5df2\u6709\u7684 Unique Key \u51b2\u7a81\u503c\n        duplicateValues = getDuplicatedValues(insertIgnore, LockMode.SHARED_LOCK, executionContext, ukGroupByTable,\n            (rowCount) -&gt; memoryAllocator.allocateReservedMemory(\n                MemoryEstimator.calcSelectValuesMemCost(rowCount, selectRowType)), selectRowType, true,\n            handlerParams);\n\n        final List&lt;Map&lt;Integer, ParameterContext&gt;&gt; batchParameters =\n            executionContext.getParams().getBatchParameters();\n\n        \/\/ \u6839\u636e\u4e0a\u4e00\u6b65\u5f97\u5230\u7684\u7ed3\u679c\uff0c\u53bb\u6389 INSERT IGNORE \u4e2d\u7684\u51b2\u7a81\u503c\n        deduplicated = getDeduplicatedParams(insertIgnore.getUkColumnMetas(), insertIgnore.getBeforeUkMapping(),\n            insertIgnore.getAfterUkMapping(), RelUtils.getRelInput(insertIgnore), duplicateValues,\n            batchParameters, executionContext);\n\n        if (!deduplicated.isEmpty()) {\n            insertEc.setParams(new Parameters(deduplicated));\n        } else {\n            \/\/ All duplicated\n            return affectRows;\n        }\n\n        \/\/ \u6267\u884c INSERT\n        try {\n            if (gsiConcurrentWrite) {\n                affectRows = concurrentExecute(insertIgnore, insertEc);\n            } else {\n                affectRows = sequentialExecute(insertIgnore, insertEc);\n            }\n        } catch (Throwable e) {\n            handleException(executionContext, e, GeneralUtil.isNotEmpty(insertIgnore.getGsiInsertWriters()));\n        }\n    } finally {\n        selectValuesPool.destroy();\n    }\n    return affectRows;\n}RETURNING \u4f18\u5316\u4e0a\u4e00\u8282\u63d0\u5230\u7684 INSERT IGNORE \u7684\u903b\u8f91\u6267\u884c\u65b9\u5f0f\uff0c\u867d\u7136\u4fdd\u8bc1\u4e86\u6570\u636e\u7684\u6b63\u786e\u6027\uff0c\u4f46\u662f\u4e5f\u4f7f\u5f97\u4e00\u6761 INSERT IGNORE \u8bed\u53e5\u81f3\u5c11\u9700\u8981 CN \u548c DN \u7684\u4e24\u6b21\u4ea4\u4e92\u624d\u80fd\u5b8c\u6210\uff08\u7b2c\u4e00\u6b21 SELECT\uff0c\u7b2c\u4e8c\u6b21 INSERT\uff09\uff0c\u5f71\u54cd\u4e86 INSERT IGNORE \u7684\u6267\u884c\u6027\u80fd\u3002\u76ee\u524d\u7684 DN \u5df2\u7ecf\u652f\u6301\u4e86 AliSQL \u7684 RETURNING \u4f18\u5316\uff0c\u5176\u53ef\u4ee5\u5728 DN \u7684 INSERT IGNORE \u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\u8fd4\u56de\u6210\u529f\u63d2\u5165\u7684\u503c\u3002\u5229\u7528\u8fd9\u4e00\u529f\u80fd\uff0cPolarDB-X \u5bf9 INSERT IGNORE \u8fdb\u884c\u4e86\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\uff1a\u76f4\u63a5\u5c06 INSERT IGNORE \u4e0b\u53d1\uff0c\u5982\u679c\u5728\u4e3b\u8868\u548c GSI \u4e0a\u5168\u90e8\u6210\u529f\u8fd4\u56de\uff0c\u90a3\u4e48\u5c31\u8bf4\u660e\u63d2\u5165\u503c\u4e2d\u6ca1\u6709\u51b2\u7a81\uff0c\u4e8e\u662f\u5c31\u6210\u529f\u5b8c\u6210\u8be5\u6761 INSERT IGNORE \u7684\u6267\u884c\uff1b\u5426\u5219\u5c31\u5c06\u591a\u63d2\u5165\u7684\u503c\u5220\u9664\u3002\u6267\u884c\u65f6\uff0cCN \u9996\u5148\u4f1a\u6839\u636e\u4e0a\u6587\u4e2d\u7684\u8bed\u6cd5\u4e0b\u53d1\u5e26\u6709 RETURNING \u7684\u7269\u7406 INSERT IGNORE \u8bed\u53e5\u5230 DN\uff0c\u6bd4\u5982\uff1acall dbms_trans.returning(&quot;a&quot;, &quot;insert into t1_xxxx values(1,1)&quot;);\u5176\u4e2d\u8fd4\u56de\u5217\u662f\u4e3b\u952e\uff0c\u7528\u6765\u6807\u8bc6\u63d2\u5165\u7684\u4e00\u6279\u6570\u636e\u4e2d\u54ea\u4e9b\u88ab\u6210\u529f\u63d2\u5165\u4e86\uff1bt1_xxxx \u662f\u903b\u8f91\u8868 t1 \u7684\u4e00\u4e2a\u7269\u7406\u5206\u8868\u3002\u5f53\u4e3b\u8868\u548c GSI \u4e0a\u7684\u6240\u6709 INSERT IGNORE \u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u6211\u4eec\u8ba1\u7b97\u4e3b\u8868\u548c GSI \u4e2d\u6210\u529f\u63d2\u5165\u503c\u7684\u4ea4\u96c6\u4f5c\u4e3a\u6700\u540e\u7684\u7ed3\u679c\uff0c\u7136\u540e\u5220\u9664\u591a\u63d2\u5165\u7684\u503c\u3002\u8fd9\u90e8\u5206\u4ee3\u7801\u5728com.alibaba.polardbx.repo.mysql.handler.LogicalInsertIgnoreHandler#getRowsToBeRemovedprivate Map&lt;String, List&lt;List&lt;Object&gt;&gt;&gt; getRowsToBeRemoved(String tableName,\n                                                           Map&lt;String, List&lt;List&lt;Object&gt;&gt;&gt; tableInsertedValues,\n                                                           List&lt;Integer&gt; beforePkMapping,\n                                                           List&lt;ColumnMeta&gt; pkColumnMetas) {\n    final Map&lt;String, Set&lt;GroupKey&gt;&gt; tableInsertedPks = new TreeMap&lt;&gt;(String.CASE_INSENSITIVE_ORDER);\n    final Map&lt;String, List&lt;Pair&lt;GroupKey, List&lt;Object&gt;&gt;&gt;&gt; tablePkRows =\n        new TreeMap&lt;&gt;(String.CASE_INSENSITIVE_ORDER);\n    tableInsertedValues.forEach((tn, insertedValues) -&gt; {\n        final Set&lt;GroupKey&gt; insertedPks = new TreeSet&lt;&gt;();\n        final List&lt;Pair&lt;GroupKey, List&lt;Object&gt;&gt;&gt; pkRows = new ArrayList&lt;&gt;();\n        for (List&lt;Object&gt; inserted : insertedValues) {\n            final Object[] groupKeys = beforePkMapping.stream().map(inserted::get).toArray();\n            final GroupKey pk = new GroupKey(groupKeys, pkColumnMetas);\n            insertedPks.add(pk);\n            pkRows.add(Pair.of(pk, inserted));\n        }\n        tableInsertedPks.put(tn, insertedPks);\n        tablePkRows.put(tn, pkRows);\n    });\n\n    \/\/ Get intersect of inserted values\n    final Set&lt;GroupKey&gt; distinctPks = new TreeSet&lt;&gt;();\n    for (GroupKey pk : tableInsertedPks.get(tableName)) {\n        if (tableInsertedPks.values().stream().allMatch(pks -&gt; pks.contains(pk))) {\n            distinctPks.add(pk);\n        }\n    }\n\n    \/\/ Remove values which not exists in at least one insert results\n    final Map&lt;String, List&lt;List&lt;Object&gt;&gt;&gt; tableDeletePks = new TreeMap&lt;&gt;(String.CASE_INSENSITIVE_ORDER);\n    tablePkRows.forEach((tn, pkRows) -&gt; {\n        final List&lt;List&lt;Object&gt;&gt; deletePks = new ArrayList&lt;&gt;();\n        pkRows.forEach(pkRow -&gt; {\n            if (!distinctPks.contains(pkRow.getKey())) {\n                deletePks.add(pkRow.getValue());\n            }\n        });\n        if (!deletePks.isEmpty()) {\n            tableDeletePks.put(tn, deletePks);\n        }\n    });\n    return tableDeletePks;\n}\u4e0e\u4e0a\u4e00\u8282\u7684\u903b\u8f91\u6267\u884c\u7684\u201c\u60b2\u89c2\u6267\u884c\u201d\u76f8\u6bd4\uff0c\u4f7f\u7528 RETURNING \u4f18\u5316\u7684 INSERT IGNORE \u76f8\u5f53\u4e8e\u201c\u4e50\u89c2\u6267\u884c\u201d\uff0c\u5982\u679c\u63d2\u5165\u7684\u503c\u672c\u8eab\u6ca1\u6709\u51b2\u7a81\uff0c\u90a3\u4e48\u4e00\u6761 INSERT IGNORE \u8bed\u53e5 CN \u548c DN \u95f4\u53ea\u9700\u8981\u4e00\u6b21\u4ea4\u4e92\u5373\u53ef\uff1b\u800c\u5728\u6709\u51b2\u7a81\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u4e0b\u53d1 DELETE \u8bed\u53e5\u5c06\u4e3b\u8868\u6216 GSI \u4e2d\u591a\u63d2\u5165\u7684\u503c\u5220\u9664\uff0c\u4e8e\u662f CN \u548c DN \u95f4\u9700\u8981\u4e24\u6b21\u4ea4\u4e92\u3002\u53ef\u4ee5\u770b\u51fa\uff0c\u5373\u4fbf\u662f\u6709\u51b2\u7a81\u7684\u60c5\u51b5\uff0cCN \u548c DN \u95f4\u7684\u4ea4\u4e92\u6b21\u6570\u4e5f\u4e0d\u4f1a\u8d85\u8fc7\u4e0a\u4e00\u8282\u7684\u903b\u8f91\u6267\u884c\u3002\u56e0\u6b64\u5728\u65e0\u6cd5\u76f4\u63a5\u4e0b\u63a8\u7684\u60c5\u51b5\u4e0b\uff0cINSERT IGNORE \u7684\u6267\u884c\u7b56\u7565\u662f\u9ed8\u8ba4\u4f7f\u7528 RETURNING \u4f18\u5316\u6267\u884c\u3002\u5f53\u7136 RETURNING \u4f18\u5316\u7684\u4f7f\u7528\u4e5f\u6709\u4e00\u4e9b\u9650\u5236\uff0c\u6bd4\u5982\u63d2\u5165\u7684 Value \u6709\u91cd\u590d\u4e3b\u952e\u65f6\u5c31\u4e0d\u80fd\u4f7f\u7528\uff0c\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u65e0\u6cd5\u5224\u65ad\u5177\u4f53\u662f\u54ea\u4e00\u884c\u88ab\u6210\u529f\u63d2\u5165\uff0c\u54ea\u4e00\u884c\u9700\u8981\u5220\u9664\uff1b\u5177\u4f53\u53ef\u4ee5\u9605\u8bfb\u4ee3\u7801\u4e2d\u7684\u6761\u4ef6\u5224\u65ad\u3002\u5f53\u4e0d\u80fd\u4f7f\u7528 RETURNING \u4f18\u5316\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u9009\u62e9\u4e0a\u4e00\u8282\u4e2d\u7684\u903b\u8f91\u6267\u884c\u65b9\u5f0f\u6267\u884c\u8be5\u6761 INSERT IGNORE \u8bed\u53e5\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u6b63\u786e\u6027\u3002\u4f7f\u7528 RETURNING \u4f18\u5316\u7684\u6267\u884c\u6d41\u7a0b\uff1acom.alibaba.polardbx.repo.mysql.handler.LogicalInsertIgnoreHandler#doExecuteprotected int doExecute(LogicalInsert insert, ExecutionContext executionContext,\n                        LogicalInsert.HandlerParams handlerParams) {\n    \/\/ ...\n\n    \/\/ \u5224\u65ad\u80fd\u5426\u4f7f\u7528 RETURNING \u4f18\u5316\n    boolean canUseReturning =\n        executorContext.getStorageInfoManager().supportsReturning() &amp;&amp; executionContext.getParamManager()\n            .getBoolean(ConnectionParams.DML_USE_RETURNING) &amp;&amp; allDnUseXDataSource &amp;&amp; gsiCanUseReturning\n            &amp;&amp; !isBroadcast &amp;&amp; !ComplexTaskPlanUtils.canWrite(tableMeta);\n\n    if (canUseReturning) {\n        canUseReturning = noDuplicateValues(insertIgnore, insertEc);\n    }\n\n    if (canUseReturning) {\n        \/\/ \u6267\u884c INSERT IGNORE \u5e76\u83b7\u5f97\u8fd4\u56de\u7ed3\u679c\n        final List&lt;RelNode&gt; allPhyPlan =\n            new ArrayList&lt;&gt;(replaceSeqAndBuildPhyPlan(insertIgnore, insertEc, handlerParams));\n        getPhysicalPlanForGsi(insertIgnore.getGsiInsertIgnoreWriters(), insertEc, allPhyPlan);\n        final Map&lt;String, List&lt;List&lt;Object&gt;&gt;&gt; tableInsertedValues =\n            executeAndGetReturning(executionContext, allPhyPlan, insertIgnore, insertEc, memoryAllocator,\n                selectRowType);\n\n        \/\/ ...\n\n        \/\/ \u751f\u6210 DELETE\n        final boolean removeAllInserted =\n            targetTableNames.stream().anyMatch(tn -&gt; !tableInsertedValues.containsKey(tn));\n\n        if (removeAllInserted) {\n            affectedRows -=\n                removeInserted(insertIgnore, schemaName, tableName, isBroadcast, insertEc, tableInsertedValues);\n            if (returnIgnored) {\n                ignoredRows = totalRows;\n            }\n        } else {\n            final List&lt;Integer&gt; beforePkMapping = insertIgnore.getBeforePkMapping();\n            final List&lt;ColumnMeta&gt; pkColumnMetas = insertIgnore.getPkColumnMetas();\n\n            \/\/ \u8ba1\u7b97\u6240\u6709\u63d2\u5165\u503c\u7684\u4ea4\u96c6\n            final Map&lt;String, List&lt;List&lt;Object&gt;&gt;&gt; tableDeletePks =\n                getRowsToBeRemoved(tableName, tableInsertedValues, beforePkMapping, pkColumnMetas);\n\n            affectedRows -=\n                removeInserted(insertIgnore, schemaName, tableName, isBroadcast, insertEc, tableDeletePks);\n            if (returnIgnored) {\n                ignoredRows +=\n                    Optional.ofNullable(tableDeletePks.get(insertIgnore.getLogicalTableName())).map(List::size)\n                        .orElse(0);\n            }\n\n        }\n\n        handlerParams.optimizedWithReturning = true;\n\n        if (returnIgnored) {\n            return ignoredRows;\n        } else {\n            return affectedRows;\n        }\n    } else {\n        handlerParams.optimizedWithReturning = false;\n    }\n\n    \/\/ ... \n}\u6700\u540e\u4ee5\u4e00\u4e2a\u4f8b\u5b50\u6765\u5c55\u73b0 RETURNING \u4f18\u5316\u7684\u6267\u884c\u6d41\u7a0b\u4e0e\u903b\u8f91\u6267\u884c\u7684\u4e0d\u540c\u3002\u901a\u8fc7 \/+TDDL:CMD_EXTRA(DML_USE_RETURNING=TRUE)\/ \u8fd9\u6761 HINT\uff0c\u7528\u6237\u53ef\u4ee5\u624b\u52a8\u63a7\u5236\u662f\u5426\u4f7f\u7528 RETURNING \u4f18\u5316\u3002\u9996\u5148\u5efa\u8868\u5e76\u63d2\u5165\u4e00\u6761\u6570\u636e\uff1aCREATE TABLE `t` (\n`id` int(11) NOT NULL,\n`a` int(11) NOT NULL,\n`b` int(11) NOT NULL,\nPRIMARY KEY (`id`),\nUNIQUE GLOBAL KEY `g_i_a` (`a`) COVERING (`id`) DBPARTITION BY HASH(`a`)<\/code><\/pre>\n<p>) DBPARTITION BY HASH(<code>id<\/code>);<\/p>\n<p>INSERT INTO t VALUES (1,3,3);\u518d\u6267\u884c\u4e00\u6761 INSERT IGNORE\uff1aINSERT IGNORE INTO t VALUES (1,2,3),(2,3,4),(3,4,5);\u5176\u4e2d (1,2,3) \u4e0e (1,3,3) \u4e3b\u952e\u51b2\u7a81\uff0c(2,3,4) \u4e0e (1,3,3) \u5bf9\u4e8e Unique Key g_i_a \u51b2\u7a81\u3002\u5982\u679c\u662f RETURNING \u4f18\u5316\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042737698\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u53ef\u4ee5\u770b\u5230 PolarDB-X \u5148\u8fdb\u884c\u4e86 INSERT IGNORE\uff0c\u518d\u5c06\u591a\u63d2\u5165\u7684\u6570\u636e\u5220\u9664\uff1a(1,2,3) \u5728\u4e3b\u8868\u4e0a\u51b2\u7a81\u5728 UGSI \u4e0a\u6210\u529f\u63d2\u5165\uff0c(2,3,4) \u5728 UGSI \u4e0a\u51b2\u7a81\u5728\u4e3b\u8868\u4e0a\u6210\u529f\u63d2\u5165\uff0c\u56e0\u6b64\u5206\u522b\u4e0b\u53d1\u5bf9\u5e94\u7684 DELETE \u5230 UGSI \u548c\u4e3b\u8868\u4e0a\u3002\u5982\u679c\u5173\u95ed RETURNING \u4f18\u5316\uff0c\u903b\u8f91\u6267\u884c\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042737699\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u53ef\u4ee5\u770b\u5230 PolarDB-X \u5148\u8fdb\u884c\u4e86 SELECT\uff0c\u518d\u5c06\u6ca1\u6709\u51b2\u7a81\u7684\u6570\u636e (3,4,5) \u63d2\u5165\u3002\u5c0f\u7ed3\u672c\u6587\u4ecb\u7ecd\u4e86 PolarDB-X \u4e2d INSERT IGNORE \u7684\u6267\u884c\u6d41\u7a0b\u3002\u9664\u4e86 INSERT IGNORE \u4e4b\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b DML \u8bed\u53e5\u5728\u6267\u884c\u65f6\u4e5f\u9700\u8981\u8fdb\u884c\u91cd\u590d\u503c\u7684\u5224\u65ad\uff0c\u6bd4\u5982 REPLACE\u3001INSERT ON DUPLICATE KEY UPDATE \u7b49\uff0c\u8fd9\u4e9b\u8bed\u53e5\u5728\u6709 GSI \u7684\u60c5\u51b5\u4e0b\u5747\u91c7\u7528\u4e86\u903b\u8f91\u6267\u884c\u7684\u65b9\u5f0f\uff0c\u5373\u5148\u8fdb\u884c SELECT \u518d\u8fdb\u884c\u5224\u91cd\u3001\u66f4\u65b0\u7b49\u64cd\u4f5c\uff0c\u611f\u5174\u8da3\u7684\u8bfb\u8005\u53ef\u4ee5\u81ea\u884c\u9605\u8bfb\u76f8\u5173\u4ee3\u7801\u3002\u6b22\u8fce\u5173\u6ce8PolarDB-X\u77e5\u4e4e\u673a\u6784\u53f7\uff0c\u9605\u8bfb\u66f4\u591a\u6280\u672f\u597d\u6587\u3002\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=OyL41mN7dEd0khgm6CTa5A%3D%3D.X84CQ4rXjWhQksSvMs6EiGzpH5779hxQwGQu8MhCDsBekC37j6NPUi75M9N0UmYy\" rel=\"nofollow\">https:\/\/click.aliyun.com\/m\/10&#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\/1190000042737696\">https:\/\/segmentfault.com\/a\/1190000042737696<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u672c\u6587\u5c06\u8fdb\u4e00\u6b65\u4ecb\u7ecd PolarDB-X \u4e2d INSERT IGNORE \u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5176\u6839\u636e\u63d2\u5165\u7684\u8868\u662f\u5426\u6709 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006793","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006793","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"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006793"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006793\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}