You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Performance]: No index merge for multi-column OR #32203
Table: cov8m (k INT PRIMARY KEY, g INT NOT NULL, v INT NOT NULL, KEY idx_g(g), KEY idx_v(v)) — 30,000,000 rows
Reproduction
-- Query: OR across two *different but individually indexed* columnsSELECTCOUNT(*) FROM cov8m WHERE g =5OR v =42;
Each branch alone uses its index efficiently:
Query
Time
WHERE g = 5
57 ms
WHERE v = 42
38 ms
WHERE g = 5 OR v = 42
543 ms — full table scan
MySQL's index_merge union answers this in a few ms by running both index lookups and merging. An equivalent hand-written UNION ALL rewrite is also worse (1074 ms), so the user has no workaround.
Plan
AP QUERY PLAN ON ONE CN(8 core)
-> Table Scan on jumpstats.cov8m
Filter Cond: ((cov8m.v = 42) or (cov8m.g = 5)) -- no index at all
Root cause (code)
There is no index-merge mechanism: apply_indices.go evaluates per-index candidates (matchRegularIndexOnlyScan, point backfill, etc.) and picks a single best index; no code path unions multiple indexes for a disjunctive predicate. A repo-wide search for IndexMerge/index_union/finds nothing.
Additionally, predicate splitting of a top-level OR into per-branch index scans does not exist: the OR stays a single residual filter on the base table.
Impact
col_a = X OR col_b = Y over two indexed columns is a very common OLTP shape (filter by either of two keys, e.g. user_id = ? OR order_no = ?). On large tables it always degrades to a full scan, silently losing both indexes. Related to the same-column OR underestimation issue (#32059, estimation side) — this one is the execution-path side: no index merge.
Suggested fix direction
Split top-level ORs into per-branch access paths (by index when a branch's columns are covered) and merge rows with dedup (or leave dedup to UNION semantics as MySQL index_merge union does).
Merged PR #32086 (#32086), commit 54a6b17, introduces pkg/sql/plan/index_merge.go and its invocation from apply_indices.go. The commit is an ancestor of inspected origin/main b6f5f59; live main also retains the implementation. TestRegularIndexMergePublicPlans verifies unhinted COUNT(*) WHERE a=3 OR b=10 uses both secondary indexes, one distinct UNION, and no base-table scan. test/distributed/cases/optimizer/secondary_index_or_merge.sql and .result verify two-index execution and matching counts against a scan, including overlapping matches and NULLs.
核对现有修复审计后,关联已合并 main PR #32086,merge SHA 54a6b173282c1df71fcbf54fff42869a27cf4fc5,最终 head b1d9e4672a19f14079e7caea433901825ff3b805。该修复新增跨普通二级索引 OR 的成本准入路径,按物理主键做 distinct union;精确 COUNT/主键覆盖场景不回扫基表,非覆盖场景复用一次 INDEX 回表并保留残余条件。它对应本 issue 所述缺失的访问能力,而非承诺所有 OR 都使用索引。
证据:已读取修复范围、公开 SQL case/result 和完整讨论。作者报告 planner/消费者测试通过、BVT 68/68 两次、两 CN 对照,以及无 hint 的 200,000 行案例扫描输入降至 401 行/两个索引块。评审 5751430102 针对较早 head 224ce6f830c89e43c7e6fcc4e61ce8d430e1440a 报告 38 个聚焦用例、完整 planner 和 20 个消费者用例通过;其中完整 planner 有两个既有跳过。此评审不能冒称对最终 head 的本轮独立实测。合并队列记录 CI Required success。
本轮只做远端证据核对,未运行 SQL、构建或测试;本 issue 原始 30M 数据集及 543 ms/1074 ms 性能尚未重放。请 QA 在包含该 merge 的 main 上复测原始无 hint COUNT 查询,核对两路索引、去重、实际读取行/块与耗时,并以 IGNORE INDEX 独立对照;覆盖重叠匹配、NULL、非覆盖投影和 LIMIT/OFFSET。统计缺失、低选择率、超过八个分支或不支持形态可按成本/安全规则回退扫描,不应把合法回退直接视为缺陷,也不能仅凭出现索引节点宣称性能验收。
Activity
gouhongshen commentedon Nov 13, 2026
Existing-fix audit
Assessment: already fixed on current main
Fixed evidence
Merged PR #32086 (#32086), commit 54a6b17, introduces pkg/sql/plan/index_merge.go and its invocation from apply_indices.go. The commit is an ancestor of inspected origin/main b6f5f59; live main also retains the implementation. TestRegularIndexMergePublicPlans verifies unhinted COUNT(*) WHERE a=3 OR b=10 uses both secondary indexes, one distinct UNION, and no base-table scan. test/distributed/cases/optimizer/secondary_index_or_merge.sql and .result verify two-index execution and matching counts against a scan, including overlapping matches and NULLs.
XuPeng-SH commentedon Nov 13, 2026
QA 交接(非验收通过)
核对现有修复审计后,关联已合并 main PR #32086,merge SHA
54a6b173282c1df71fcbf54fff42869a27cf4fc5,最终 headb1d9e4672a19f14079e7caea433901825ff3b805。该修复新增跨普通二级索引 OR 的成本准入路径,按物理主键做 distinct union;精确 COUNT/主键覆盖场景不回扫基表,非覆盖场景复用一次 INDEX 回表并保留残余条件。它对应本 issue 所述缺失的访问能力,而非承诺所有 OR 都使用索引。证据:已读取修复范围、公开 SQL case/result 和完整讨论。作者报告 planner/消费者测试通过、BVT 68/68 两次、两 CN 对照,以及无 hint 的 200,000 行案例扫描输入降至 401 行/两个索引块。评审 5751430102 针对较早 head
224ce6f830c89e43c7e6fcc4e61ce8d430e1440a报告 38 个聚焦用例、完整 planner 和 20 个消费者用例通过;其中完整 planner 有两个既有跳过。此评审不能冒称对最终 head 的本轮独立实测。合并队列记录 CI Required success。本轮只做远端证据核对,未运行 SQL、构建或测试;本 issue 原始 30M 数据集及 543 ms/1074 ms 性能尚未重放。请 QA 在包含该 merge 的 main 上复测原始无 hint COUNT 查询,核对两路索引、去重、实际读取行/块与耗时,并以 IGNORE INDEX 独立对照;覆盖重叠匹配、NULL、非覆盖投影和 LIMIT/OFFSET。统计缺失、低选择率、超过八个分支或不支持形态可按成本/安全规则回退扫描,不应把合法回退直接视为缺陷,也不能仅凭出现索引节点宣称性能验收。
完整 issue 讨论目前未见已合并修复上的已验证反例或未完成开发 follow-up,转交 Ariznawlll 验证;保持 issue 开放,不代表原始规模性能已验收。
Ariznawlll commentedon Nov 14, 2026
结论
测试通过。最新 main 在三 CN、3000 万行数据下可使用两路二级索引完成 OR 查询,结果与独立全表扫描一致。
关键证据
main@b465f86824151a6d596bb67ca2211302fad4e368,独立 Linux 环境,1 LogService / 1 TN / 3 CN;三个 SQL 入口均核对git_version()完整 SHA。数据为合成分布,共 30,000,003 行。WHERE g=5 OR v=42:每个 CN 各 4 次,共 12 次;默认计划和IGNORE INDEX对照均返回 31,001。覆盖两路匹配重叠去重、NULL,以及非覆盖列投影和 LIMIT/OFFSET 对照。EXPLAIN ANALYZE:默认计划分别通过两路索引读取 30,001 和 1,001 行,合并去重为 31,001 行;全表对照读取 30,000,003 行。12 次默认查询中位约 11.15 ms,全表对照中位约 504.35 ms;这是本次合成数据和 warm-cache 条件下的测量,并非生产时延承诺。main@71390fb3c16f986b5cbe77e44ea0e5b7f5dbcff6上曾以两个 CN 各 3 轮通过;不将其记作本次 SHA 的 BVT 运行。待解决点