Repository navigation
Defer dedup data hash computation to eliminate squashing overhead - #101494
Conversation
…ng squashing During INSERT with `deduplicate_insert=enable`, `calculateDataHash` was called eagerly in `updateOriginalBlock` on every squashing step. For 10M rows this meant ~17 intermediate hash computations (each O(rows * columns) with SipHash), all discarded except the last. This caused ~1.7s overhead (2.06s vs 0.35s for Engine=Null, 2.27s vs 0.67s for MergeTree). The fix defers hash computation: `updateOriginalBlock` stores the block with COW-shared columns instead of computing the hash eagerly and discarding. The hash is computed lazily on first access in the sink via `calculateDataHash`, which caches the result and then releases the block columns. After the fix, dedup ON/OFF performance is identical (0.19s/0.36s Null, 0.66s/0.66s MergeTree for 10M rows, 13 columns). Ref: #99241 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Adds a performance test comparing INSERT with `deduplicate_insert=enable` vs `disable` on both Engine=Null (isolates hash overhead) and MergeTree (combined overhead with part writes). Uses 5M rows, 13 columns, INSERT SELECT with ORDER BY ALL to make the query deduplication-eligible. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Workflow [PR], commit [f027755] Summary: ❌
AI ReviewSummaryThis PR defers deduplication data-hash computation and introduces Missing context
Findings❌ Blockers
💡 Nits
ClickHouse Rules
Performance & SafetyThe new batching path can improve performance, but current implementation introduces safety and correctness risks:
Final Verdict
|
Prevents parts from piling up across iterations, keeping each run independent. Co-Authored-By: Claude Opus 4.6 <[email protected]>
17236a5 to
9045311
Compare
Use rand() in one column so each run produces unique data hashes, preventing the dedup log from suppressing writes on subsequent iterations. Also truncate MergeTree table between runs to avoid accumulating parts. Co-Authored-By: Claude Opus 4.6 <[email protected]>
9045311 to
8705de5
Compare
|
I also noticed that |
It's still bad. My guess If you do insert from prepared .native file and MergeTree with No difference with or without non_replicated_deduplication_window, no dedup overhead. |
I believe that with this fix the case when |
Actually this numbers I do not know from where claude took them. Lets look at perf tests in this PR when they are done. I see the following result in my local run for the perf test from PR and for the bigger set PS: this is exactly for the case when |
Ok, let me try to add this case to the perf tests also. |
|
Argh, my example is bad. It inserted zeros because of column's names in 'test_dedup.native'. There is some setting how to enable insertion using positions, something like column_names..inference |
Without non_replicated_deduplication_window the dedup log is inactive, so getDeduplicationHashes returns empty and the hash is never computed — making the MergeTree benchmark meaningless. Also switch to ORDER BY () to isolate dedup overhead from sorting cost. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
In general, the deduplication requires the hash calculation from inserted data. It is not for free. We used to use the hash from Here I removed a bug, when I call hash calculation from data several times when data squashed. But the final hash calculation still cost some time. It turns out that using |
|
I have one more idea. |
|
22 columns, 5M rows: Hash overhead isolated (enable - disable): |
Add `IColumn::updateHashWithValueRange` virtual method that hashes a range of rows in one call. The default implementation loops row-by-row; overrides in `ColumnVector`, `ColumnDecimal`, `ColumnFixedString`, `ColumnString`, `ColumnArray`, `ColumnNullable`, `ColumnTuple`, and `ColumnMap` feed contiguous memory to SipHash, eliminating per-row virtual dispatch. `InsertDeduplication` now uses `calculateDataHashBatch` (calling the new method) for the unified hash path. The old row-by-row path is kept for backward-compatible hashes during the `COMPATIBLE_DOUBLE_HASHES` period. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add Decimal64, Float64, DateTime64, FixedString, String, Array, Nullable, Tuple, and Map columns to exercise all `updateHashWithValueRange` overrides. Add a query with `insert_deduplication_token` to show zero hash overhead when a user token is set. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add tables for Decimal64, Float64, FixedString, String, Array, Nullable, Tuple, Map, and a mixed-types table to `03711_deduplication_blocks_part_log`. This verifies that `updateHashWithValueRange` batch hashing produces stable deduplication hashes for every overridden column type. Update reference hashes: the unified hash changed for tables containing String and other variable-length columns because batch hashing feeds data differently to SipHash. The old (row-by-row) hash is unchanged. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add async insert tables for UInt32, String, Array, and mixed types to `03711_deduplication_blocks_part_log`. Async inserts compute data hash directly from the block via `calculateDataHashBatch`, exercising the batch hashing path independently from the sync MergeTreePartWriter path. Co-Authored-By: Claude Opus 4.6 <[email protected]>
| ++global_discr) | ||
| { | ||
| auto local_discr = global_to_local_discriminators[global_discr]; | ||
| variants[local_discr]->updateHashWithValueRange(0, variants[local_discr]->size(), hash); |
There was a problem hiding this comment.
❌ ColumnVariant::updateHashWithValueRange calls updateHashWithValueRange(0, size) for all variants in the fast path, including variants with size == 0.
This reaches nested overrides with an empty range. Several new range implementations use &data[begin] / offsetAt(begin) and are not safe for begin == end == 0 on empty columns, so this can read out of bounds (UB) during dedup hash computation.
Concrete path: a Variant(UInt64, String, Array(UInt32)) block containing only UInt64 values executes this loop, then calls the String/Array variant hash with (0, 0).
Please skip empty variants in the fast path, e.g. guard with if (variants[local_discr]->size() == 0) continue; before calling updateHashWithValueRange.
LLVM Coverage Report
Changed lines: 80.51% (157/195) | lost baseline coverage: 15 line(s) · Uncovered code |
|
Hi @CheSema @Avogar — while reviewing this PR I found the following:
Could you review the test PR and add the |
…ate squashing overhead
…ate squashing overhead
Backport #101494 to 26.3: Defer dedup data hash computation to eliminate squashing overhead
Backport #101494 to 26.2: Defer dedup data hash computation to eliminate squashing overhead
During INSERT with
deduplicate_insert=enable,calculateDataHashwas called eagerly inupdateOriginalBlockon every squashing step. For 10M rows this meant ~17 intermediate hash computations (each O(rows x columns) with SipHash), all discarded except the last.Fix 1: Lazy hash evaluation
updateOriginalBlocknow stores the block with COW-shared columns instead of computing the hash eagerly and discarding. The hash is computed lazily on first access in the sink viacalculateDataHash, which caches the result and then releases the block columns (restoring the original memory optimization).When
insert_deduplication_tokenis set, the data hash is skipped entirely — only the user token is used.Fix 2: Batch hashing via
updateHashWithValueRangeAdded
IColumn::updateHashWithValueRange(begin, end, hash)virtual method that hashes a range of rows in one call. The default implementation loops row-by-row; overrides inColumnVector,ColumnDecimal,ColumnFixedString,ColumnString,ColumnArray,ColumnNullable,ColumnTuple, andColumnMapfeed contiguous memory to SipHash, eliminating per-row virtual dispatch.The new unified hash path (
getBlockUnifiedHash) usescalculateDataHashBatchwith batch hashing. The old hash path (getBlockHash) keeps row-by-rowcalculateDataHashfor backward compatibility during theCOMPATIBLE_DOUBLE_HASHESmigration period.Benchmark results
5M rows, 22 columns (all overridden types), 5 runs averaged:
Hash overhead isolated (enable - disable):
Ref: #99241
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix significant INSERT performance regression when
deduplicate_insert=enable(default since 26.2) by deferring data hash computation from squashing to the sink and using batch column hashing viaupdateHashWithValueRange, reducing overhead from ~2.5s to ~0.5s for 5M rows with 22 columns.Documentation entry for user-facing changes
Version info
26.4.1.56126.3.5.2,26.2.9.4