mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
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:
co-authored by
Himanshu
Himanshu S
parent
0ba8b6f0a8
commit
80c889cd9e
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user