From 80c889cd9e9807dc8f2e643ca6916512d4ef6c72 Mon Sep 17 00:00:00 2001 From: Himanshu Saini Date: Mon, 30 Nov 2026 13:23:27 +0000 Subject: [PATCH] docs: explain merge insert execution costs (#4501) resolves #4258 ## Summary - document how indexed partial-column merges patch columns across touched fragments - explain how full-scan merges rewrite rows and can materialize the target table - describe the index-coverage, batching, and memory tradeoffs - align the Python, Rust, and TypeScript `use_index` API documentation ## Verification - `git diff --check` - Ruff 0.15.20 formatting check for `python/python/lancedb/merge.py` - Biome 1.9.4 check for `nodejs/lancedb/merge.ts` - reviewed the rendered MDX structure and cross-SDK wording The TypeDoc generation command was also attempted. It cannot complete in this checkout because the Rust-generated `nodejs/lancedb/native.d.ts` artifact is absent and Rust tooling is not installed; TypeDoc stops on the resulting pre-existing module-resolution errors before generating output. --------- Signed-off-by: Himanshu S Co-authored-by: Himanshu <18554133+mrhimanshu@users.noreply.github.com> Co-authored-by: Himanshu S --- docs/src/js/classes/MergeInsertBuilder.md | 30 +++++++++++++---- docs/web/ingestion/merge-insert.mdx | 40 +++++++++++++++++++++++ nodejs/lancedb/merge.ts | 30 +++++++++++++---- python/python/lancedb/merge.py | 30 +++++++++++++---- rust/lancedb/src/table/merge.rs | 28 ++++++++++++---- 5 files changed, 134 insertions(+), 24 deletions(-) diff --git a/docs/src/js/classes/MergeInsertBuilder.md b/docs/src/js/classes/MergeInsertBuilder.md index d4ee34b13..02f0a7ea3 100644 --- a/docs/src/js/classes/MergeInsertBuilder.md +++ b/docs/src/js/classes/MergeInsertBuilder.md @@ -58,17 +58,35 @@ the merge result useIndex(useIndex): MergeInsertBuilder ``` -Controls whether to use indexes for the merge operation. +Controls the join and write strategy for the merge operation. -When set to `true` (the default), the operation will use an index if available -on the join key for improved performance. When set to `false`, it forces a full -table scan even if an index exists. This can be useful for benchmarking or when -the query optimizer chooses a suboptimal path. +When `true` (the default), the operation can use scalar indexes on the join +keys when the operation supports the indexed path and all join keys have +usable indexes. When a partial-column update actually takes this path, it +patches the supplied non-key columns, rewriting those columns for every row +in each fragment containing a match. + +An indexed partial update preserves index coverage on the join keys, but indexes +on the rewritten non-key columns, including vector indexes, lose coverage in the +patched fragments. Newly written rows, including inserted rows and full-row +replacements, are not covered by existing indexes until the indexes are +optimized. + +When `false`, the operation performs a full-scan join and rewrites matched rows +by deleting and appending them. This can also happen with `true`, for example +when a join key lacks a usable index or a partial-column update includes a +blob column. Any full-scan plan can materialize the entire target table on the +hash-join build side and exhaust the server on large tables. The default flag +alone is not a memory safeguard. Do not treat `false` as a general-purpose +row-rewrite switch. + +See [How merge insert executes](https://docs.lancedb.com/ingestion/merge-insert#how-merge-insert-executes) +for the performance and index-coverage tradeoffs. #### Parameters * **useIndex**: `boolean` - Whether to use indices for the merge operation. Defaults to `true`. + Whether to allow index use for the merge. Defaults to `true`. #### Returns diff --git a/docs/web/ingestion/merge-insert.mdx b/docs/web/ingestion/merge-insert.mdx index 2d6fd8470..c32963723 100644 --- a/docs/web/ingestion/merge-insert.mdx +++ b/docs/web/ingestion/merge-insert.mdx @@ -214,6 +214,46 @@ Expected table contents: Note that in the example above, when `merge_insert` creates a new row, any missing columns are written as `null`. If a missing column is non-nullable in your schema, the insert will fail. +## How merge insert executes {#how-merge-insert-executes} + +`merge_insert` first joins the incoming rows to the target table on the columns passed to +`merge_insert(...)`. Its physical write strategy depends on whether that join uses a scalar index: + +- With `use_index=True` (the default), LanceDB can use scalar indexes on the join keys when the + operation supports the indexed path and all join keys have usable indexes. When a partial-column + update actually takes this path, it patches the supplied non-key columns of the matched rows in + their existing fragments. Every updated column is read and rewritten for every row in each + fragment that contains a match, so a small but scattered update can still touch much of the table. +- With `use_index=False`, LanceDB performs a full-scan join and rewrites matched rows by deleting + them and appending complete replacement rows. This can also happen with `use_index=True`, for + example when a join key lacks a usable index or a partial-column update includes a blob column. + +Within the fragments touched by a merge, column patching is generally favorable when the fraction +of rows matched is greater than the fraction of each row represented by the updated columns. For +sparse updates spread across many fragments, grouping work by destination fragment can reduce how +much data is rewritten more than simply reducing the number of rows in each batch. + +### Index coverage after a merge {#index-coverage-after-a-merge} + +How a merge affects existing indexes depends on how each row is written: + +- **Matched rows patched by an indexed partial update.** The join keys are not rewritten, so their + existing index coverage on the patched fragments is preserved. The supplied non-key columns are + rewritten, so indexes on those columns, including vector indexes, lose coverage for the patched + fragments until the indexes are optimized. +- **Newly written rows.** Inserted rows and matched rows that are replaced by deleting and appending + them are written to new fragments, which existing indexes do not cover until the indexes are + optimized. A full-row replacement can write new rows even when the lookup used an index. + + +Do not use `use_index=False` as a general-purpose row-rewrite switch for large tables. The full-scan +plan can materialize the entire target table on the hash-join build side, so memory use grows with +the target table rather than with the incoming batch and can exhaust the server. This warning also +applies to full-scan fallbacks with `use_index=True`: the default flag alone is not a memory +safeguard. Confirm that the merge can use the indexed path and profile representative data under +your memory limits. + + ## Check the merged records {#verify} Inspect the keys that should have changed, confirm that unrelated rows remain, diff --git a/nodejs/lancedb/merge.ts b/nodejs/lancedb/merge.ts index 7d95d4a36..f7b09d066 100644 --- a/nodejs/lancedb/merge.ts +++ b/nodejs/lancedb/merge.ts @@ -72,14 +72,32 @@ export class MergeInsertBuilder { } /** - * Controls whether to use indexes for the merge operation. + * Controls the join and write strategy for the merge operation. * - * When set to `true` (the default), the operation will use an index if available - * on the join key for improved performance. When set to `false`, it forces a full - * table scan even if an index exists. This can be useful for benchmarking or when - * the query optimizer chooses a suboptimal path. + * When `true` (the default), the operation can use scalar indexes on the join + * keys when the operation supports the indexed path and all join keys have + * usable indexes. When a partial-column update actually takes this path, it + * patches the supplied non-key columns, rewriting those columns for every row + * in each fragment containing a match. * - * @param useIndex - Whether to use indices for the merge operation. Defaults to `true`. + * An indexed partial update preserves index coverage on the join keys, but indexes + * on the rewritten non-key columns, including vector indexes, lose coverage in the + * patched fragments. Newly written rows, including inserted rows and full-row + * replacements, are not covered by existing indexes until the indexes are + * optimized. + * + * When `false`, the operation performs a full-scan join and rewrites matched rows + * by deleting and appending them. This can also happen with `true`, for example + * when a join key lacks a usable index or a partial-column update includes a + * blob column. Any full-scan plan can materialize the entire target table on the + * hash-join build side and exhaust the server on large tables. The default flag + * alone is not a memory safeguard. Do not treat `false` as a general-purpose + * row-rewrite switch. + * + * See [How merge insert executes](https://docs.lancedb.com/ingestion/merge-insert#how-merge-insert-executes) + * for the performance and index-coverage tradeoffs. + * + * @param useIndex - Whether to allow index use for the merge. Defaults to `true`. */ useIndex(useIndex: boolean): MergeInsertBuilder { return new MergeInsertBuilder( diff --git a/python/python/lancedb/merge.py b/python/python/lancedb/merge.py index c861414aa..121690f25 100644 --- a/python/python/lancedb/merge.py +++ b/python/python/lancedb/merge.py @@ -100,17 +100,35 @@ class LanceMergeInsertBuilder(object): def use_index(self, use_index: bool) -> LanceMergeInsertBuilder: """ - Controls whether to use indexes for the merge operation. + Controls the join and write strategy for the merge operation. - When set to `True` (the default), the operation will use an index if available - on the join key for improved performance. When set to `False`, it forces a full - table scan even if an index exists. This can be useful for benchmarking or when - the query optimizer chooses a suboptimal path. + When ``True`` (the default), the operation can use scalar indexes on the + join keys when the operation supports the indexed path and all join keys + have usable indexes. When a partial-column update actually takes this path, + it patches the supplied non-key columns, rewriting those columns for every + row in each fragment containing a match. + + An indexed partial update preserves index coverage on the join keys, but indexes + on the rewritten non-key columns, including vector indexes, lose coverage in the + patched fragments. Newly written rows, including inserted rows and full-row + replacements, are not covered by existing indexes until the indexes are + optimized. + + When ``False``, the operation performs a full-scan join and rewrites matched + rows by deleting and appending them. This can also happen with ``True``, for + example when a join key lacks a usable index or a partial-column update + includes a blob column. Any full-scan plan can materialize the entire target + table on the hash-join build side and exhaust the server on large tables. + The default flag alone is not a memory safeguard. Do not treat ``False`` as + a general-purpose row-rewrite switch. + + See [How merge insert executes](https://docs.lancedb.com/ingestion/merge-insert#how-merge-insert-executes) + for the performance and index-coverage tradeoffs. Parameters ---------- use_index: bool - Whether to use indices for the merge operation. Defaults to `True`. + Whether to allow index use for the merge. Defaults to ``True``. """ self._use_index = use_index return self diff --git a/rust/lancedb/src/table/merge.rs b/rust/lancedb/src/table/merge.rs index 79c724f3e..5f9363dcd 100644 --- a/rust/lancedb/src/table/merge.rs +++ b/rust/lancedb/src/table/merge.rs @@ -187,14 +187,30 @@ impl MergeInsertBuilder { self } - /// Controls whether to use indexes for the merge operation. + /// Controls the join and write strategy for the merge operation. /// - /// When set to `true` (the default), the operation will use an index if available - /// on the join key for improved performance. When set to `false`, it forces a full - /// table scan even if an index exists. This can be useful for benchmarking or when - /// the query optimizer chooses a suboptimal path. + /// When `true` (the default), the operation can use scalar indexes on the join + /// keys when the operation supports the indexed path and all join keys have + /// usable indexes. When a partial-column update actually takes this path, it + /// patches the supplied non-key columns, rewriting those columns for every row + /// in each fragment containing a match. /// - /// If not set, defaults to `true` (use index if available). + /// An indexed partial update preserves index coverage on the join keys, but + /// indexes on the rewritten non-key columns, including vector indexes, lose + /// coverage in the patched fragments. Newly written rows, including inserted + /// rows and full-row replacements, are not covered by existing indexes until the + /// indexes are optimized. + /// + /// When `false`, the operation performs a full-scan join and rewrites matched rows + /// by deleting and appending them. This can also happen with `true`, for example + /// when a join key lacks a usable index or a partial-column update includes a + /// blob column. Any full-scan plan can materialize the entire target table on the + /// hash-join build side and exhaust the server on large tables. The default flag + /// alone is not a memory safeguard. Do not treat `false` as a general-purpose + /// row-rewrite switch. + /// + /// See [How merge insert executes](https://docs.lancedb.com/ingestion/merge-insert#how-merge-insert-executes) + /// for the performance and index-coverage tradeoffs. pub fn use_index(&mut self, use_index: bool) -> &mut Self { self.use_index = use_index; self