Repository navigation
Description
Description
For a high-frequency key in a composite secondary index, the automatic plan chooses index backfill and processes hundreds of thousands of matching rows before ORDER BY id LIMIT 50. An otherwise identical query that ignores this secondary index uses the primary-key-ordered scan with a Top-N bound and is substantially faster. Both return the same complete 50-row result.
Environment
main@bbfe55669a1440424914d45a283a1d0e36f722fa, runtime SHA verified.- Isolated local two-CN / one-TN cluster; timings are local warm-cache observations, not a production SLA.
Steps to reproduce
Create the seed data and snaps table using the complete Python fixture in #31829. Use independent rep values 0, 1 and 2 in three fresh databases. After each fixture, create the test table (replace N with 2, 3 and 4 respectively):
CREATE TABLE hot(
tenant INT, id BIGINT, k INT, v INT, flag INT,
PRIMARY KEY(tenant,id), INDEX ix_key(tenant,k,v)
);
INSERT INTO hot
SELECT tenant,id,CASE WHEN MOD(id,N)=0 THEN 1 ELSE k END,v,flag
FROM snaps;
SELECT id,k,v FROM hot WHERE tenant=1 AND k=1 ORDER BY id LIMIT 50;
SELECT id,k,v FROM hot IGNORE INDEX(ix_key)
WHERE tenant=1 AND k=1 ORDER BY id LIMIT 50;Execute both queries alternately and compare all returned rows; run EXPLAIN ANALYZE for both. Each table has 4,512,752 rows; the three transformed datasets derive from different seeded inputs and use different hotspot transforms.
Actual behavior
Six alternating repetitions per query and CN entry, with complete output equality in all 72 comparisons:
| Dataset | Matching rows | CN1 default / scan median | CN2 default / scan median |
|---|---|---|---|
| 0 | 389,912 | 1.579 s / 0.011 s | 0.793 s / 0.005 s |
| 1 | 263,533 | 0.944 s / 0.007 s | 0.822 s / 0.003 s |
| 2 | 373,744 | 1.084 s / 0.007 s | 1.047 s / 0.006 s |
An additional independently created hotspot table was tested with ten alternating repetitions per query and CN entry. Results remained equal; default medians were 0.986 s / 0.906 s versus 0.022 s / 0.022 s for scans. Other, lower-density distributions showed smaller differences; this is not a claim that every index access is slower.
Expected behavior
The optimizer should account for the high candidate count, index backfill, and the competing ordered-scan Top-N pruning when choosing a plan. Preserve exact ordering, ties, NULL semantics and the benefits of index access for selective values; do not unconditionally ignore the index.
Evidence and analysis
For dataset 0, EXPLAIN ANALYZE shows the default plan's Index Table Scan -> INDEX Join -> Sort LIMIT 50; both the index and base sides contribute 389,912 rows to the join, and the sort receives 389,912 rows. The scan control reports Sort LIMIT 50 with MsgTopValue and its Table Scan reads 66,612 rows / emits 34,775. These are operator row counters, not physical disk byte claims; timings are not a cold-cache or multi-client benchmark.
The source's regular-index selection compares estimated work against base work in apply_indices.go. Whether the underlying problem is selectivity estimates, the ordered-Top-N cost, or a combination needs investigation.
Stability and controls
- 3 separately generated hotspot tables, two CN entry points, alternating query order, six repetitions each: 72/72 exact comparisons match.
- Separate three-distribution index matrix covered point, range, NULL and dense predicates; 216 query results plus six failure/recovery checks match, with actual index plans verified.
- Invalid-column errors left source fingerprints unchanged and connections usable. No result-corruption claim is made.
Regression coverage
Measure and compare common/rare values, LIMIT and no LIMIT, projection requiring backfill versus index-only access, leading/non-leading order keys, ASC/DESC, fresh/stale statistics, different distributions and cold/warm cache. Verify exact result rows separately from timing and include cases where the index is genuinely beneficial.
Related
- [Performance]: Cost model chooses serial-extract-heavy index plan for EXISTS semi-join #26803 (closed) concerns encoded-key extraction costs in an EXISTS semi-join; this is a high-density index-backfill plus lost scan-side Top-N pruning shape.
- [Performance]: DESC ORDER BY with LIMIT misses the top-N fast path #31803 concerns DESC versus ASC ordering; both queries here use ASC and differ in access path.
- Data generation is shared with [Performance]: projected nullable EXISTS spends seconds on duplicate-heavy MARK join witnesses #31829; the query and mechanism are different.
Activity