Skip to content

[Perf]: INSERT ... ON DUPLICATE KEY UPDATE uses full table scan + DEDUP join for conflict detection (no pk/UK filter pushdown) #29196

Closed as not planned

Description

@Ariznawlll

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 and buildOnDupTargetPkResolution) emits:
    • One or more full TABLE_SCAN nodes over the target table (and hidden UK index tables) with no FilterList.
    • A JOIN node with JoinType: 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).

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/dedupjoin receives 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:

Related

Suggested fix direction

In the ODKU UPDATE path:

  1. Collect candidate PK/UK values from the insert-side batch (constants or exprs).
  2. 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".
  3. Preserve the target_pk resolution and multi-update rewrite for UK cross conflicts.
  4. 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

  1. added
    kind/bug
    severity/s1
    and removed
    severity/s1
    on Sep 21, 2026
  2. added theissue type on Sep 21, 2026
  3. added this to the MO-202609 milestone on Sep 21, 2026
  4. self-assigned this
    on Sep 21, 2026
  5. Ariznawlll commented on Sep 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    不是正确性 bug。计划确实是全表 TABLE_SCAN(没有 FilterList)再 DEDUP 再 MULTI_UPDATE,冲突检测的结果是对的,缺的是把 pk/唯一键条件推进 scan。同一类优化诉求在 #10655 已经按需求关闭。按 not planned 关闭。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/performancekind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions