Repository navigation
Description
Description
ORDER BY <pk columns> DESC LIMIT n pays a 16× penalty over the identical ASC query on current main — a pure-planner/executor performance gap in the single most common pagination pattern, with identical EXPLAIN plans for both directions (correctness is intact; this is a performance defect proven by timing + plan analysis).
Measured on a 200k-row table with composite PK (a,b,c) (main c6683bd2f5, 2026-10-30, dual-CN launch), median of 3 runs each:
| query | median |
|---|---|
SELECT a,b,c,v FROM bigfact ORDER BY a,b,c LIMIT 5 |
3 ms |
SELECT a,b,c,v FROM bigfact ORDER BY a DESC,b DESC,c DESC LIMIT 5 |
48 ms |
| same two queries without LIMIT (full sort) | 357 vs 359 ms (identical) |
| no ORDER BY, LIMIT 5 | 1 ms |
Key observations isolating the defect:
- EXPLAIN shows the same plan shape for both directions —
Sort (Sort Key: … [INTERNAL|DESC]) → Limit 5 → Table Scan. The 16× gap is not from plan selection. - The no-LIMIT row proves the sort itself handles both directions at identical cost — the divergence appears only when
Limitcomposes with a DESC sort key. - Natural inference: the ASC+Limit path is being executed as an early-terminating top-N (stop after 5 index-ordered rows / bounded heap), while the DESC key defeats that fast path and falls back to a much more expensive strategy (full materialization or in-memory heap of a wider buffer). Since both plans print identically, the asymmetry hides from plan inspection entirely — users profiling with EXPLAIN will see "the same plan" and conclude the slowness is elsewhere.
- A real-table shape where this bites: "latest N records" pages (
ORDER BY created_at DESC LIMIT 20) over any moderately sized table — with a DESC index defined or with a composite PK whose leading column is scanned in reverse.
Impact
- Every DESC-side pagination/latest-N query inherits an ~16× CPU/latency tax invisible in EXPLAIN. At 200k rows the absolute numbers are small, but the penalty is structural — it scales with data size up to the full-sort floor (357 ms here), while ASC-side stays top-N-bounded.
- Interactive dashboards whose default view is "newest first" are exactly the pattern that hits this.
Expected behavior
DESC sort keys with LIMIT should run the same bounded top-N execution as ASC (a max-heap instead of min-heap suffices; no planner change needed — the execution path for Sort(Limit) should pick its heap orientation from the sort-key direction).
Reproduction
CREATE TABLE bigfact(a INT, b INT, c INT, v INT, PRIMARY KEY(a,b,c)); -- 200k rows via loop
-- time both:
SELECT a,b,c,v FROM bigfact ORDER BY a,b,c LIMIT 5; -- 3 ms
SELECT a,b,c,v FROM bigfact ORDER BY a DESC,b DESC,c DESC LIMIT 5; -- 48 msmain @ c6683bd (2026-10-30), pymysql, medians of 3 runs, table rows 200,000.
Activity
Ariznawlll commentedon Oct 30, 2026
Cache-controlled refinement of the timing in this report (same build/table, full-table warm-up pass before each measurement — eliminates block-cache noise from the earlier cold-run 105→12 ms spread):
ORDER BY a,b,c LIMIT 5ORDER BY a DESC,b DESC,c DESC LIMIT 5ORDER BY a,b,c LIMIT 5 OFFSET 100ORDER BY a DESC,… LIMIT 5 OFFSET 100ORDER BY a,b,c LIMIT 5 OFFSET 199000ORDER BY a DESC,… LIMIT 5 OFFSET 199000Two sharpenings for the fix owner:
Limit: 5, Offset: 100keeps paying well past the point where a bounded top-(offset+limit) heap should have early-terminated — consistent with the DESC key missing the bounded-heap path entirely and materializing far more (or all) of the scan before applying offset+limit.EXPLAIN for all six shows the same
Sort → Limit/Offset → Table Scanshape — plan-invisible, execution-only divergence. Fix expectation unchanged: honor the sort-key direction when choosing/executing the top-N (max-heap) strategy so DESC composes with Limit/Offset at the same cost as ASC.Ariznawlll commentedon Oct 30, 2026
Layer-separation cross-link from a fresh finding: #31856 (filed just now) is the planner-side twin of this executor-side issue. Combined picture for triage:
Note the interaction: even after this issue's executor fix, an
ORDER BY indexed_col DESC LIMIT nstill scans the whole table per #31856; and after #31856's planner rules land (index-order delivery incl. DESC via reverse scan), the DESC-specific top-N heap defect in this issue may become mostly unreachable through the common shapes (delivery already ordered → no Sort→Limit composition at all). Suggest coordinating both fixes; either one alone leaves the other's tax visible.Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。当前 main 上,三列复合主键的倒序 Top-N 仍明显慢于正序;本轮结果正确。
关键证据
main@d39d8a40343ccd40bf665036388df7a40ee867f8,隔离双 CN / 单 TN;三个新建库各 200,000 行,使用 issue 的三列复合主键及原始两条LIMIT 5查询,各交替运行 10 次。正序中位数 3.07–3.39 ms,倒序 44.76–48.55 ms;返回值均与独立生成的数据排序一致。bigfact建表后执行INSERT INTO bigfact SELECT MOD(result,53),MOD(result,71),result,MOD(result,13) FROM generate_series(200000) g;,再运行 issue 中的两条查询及EXPLAIN ANALYZE。待解决点
perf(top): prune dominated rows in verified reverse-ordered batches (#…
XuPeng-SH commentedon Oct 30, 2026
QA handoff for merged #31847 at
a54850983d9cddaacc97e38a84515ec724c937b7. This round reviewed the issue and change but did not build or run functional or performance tests at the merge SHA: available memory is beyond the existing guard. The independentd39d8aQA failure predates this merge; the PR author's benchmark and tests are separate evidence, not results from this round.Please compare ASC/DESC LIMIT and shallow OFFSET under matched warm conditions, and verify results against a full-sort oracle across ties, NULL, mixed keys, multiple batches, spill/cancellation and cleanup. This optimization still consumes the input; index-order planning is separately tracked by #31856. Handoff does not claim a pass or a measured performance improvement.
Ariznawlll commentedon Oct 31, 2026
结论
测试通过。原 20 万行倒序 Top-N 性能问题显著改善;真实 SQL 的 spill 结果和执行中取消后的清理均通过本轮验证。
关键证据
main@ae40f12a647880a63466de32a9f507c2c72dca0b,本地隔离 2 CN / 1 TN / 1 LogService;实际服务二进制完整 SHA 一致且vcs.modified=false,修复 perf(top): prune dominated rows in verified reverse-ordered batches #31847 的合并提交a54850983d9cddaacc97e38a84515ec724c937b7在祖先链。LIMIT 5中位数分别约 3.2–3.3 / 7.9–8.3 ms,OFFSET 100约 3.3–3.4 / 9.1–9.3 ms。此前同机、同数据生成方式和拓扑的修复前d39d8a40343ccd40bf665036388df7a40ee867f8分别约 3.0 / 45.5 ms 和 3.0–3.1 / 92.7–93.0 ms。前后是独立 A/B 而非交错采样,不能当作生产 SLA;正倒序仍有差距,研发在 perf(top): prune dominated rows in verified reverse-ordered batches #31847 中明确不承诺方向耗时完全一致。LIMIT 16385超过 Top spill 门槛,双 CN 正序和倒序各 3 轮完整结果与独立全表排序一致;每轮EXPLAIN ANALYZE显示非零 spill:正序 24,576 行、约 384.53 KiB,倒序 200,000 行、约 3.06 MiB。原LIMIT 5、OFFSET 100及OFFSET 16385也按独立结果逐行比较;后者没有观察到 spill,不计为 spill 通过。top_spill 文件,再从独立连接执行KILL QUERY;双 CN 各 20 轮,共 40/40 返回 1317context canceled,线程结束、Top spill 句柄回到基线、原连接可执行SELECT 1,每轮源表仍为 200,000 行。连续一组取消前 RSS 约 143/137 MiB,结束后约 186/230 MiB、后续采样约 96/84 MiB;这不是峰值或长期泄漏证明。-race测试重复 3 次成功。NULL、并列键、混合方向和 OFFSET 的 MOTR 回归 在两 CN 各 3 轮通过;真实 spill/取消脚本及环境限制见 MOTR #298 验证记录,该 PR 的 Go 和 Python 离线 CI 均成功。待解决点