Skip to content

Commit 07083e8

Browse files
Update 'The Infamous ORDER BY LIMIT Query Optimizer Bug' re 8.0.40 bug fix
1 parent 16ef437 commit 07083e8

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

content/infamous-order-by-limit-query-optimizer-bug.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "The Infamous ORDER BY LIMIT Query Optimizer Bug"
55
subtitle: "16 Years of Fun"
66
tags: ["mysql", "query-optimization", "bug"]
77
comments: true
8-
lastmod: "2023-11-29T09:56:00-05:00"
8+
lastmod: "2025-04-23T13:37:00-04:00"
99
aliases:
1010
- /post/infamous-order-by-limit-query-optimizer-bug/
1111
disqus_url: "https://hackmysql.com/post/infamous-order-by-limit-query-optimizer-bug/"
@@ -339,7 +339,15 @@ When this optimizer flag is off, the function that does the cost calculations de
339339
340340
[^1]: [WL#13929: Introduce new optimizer switch to disable limit optimization](https://dev.mysql.com/worklog/task/?id=13929)
341341
342-
Then everyone asks: "Should I disable this optimization (globally)?"
342+
Should you disable this optimization (globally)?
343+
344+
**MySQL ≥ 8.0.40**
345+
346+
Yes.
347+
Disabling `prefer_ordering_index` is safe and recommended in MySQL ≥ 8.0.40 because I [fixed the bug](https://bugs.mysql.com/bug.php?id=113699) mentioned below.
348+
This flag defaults to on, so disabling it requires explicitly configuring [`optimizer_switch`](https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_optimizer_switch).
349+
350+
**MySQL ≤ 8.0.39**
343351
344352
Yes
345353
: But only for _new development_.
@@ -362,11 +370,12 @@ If you lose, you can quickly re-enable it, or use `FORCE INDEX` to force the sec
362370
<div class="note warn left-icon">
363371
<img class="ion" src="/img/ionicons/warning-outline.svg">
364372
<p>
365-
Disabling <code>prefer_ordering_index</code> causes another bug: MySQL does not scan the primary key for <code>SELECT ... FROM t ORDER BY pk_col LIMIT n</code>.
373+
Disabling <code>prefer_ordering_index</code> in MySQL &le; 8.0.39 causes another bug: MySQL does not scan the primary key for <code>SELECT ... FROM t ORDER BY pk_col LIMIT n</code>.
366374
Instead, it does a <em><b>full</b> table scan</em> plus sort, which is unnecessary and very likely to cause problems.
367375
Unlike the main subject of this blog post, I would call this a real bug.
368376
Thank you to Jay Janssen and Morgan Tocker for brining this to my attention.
369-
When I have time, I'll debug the source code for this and write another blog post.
377+
<br><br>
378+
I fixed <a href="https://bugs.mysql.com/bug.php?id=113699">this bug</a> in MySQL 8.0.40.
370379
</p>
371380
</div>
372381

0 commit comments

Comments
 (0)