Repository navigation
Closed as not planned
Closed as not planned
Copy link
Description
INSERT INTO ... ON DUPLICATE KEY UPDATE (ODKU) is reported to be very slow, especially as target table size grows or with unique keys present.
Current behavior (plan shape)
For ODKU (onDupAction=UPDATE):
- Planner in
pkg/sql/plan/bind_insert.go(around lines 1760-1840 andbuildOnDupTargetPkResolution) emits:- One or more full
TABLE_SCANnodes over the target table (and hidden UK index tables) with no FilterList. - A
JOINnode withJoinType: DEDUP,OnDuplicateAction: UPDATE. - For tables with both PK + UK(s): extra LEFT JOINs + COALESCE to build
target_pk(still full scans). - Then
MULTI_UPDATE(delete old + insert updated for conflicts).
- One or more full
Example structure (simplified):
... -> [values/sink]
-> DEDUP_JOIN (build side = TABLE_SCAN of base table on PK/UK, probe = incoming)
-> MULTI_UPDATE
No collection of the incoming PK/UK constant values into an IN (...) list or runtime filter pushed down to the dedup scan.
Root cause hypothesis
- Every ODKU (even with few conflicts) materializes/builds a hash on (potentially) the entire table on the build side of DedupJoin.
- UK case adds N extra full scans + joins.
- Execution in
pkg/sql/colexec/dedupjoinreceives full build batches, does hash probe + per-match expr eval + bitmap for updates. - Leads to O(table size) work per batch + write amplification (del+ins) on hits.
- Matches exactly the scenario described in closed tech request [Tech Request]: improve check pk dup in some case #10655 ("change to table_scan with filter like pk_col in (1,2,3)").
Expected
Targeted scan / index lookup / pushed pk IN (consts from this insert batch) or equivalent runtime filter so only potential conflicting rows are read for the dedup build.
Reproduction (please attach your numbers + EXPLAIN)
CREATE TABLE t (id BIGINT PRIMARY KEY, v1 INT, v2 VARCHAR(100), UNIQUE KEY uk_v1 (v1));
-- warmup / scale
INSERT INTO t SELECT x, x%100000, concat('v',x) FROM generate_series(1, 1000000) g(x); -- adjust size
-- measure plain insert vs ODKU
-- time the following
INSERT INTO t (id, v1, v2) VALUES (123, 42, 'new'), (456, 43, 'new2')
ON DUPLICATE KEY UPDATE v2 = VALUES(v2), v1 = VALUES(v1);Please provide:
EXPLAIN INSERT ... ON DUPLICATE ...(or EXPLAIN ANALYZE if supported)- Table size, #UKs, conflict rate
- Timings (wall, or from profile)
- MO version / commit
- Whether table has FULLTEXT / vector indexes (see related [Bug]: ODKU with synchronous FULLTEXT takes 12 minutes for 1,000 rows due to unconditional posting rebuild #27933)
Related
- Closed tech request that asked for this optimization: [Tech Request]: improve check pk dup in some case #10655
- Current code pointers:
- Planning:
pkg/sql/plan/bind_insert.go:buildOnDupTargetPkResolution,appendDedup..., scanNodeID creation for dedup - Binder:
pkg/sql/plan/ondup_update_binder.go - Exec:
pkg/sql/colexec/dedupjoin/{join.go,types.go} - Final write:
pkg/sql/colexec/multi_update
- Planning:
- Other recent ODKU work: PRs around dedupjoin build-only columns, UK support (support unique key for on duplicate key statement #11632), fulltext ODKU perf ([Bug]: ODKU with synchronous FULLTEXT takes 12 minutes for 1,000 rows due to unconditional posting rebuild #27933)
Suggested fix direction
In the ODKU UPDATE path:
- Collect candidate PK/UK values from the insert-side batch (constants or exprs).
- Either:
- Push a filter (
pk IN ...or runtime filter) to the TABLE_SCAN feeding the DEDUP, or - Use a specialized lookup / index probe instead of full scan + hash build for the "old row image".
- Push a filter (
- Preserve the target_pk resolution and multi-update rewrite for UK cross conflicts.
- Consider canSkipDedup + auto-inc cases already partially handled.
This should dramatically reduce work for typical upsert workloads against large tables.
Verification notes:
- DedupJoin unit tests pass with correct CGO environment.
- No other open issue covers the general (non-FULLTEXT) ODKU performance case.
Activity
Ariznawlll commentedon Sep 23, 2026
不是正确性 bug。计划确实是全表
TABLE_SCAN(没有 FilterList)再 DEDUP 再 MULTI_UPDATE,冲突检测的结果是对的,缺的是把 pk/唯一键条件推进 scan。同一类优化诉求在 #10655 已经按需求关闭。按 not planned 关闭。fix(sql): honor empty scan decisions before constructing readers (#29650