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 <me.mrhimanshu@gmail.com>
Co-authored-by: Himanshu <18554133+mrhimanshu@users.noreply.github.com>
Co-authored-by: Himanshu S <me.mrhimanshu@gmail.com>
This commit is contained in:
Himanshu Saini
2026-11-30 21:23:27 +08:00
committed by GitHub
co-authored by Himanshu Himanshu S
parent 0ba8b6f0a8
commit 80c889cd9e
5 changed files with 134 additions and 24 deletions
+40
View File
@@ -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.
<Warning>
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.
</Warning>
## Check the merged records {#verify}
Inspect the keys that should have changed, confirm that unrelated rows remain,