Skip to content

perf: stop per-query planning and index work from growing with table size - #114

Closed
zehiko wants to merge 2 commits into
release-10.0.0from
zeljko/r10-scale-cpu
Closed

zehiko wants to merge 2 commits into
release-10.0.0from
zeljko/r10-scale-cpu

Conversation

@zehiko

@zehiko zehiko commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Draft: one branch to load-test together, to be split into separate PRs once measured.

Why

On a Metastore chunks table 10x larger (446 fragments instead of 44, same rows per query), querier CPU per request grew by about half: T2 6.5 → 9.7 ms, D1 1.26 → 1.90 ms per sample.
Each query returns the same data at both sizes; the growth is per-query work that scales with the table:

Cost per T2 request (µs), 1x → 10x Cause Here
plan_scan 128 → 1,040, load_fragments 91 → 706, fragment-list clones 182 → 459 planning walks and loads every fragment, even when the index answer is in one or two backport of upstream lance-format#8587
open_scalar_index → try_from_serialized 253 → 738 every btree cache hit rebuilds BTreeLookup, walking every page new: keep the built lookup in the cached state

What

  1. Backport of perf(filtered-read): prune non-candidate fragment metadata lance-format/lance#8587 (perf(filtered-read): prune non-candidate fragment metadata), on top of perf(filtered-read): reuse plan-time fragment metadata for the read stream lance-format/lance#7792 which release-10.0.0 already has.
    Three conflicts, all additive: prune_ranges_by_page_values (from perf(encoding): backport per-page, range-scoped page initialization #112) and intersect_index_ranges (from perf(filtered-read): prune non-candidate fragment metadata lance-format/lance#8587) both added next to each other, plus test imports.
  2. BTreeIndexState holds Arc<BTreeLookup> instead of the lookup batch.
    It is built once when the state is loaded or deserialized; reconstruct reuses it.
    The serialized form is unchanged.
    Upstream main still rebuilds on every hit, so this one is new and upstreamable.

A third change, caching the max chunk length per manifest instead of reading index statistics per query, is on the Metastore side.

Testing

  • cargo test -p lance-index --lib scalar::btree: 50 passed.

  • cargo test -p lance --lib io::exec::filtered_read: 77 passed.

  • Load tests on a 1 CU stack, same stack and datasets, querier saturated, Metastore image with this branch (plus the Metastore-side max-length cache) vs the previous one:

    Throughput 1x (before) 10x before → after 17x before → after
    T2 req/s 488–504 328 → 437 (+33%) 201 → 319 (+59%)
    D1 samples/s 2,445 1,666 → 2,034 (+22%) 1,223 → 1,682 (+38%)
    A2 req/s 52 24 → 24 —

    A2 is unchanged: its extra cost is reading more fragments per request (each of its requests targets up to 400 segments), which these changes do not address.

  • perf on the querier, T2 on 10x, µs of CPU per request before → after: total 9,674 → 7,186; plan_scan 1,040 → 47; mask_to_offset_ranges 453 → 17; load_fragment(s) 973 → 504; btree reconstruct 564 → 5; index_statistics (Metastore side) 691 → 0.

Agent

This PR was written by Claude Code (an LLM), at the request of the author.

🤖 Generated with Claude Code

BubbleCal and others added 2 commits November 16, 2026 16:31
…mat#8587)

After a scalar index returns its row-ID mask,
`FilteredReadExec::get_or_create_plan_impl` still loads deletion
vectors, row counts, and row-ID metadata for every fragment before
applying that mask. An exact index miss therefore performs an O(fragment
count) metadata pass even though no fragment can contribute a row.

This is a follow-up to lance-format#7792. That PR removed the second all-fragment
metadata load during stream construction; the first planning-time pass
remained. Related to lance-format#4189, which tracks broader filtered-read planning
costs.

Use the index result's upper bound to decide whether a fragment can
contribute before loading its full metadata:

- skip every covered fragment for an exact empty allow-list;
- use the fragment portion of address-style row IDs for direct pruning;
- for non-empty stable row-ID masks, use each fragment's row-ID sequence
to route candidates, then load deletion vectors and row counts only for
candidate fragments;
- carry the routed stable row-ID sequence and upper offset ranges into
final planning, so exact and refined/at-most results do not map the same
IDs twice;
- cap retained stable-ID range payload at 16 MiB per plan and drain
retained vectors fragment-by-fragment; over-budget fragments reuse the
loaded row-ID sequence but recompute ranges during final planning;
- keep uncovered fragments unless `only_indexed_fragments` is enabled;
- conservatively keep block-list upper bounds and every fragment needed
to calculate a pre-filter scan range.

Candidate fragments still load and apply their deletion vectors, so
stale deleted index hits remain excluded. Pruning uses the upper bound
of refined index results to avoid false negatives.

Lower is better for every metric below.

| Scenario / metric | Baseline (`e958adfdf`) | This PR (`150e000f0`) |
Benefit |
| --- | ---: | ---: | ---: |
| Exact-empty plan latency | 1,630.20 ms/query | 5.14 ms/query | 316.9x
speedup |
| Indexed zero-hit query latency | 2,067.66 ms/query | 84.73 ms/query |
24.4x speedup |
| Indexed one-hit query latency | 2,081.91 ms/query | 163.69 ms/query |
12.7x speedup |
| Exact-empty plan S3 reads | 2,600 reads/query | 0 reads/query | 2,600
reads/query eliminated |
| Indexed zero-hit S3 reads | 2,603 reads/query | 3 reads/query | 867.7x
fewer reads |
| Indexed one-hit S3 reads | 2,606 reads/query | 7 reads/query | 372.3x
fewer reads |

The review follow-up also measures cache-hot dense stable-row-ID
planning, where all 5,200 physical row IDs route to all 2,600 fragments:

| Scenario / metric | Before follow-up (`824551d44`) | This PR
(`150e000f0`) | Benefit |
| --- | ---: | ---: | ---: |
| Dense stable-ID plan latency | 9.17 ms/plan | 7.86 ms/plan | 1.17x
speedup |

The main-branch control was 7.13 ms/plan. The current implementation is
0.72 ms/plan (1.10x) above that control because it performs the
stable-ID routing pass needed for pruning, but it no longer repeats the
same mapping during final planning. All cache-hot measured plans
performed 0 S3 reads.

Environment and methodology:

- AWS `m7i.4xlarge` in `us-east-1a`, reading S3 in the same region.
- One dataset with 2,600 stable-row-ID fragments, 2 rows per fragment,
one real deletion vector per fragment, 14 payload columns, and BTree
indices on `org_id` and `repo_id`.
- Separate main, pre-follow-up, and current PR binaries built with Rust
1.97.1, `release-with-debug`, `--no-default-features --features aws`;
their SHA256 hashes were checked before measurement.
- Three serialized trials per case; base/PR order alternated by trial.
Each trial used a new process and fresh Lance `Session`; the table
reports medians.
- Dataset-open time and I/O were excluded. Query/planning I/O uses
incremental `IOTracker` statistics after open.
- The dense case used three processes per revision. Each process ran one
excluded S3 warm-up plan followed by 21 measured plans; the table
reports the median of the three process medians. Index-result
serialization and exec construction were outside the timed region.
- This measures the metadata path against real S3, not the Plan Executor
persistent-disk cache.

Measured bytes followed the same pattern: exact-empty planning dropped
from 1,814,800 B/query to 0 B/query; the zero-hit query dropped from
1,882,874 B/query to 68,074 B/query; the one-hit query dropped from
1,965,927 B/query to 151,825 B/query.

The regression coverage includes stable and address-style row IDs,
exact-empty and sparse non-empty masks, refined upper bounds, partially
indexed datasets, and `only_indexed_fragments` behavior. A deterministic
dense stable-ID test counts `mask_to_offset_ranges` spans: four
candidate fragments must produce exactly four mappings; the
pre-follow-up implementation produced eight. A separate boundary test
verifies that retained range payload cannot exceed the per-plan budget.

For a non-empty stable row-ID mask, this change still visits each
covered fragment's row-ID sequence to discover candidate fragments. The
benchmark fixture stores those sequences inline, so it removes the S3
deletion-vector/row-count reads but not the O(fragment count) routing
walk. Datasets with external row-ID metadata can still perform
O(fragment count) row-ID metadata reads. Eliminating that remaining cost
requires carrying physical candidate-fragment information from
scalar-index execution or persisted row-ID routing metadata.

- `cargo fmt --all`
- `cargo test -p lance io::exec::filtered_read::tests --
--test-threads=1` (75 passed)
- `cargo clippy --all --tests --benches -- -D warnings`
- `RUSTFLAGS='-D warnings' cargo +nightly-2026-07-13 check -p lance
--tests`
- AWS S3 benchmark described above

(cherry picked from commit f177fae)
A cache hit on BTreeIndexState rebuilt BTreeLookup from the lookup batch,
walking every page to find null pages, on every index open. That made each
open cost grow with the index size: on a 446-fragment table it was ~0.5 ms
per open, paid by every scalar-index query and every index_statistics call.

The cached state now holds the built Arc<BTreeLookup>, so it is built once
when the state is loaded or deserialized and shared by every reconstruct.
The serialized form is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants