Skip to content

[Performance]: FORCE INDEX with unrelated ORDER BY drops selective index bounds #31858

Open

Description

@Ariznawlll

Description

With a selective composite secondary-index predicate, adding an unrelated ORDER BY to a query with unscoped FORCE INDEX causes the chosen index scan to lose its usable predicate. The index access remains present but reads and sends many unrelated keys to the index backfill join. The same ordered query without the hint uses the same index with the predicate, and the forced query without the order also retains it. Returned rows are correct; the problem is unnecessary work and latency.

Environment

  • MatrixOne main@d39d8a40343ccd40bf665036388df7a40ee867f8; runtime SHA verified.
  • Local isolated two-CN / one-TN deployment, query on one CN. Measurements below are warm-cache local measurements, not a production SLA.
  • Comparison: MySQL 8.3.0 on the same host with equivalent schema/data; architecture and configuration are not identical.

Steps to reproduce

CREATE DATABASE force_order_repro;
USE force_order_repro;
CREATE TABLE t (
  tenant INT NOT NULL, id INT NOT NULL, bucket INT, score INT,
  PRIMARY KEY (tenant,id), INDEX ix(bucket,score)
);
INSERT INTO t
SELECT MOD(result,97), result, MOD(result,1000),
       CASE WHEN MOD(result,41)=0 THEN NULL ELSE MOD(result,201)-100 END
FROM generate_series(120000) g;
ANALYZE TABLE t;

-- Automatic path: selective ix predicate retained.
EXPLAIN ANALYZE SELECT tenant,id,bucket,score FROM t
WHERE bucket=43 AND score BETWEEN -10 AND 10
ORDER BY tenant,id LIMIT 50;

-- Forced path: same index, but index predicate disappears.
EXPLAIN ANALYZE SELECT tenant,id,bucket,score FROM t FORCE INDEX(ix)
WHERE bucket=43 AND score BETWEEN -10 AND 10
ORDER BY tenant,id LIMIT 50;

-- Control: force alone does not prevent selective index filtering.
EXPLAIN ANALYZE SELECT tenant,id,bucket,score FROM t FORCE INDEX(ix)
WHERE bucket=43 AND score BETWEEN -10 AND 10;

Actual behavior

All forms return the same 13 rows (compare the unordered control as a set). In a representative analysis, automatic access has Index Table Scan input 120 / output 13, with prefix_eq and the score condition. Forced ordered access has no filter on the Index Table Scan, which inputs/outputs 40,960 rows; its backfill side reads 120,000 rows and filters to 13. These are reported operator counters, not physical disk-read byte counts. The forced query without ORDER BY retains the index filters and reports 120 / 13.

Three freshly created 120,000-row databases, ten alternating executions of each ordered query: MO automatic versus forced medians were 0.82 / 2.22 ms, 0.80 / 2.12 ms, and 3.26 / 5.25 ms. Independently, three differently skewed fixtures exercised from both CN entry points showed larger warm-cache differences for the selective ordered query (automatic 0.83–4.51 ms versus forced 8.50–41.10 ms). Results were checked on every run. MySQL 8.3 with the equivalent first fixture returned the same rows and automatic/forced medians of 0.21 / 0.14 ms. These are local observations, not a guarantee of a fixed ratio.

Expected behavior

A valid forced index should retain legal selective index bounds even when it cannot supply the requested order; the remaining sort can still enforce ORDER BY. Results and semantics must remain unchanged.

Stability and controls

  • Three fresh SQL-generated databases: 3/3 reproduce the filtered automatic versus unfiltered forced index plan and 60/60 repeated ordered result checks agree.
  • Three additional datasets, two MO entry CNs and MySQL comparison: 360/360 ordered/non-LIMIT ordered result comparisons agree. An invalid-column query is rejected and the source fingerprint and connection remain usable.
  • No inference of incorrect results, remote per-CN row counts, cold-cache behavior or production SLA.

Evidence and code analysis

The SQL above is self-contained and the issue includes the observed EXPLAIN ANALYZE row counts. The explicit forced-order fallback in apply_indices.go is a possible route to inspect; it has not been proven to be the root cause.

Regression coverage

Check selective equality/range on a composite index with plain FORCE INDEX, order matching and not matching index columns, LIMIT and no LIMIT, covering and backfill projections. Verify full results and the index-side predicate/candidate counts, with an unforced control and negative-path state checks.

Related

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

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions