Skip to content

[Performance]: composite index second-column range prunes nothing, constant prefix scan #32063

Closed as duplicate

Description

@Ariznawlll

Summary

A range predicate on the second column of a composite secondary index is not turned into an index key-range; it is planned as prefix_eq(first-col) plus a per-entry residual filter serial_extract(key, 1) > n evaluated over every entry of the first column's prefix. The observable behavior: on a 10M-row table with index (a, b), WHERE a = 9 AND b > N costs the same ~115 ms whether N matches 0 rows, 100k rows, or all 1M rows of the prefix — i.e. the range on b prunes nothing at the index level. For comparison a full table scan of the same table is 15 ms — the "optimized" index plan is ~8× slower than the sequential scan it replaced in the zero-match case.

Reproduction (main@HEAD, c6683bd)

CREATE TABLE comp_t (k INT PRIMARY KEY, a INT, b INT, KEY idx_ab (a, b));
-- 10,000,000 rows: a = k % 10 (1M rows/value), b = k % 100 (100k rows/value pair)
-- FLUSH + ANALYZE
query actual rows wall time (median of 5)
WHERE a = 9 AND b > 99 0 114.9 ms
WHERE a = 9 AND b > 90 100,000 116.7 ms
WHERE a = 9 AND b > -1 (degenerate) 1,000,000 126.7 ms
WHERE a = 9 AND b = 5 (point, both cols) 100,000* ~0 ms (instant)
full scan SELECT SUM(k) 10,000,000 15 ms

(*) point lookups on both columns are exact and fast — the machinery clearly supports two-column point predicates; only the second-column range degrades.

The plan shows exactly what happens:

Index Table Scan on comp_t.idx_ab
  Filter Cond: prefix_eq(__mo_index_idx_col), (serial_extract(__mo_index_idx_col, 1, INT)) > 90
  Block Filter Cond: prefix_eq(__mo_index_idx_col)

prefix_eq finds the a = 9 sub-range (1M entries); the b > 90 condition is then a residual serial_extract filter applied row-by-row across all of them — with no compiler-level knowledge that idx_ab is sorted by (a, b) and that b > 90 within a = 9 is itself a contiguous key range with a seekable start. The estimated outcnt=1000 (vs real 100k) further shows the stats path doesn't model the range either.

Impact

INDEX(a, b) with WHERE a = ? AND b > ? is the canonical composite-index access shape (time-series buckets: WHERE tenant = ? AND ts > ?). Here MO's composite index is worse than useless for that shape: it costs a constant 1M-entry scan + per-row UDF-ish serial_extract evaluate, ~8× a plain scan at 0 matches and with none of the pruning an ordered index provides. Users add composite indexes specifically for this pattern and get a silent slowdown.

Fix direction

In applyIndicesForFilters(RegularIndex) (pkg/sql/plan/apply_indices.go:703/:2378), when the filter set contains an equality prefix over index parts 0..i-1 and a range predicate over part i, emit a compound key-range (prefix_eq(a) AND prefix_range(b > 90) over the serialized key, or a start key (9, 90) seek) instead of prefix_eq + serial_extract residual. The serialized-key encoding already preserves part order, so constructing the start bound from the literal is mechanical. At minimum, treat the second-column range as a block filter so block-level zone maps on the index table can prune, instead of evaluating serial_extract per entry.

Activity

  1. added
    kind/bug
    on Nov 3, 2026
  2. added this to the MO-202611 milestone on Nov 3, 2026
  3. added theissue type on Nov 3, 2026
  4. XuPeng-SH commented on Nov 9, 2026

    @XuPeng-SH
    Contributor

    This issue is being consolidated into #32209, which now tracks secondary-index serialized range pruning. Keep the equality-prefix plus second-column range reproduction as a separate acceptance case; point lookups and full-scan controls remain useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions