Skip to content

[Performance]: index presence flips join to nested-loop, 18x slower than hash #32017

Open

Description

@Ariznawlll

Summary

Adding a secondary index on the join column makes an 8M-row join 17.6× slower: the planner switches from a hash join (full scan of the large table, tiny build side) to a nested-loop INDEX join driven by 8M index-ordered probes, and there is no row-count threshold to stop it.

Environment: main@HEAD (c6683bd), etc/launch-multi-cn.

Reproduction

-- big10m: 8,000,000 rows (k PK, k2 INT, KEY idx_k2 (k2)); b: 100k rows, 200 match grp<100
SELECT b.grp, COUNT(*) FROM big10m      JOIN b ON big10m.k2      = b.grp WHERE b.grp < 100 GROUP BY b.grp;
-- 853.4 ms   (plan: Index Table Scan on big10m.idx_k2 driving Join Type: INDEX per-row probes of b)

SELECT b.grp, COUNT(*) FROM big10m_noidx JOIN b ON big10m_noidx.k2 = b.grp WHERE b.grp < 100 GROUP BY b.grp;
--  48.5 ms   (identical table without idx_k2; plan: hash join, full scan + 200-row build)

Identical results (1,600,000). The only difference between the tables is the presence of idx_k2.

Related family (same root assumption that per-row indexed access is cheap): #32015 (filter-side join-back never yields to full scan, 200× at 10% selectivity) and #32013 (semijoin runtime-filter path, 27×). This case is the join-side variant: for an 8M-row outer with a 200-row inner, nested-loop probing is chosen over hashing with no cardinality gate — MySQL's optimizer makes the opposite call here (hash/block-nested-loop with join buffer, or in 8.0 hash join outright).

Note: a manual pre-aggregation rewrite (GROUP BY k2 first, then join) runs the same semantics in 81.1 ms, confirming the 853 ms plan is the worst of the three available strategies.

Activity

  1. added
    kind/bug
    on Nov 2, 2026
  2. added this to the MO-202611 milestone on Nov 2, 2026
  3. added theissue type on Nov 2, 2026
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