[MDEV-24961] Multi-table UPDATE does not consider ORDER BY clause Created: 2021-02-24 Updated: 2023-11-28 |
|
| Status: | Stalled |
| Project: | MariaDB Server |
| Component/s: | Server |
| Affects Version/s: | 10.3.27, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.10, 10.11, 11.0, 11.1, 11.2 |
| Fix Version/s: | 10.4, 10.5, 10.6, 10.11 |
| Type: | Bug | Priority: | Major |
| Reporter: | Oskar Schneider | Assignee: | Rex Johnston |
| Resolution: | Unresolved | Votes: | 2 |
| Labels: | None | ||
| Environment: |
Debian |
||
| Issue Links: |
|
||||||||||||
| Description |
|
In this script, the ORDER BY AP.prio is not considered:
The script returns this output:
|
| Comments |
| Comment by Alice Sherepa [ 2021-02-24 ] | ||||||||||||||||||||||||||||||
|
Thanks for the report!
| ||||||||||||||||||||||||||||||
| Comment by Oskar Schneider [ 2021-03-16 ] | ||||||||||||||||||||||||||||||
|
The workaround unfortunately does not work reliably. Sometimes it updates the data as expected, sometimes in the wrong order. | ||||||||||||||||||||||||||||||
| Comment by David Townes [ 2023-03-19 ] | ||||||||||||||||||||||||||||||
|
Encountered this bug today in MariaDB 10.6.12, and it appears it might be related to internal use of temporary tables. In all the instances I have tested where "Using temporary" shows up in the query explanation and ordering is not only requested by also required on any table other than the first in a join sequence (the above example outputs correctly if the values in AP.prio are instead ascending and sequential), the ORDER BY clause seems to result in updates being applied to randomly sorted rows on subsequent tables. See The workaround above likely works sometimes but not others because internal temporary table use depends on a wide variety of factors, and convincing the server to avoid use of internal temporary tables is highly context dependent. In my testing, moving the table referenced by columns in the ORDER BY clause to first position and then swapping STRAIGHT_JOIN for INNER JOIN stopped the server from using internal temporary tables for larger queries and cause the update to perform correctly, but the same tactic does not appear to help with the simple text example above, and while the workaround above does work with the simple text example it does not with larger queries. For the queries in the original report, compare:
| ||||||||||||||||||||||||||||||
| Comment by Rex Johnston [ 2023-08-01 ] | ||||||||||||||||||||||||||||||
|
A slightly clearer test.
Just tested on 11.2, which fails in a slightly different way to 10.6. In testing, the temporary table created for iterating over (representing the equivalent select), has "rowid" pushed onto the front of the order by list, but the fix requires more than just correcting this. |