Repository navigation
Description
Problem
REPLACE INTO ... VALUES becomes prohibitively slow when the target table has a primary key and an additional unique key.
The slowdown occurs in the first statement of the transaction, before the transaction workspace has accumulated mutations. CPU profiling and EXPLAIN VERBOSE point to the physical conflict-detection plan rather than transaction workspace growth.
Schema
All measurements use this exact table:
CREATE TABLE replace_record (
id BIGINT PRIMARY KEY,
external_key VARCHAR(80) NOT NULL UNIQUE,
payload VARCHAR(120) NOT NULL,
revision INT NOT NULL
);The schema deliberately contains two independent uniqueness constraints:
- clustered/base-table primary key:
id - secondary unique key:
external_key
This is required to reproduce the OR conflict predicate generated by REPLACE planning.
Data generation
Preloaded row n is deterministic:
id = n
external_key = "key-<n>"
payload = "initial"
revision = 0
The preload contains a dense ID interval beginning at 1. Rows are inserted in batches; large preloads are expanded by INSERT ... SELECT doubling so setup time is not included in statement latency.
Transaction workload
One database connection and one explicit transaction are used. The transaction executes 12 statements sequentially; there is no client concurrency:
BEGIN;
REPLACE INTO replace_record (id, external_key, payload, revision)
VALUES (... 25,000 tuples ...);
-- repeated 12 times with disjoint ID ranges
COMMIT;For statement s in [1, 12]:
first_id = (s - 1) * 25,000 + 1
last_id = s * 25,000
external_key = "key-<id>"
payload = "replaced-<statement>-<id>"
revision = 1
Therefore:
- statements: 12
- values per statement: 25,000
- input rows per transaction: 300,000
- SQL text per statement: approximately 1.2-1.5 MiB
- input ID ranges are disjoint across statements
Original reproducer
- preloaded rows: 20,000,000
- all 300,000 input IDs already exist
- all inputs conflict through both
idandexternal_key - the first statement did not finish after more than 15 minutes
Reduced follow-up benchmark
To separate base-table conflict-scan cost from large-transaction workspace cost, the same workload is also run with:
- preloaded rows: 10,000
- statements: 12
- values per statement: 25,000
- input rows: 300,000
- conflicting rows: 10,000 (IDs 1-10,000)
- inserted rows: 290,000 (IDs 10,001-300,000)
- expected final rows: 300,000
Only the initial base-table cardinality changes. Transaction shape, statement count and input cardinality remain unchanged.
Timing boundaries
The benchmark records these durations separately:
- setup: schema creation and preload; excluded from transaction latency
- per-statement latency: one synchronous
tx.Exec(REPLACE...) - transaction execution: all 12
tx.Execcalls, excluding commit - commit latency: synchronous
tx.Commit
It also records statement SQL size and min/median/max statement latency. Planning and execution are included in each statement latency.
Validation
After commit, the reduced benchmark executes:
SELECT
count(*),
sum(CASE WHEN revision = 1 THEN 1 ELSE 0 END),
sum(CASE WHEN payload LIKE 'replaced-%' THEN 1 ELSE 0 END)
FROM replace_record;Expected reduced-benchmark result:
count(*) = 300,000
sum(revision = 1) = 300,000
sum(payload LIKE 'replaced-%') = 300,000
This checks both conflict replacement and insertion of previously absent keys.
Reproduction command
Run against a fresh MatrixOne instance built from the target commit:
go run ./.bench/replace-into \
-engine mo \
-dsn 'dump:***@tcp(127.0.0.1:6002)/?interpolateParams=false&timeout=10s&readTimeout=30m&writeTimeout=30m' \
-rows 10000 \
-statements 12 \
-values 25000The runner drops and recreates only database txn_ws_replace_bench before each measurement.
Execution-plan evidence
Even a two-row REPLACE INTO is planned as a Multi Update containing a left join equivalent to:
VALUES
LEFT JOIN replace_record
ON values.id = replace_record.id
OR values.external_key = replace_record.external_keyOn the original 20-million-row reproducer, EXPLAIN VERBOSE reports:
- join type:
LEFT - join condition: primary-key equality OR unique-key equality
- right side: full scan of approximately 19,990,000 base-table rows
- additional scans of the hidden unique-index table
- no usable runtime filter for the
ORjoin
Because the predicate contains OR across two unique constraints, it does not use the normal hash-join equality path and falls back to loopjoin.
CPU-profile evidence
A 30-second CPU profile captured while the first original statement was running showed:
hashbuild.Call/build -> loopjoin.Call -> loopjoin.container.probe: 25.86s cumulative, 58.39%loopjoin.conditionMatchAt: 21.58s cumulative, 48.72%- boolean expression/vector evaluation: 20.85s cumulative
runtime.mallocgc: 13.41s cumulative, 30.28%- substantial GC work caused by repeated predicate evaluation and allocation
A profile from the reduced 10,000-row benchmark shows the same hot path: about 59% cumulative CPU in loopjoin, about 50% in conditionMatchAt, and about 31% in mallocgc.
Expected behavior
Conflict detection should scale with incoming rows and the relevant primary/unique-key lookup paths. It should not evaluate an OR predicate between incoming values and the complete base table.
The implementation must preserve REPLACE semantics when one incoming row conflicts with different existing rows through different unique constraints.
Scope clarification
This was found while benchmarking transaction workspace PR #27013, but the measured bottleneck is upstream of the workspace mutation path:
- it appears in the first statement;
- the workspace is still effectively empty;
- the hot stack is planner-selected
loopjoin, not workspace overlay/index code.
Related benchmark tracking: #26796.
Activity
gouhongshen commentedon Aug 14, 2026
Reduced 10,000-row base-table result
Tested MatrixOne commit ae69b2b with the exact reduced protocol documented in this issue:
The run completed successfully and produced the following statement latencies:
Summary:
This confirms that the progressive slowdown is reproducible with only 10,000 preloaded base-table rows. As the same transaction accumulates disjoint 25,000-row REPLACE statements, latency increases from 7.1 seconds to 239.3 seconds even though the base table starts small.
A CPU profile captured during this reduced run shows the same hot path already documented in the issue: approximately 59% cumulative CPU in loopjoin, approximately 50% in loopjoin.conditionMatchAt, and approximately 31% in runtime.mallocgc.
gouhongshen commentedon Sep 4, 2026
PR #28170 is trying to fix this issue.
fix: preserve Data Branch lineage across TRUNCATE (#28148)
gouhongshen commentedon Sep 5, 2026
PR #28170 has merged and is awaiting manual validation. The issue remains open.
fix: preserve Data Branch lineage across TRUNCATE (matrixorigin#28148)
fix: preserve Data Branch lineage across TRUNCATE (matrixorigin#28148)
fix: preserve Data Branch lineage across TRUNCATE (matrixorigin#28148)