Bug #75750 | incorrect sorting results when using float field and LIMIT clause | ||
---|---|---|---|
Submitted: | 3 Feb 2015 14:10 | Modified: | 6 Feb 2015 14:48 |
Reporter: | Michał Grabowski | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S2 (Serious) |
Version: | 5.6.22, 5.6.23 | OS: | Linux (gentoo) |
Assigned to: | CPU Architecture: | Any | |
Tags: | FLOAT, limit, OFFSET, order by, regression |
[3 Feb 2015 14:10]
Michał Grabowski
[5 Feb 2015 20:30]
Sveta Smirnova
Thank you for the report. Verified as described. Bug is not repeatable with versions 5.5 and 5.7
[6 Feb 2015 8:09]
Øystein Grøvlen
Posted by developer: This issue comes up once in a while. Note that MySQL treat the two queries as two separate queries. The optimizer will try to find the optimal query plan for each query, and may come up with different query plans that give different ordering of the result if ordering is not fully specified. If one wants to have the same ordering for both queries, one will have to fully specify the ordering. For the given example, the ORDER BY clause should be "ORDER BY position, id".
[6 Feb 2015 9:58]
Michał Grabowski
Thank You for you answer. What do You mean saying " if ordering is not fully specified", does ORDER by position isn't enough? Why this query work well in 5.5 and 5.7 ? Don't You think that this behavior in 5.6.22 is a little bit surprising and confusing? This leads to unpredictable results.
[6 Feb 2015 10:06]
Michał Grabowski
Additionally only float field are affected, so how Your tip refer to other field types ?
[6 Feb 2015 11:56]
Øystein Grøvlen
"ORDER by position" does not fully specify the order because any order of the ids with equal position is allowed. If the two queries use different access methods, (e.g. one uses table scan with file sort, the other uses index range scan), order of rows may be different. In 5.5 and 5.7, it may seem to work well because in those cases, same query plan is used for both queries. If you want to avoid unpredictable results, you need to tell MySQL that the order of ids matters to you. Otherwise, the MySQL optimizer will pick the query plan it thinks is fastest, regardless of what previous queries you may have run.
[6 Feb 2015 13:02]
Michał Grabowski
Thus the optimizer behavior in that case in 5.6.22 is considered temporary ? 5.7 will "fix" this ?
[6 Feb 2015 14:22]
Øystein Grøvlen
Any query optimizer behavior is temporary. As your data evolve, what is the most optimal way to execute a query may change; even in the same version. Also, in new major versions, new optimizations are added and the cost model adjusted so that query plans may change. If your query depends on a specific ordering of the rows in the result set, you need to specify this ordering.
[6 Feb 2015 14:48]
Michał Grabowski
Thank you for the explanation.