[MDEV-3795] WRONG ORDER BY Created: 2012-10-04 Updated: 2013-09-10 Resolved: 2012-10-29 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 5.5.25-galera |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Critical |
| Reporter: | roberto spadim | Assignee: | Unassigned |
| Resolution: | Not a Bug | Votes: | 1 |
| Labels: | None | ||
| Environment: |
linux |
||
| Issue Links: |
|
||||||||
| Description |
|
hi, i'm running two queries but they return diferent order by results when they should be the same i will attach files to create tables |
| Comments |
| Comment by roberto spadim [ 2012-10-04 ] |
|
wrong query: SELECT tmp_tbl22.* FROM ( FROM pessoa_juridica_mov AS a WHERE id=1 AND spavendas_vendedor_id=0 ORDER BY data_ultima_venda right query: SELECT tmp_tbl22.* FROM ( FROM pessoa_juridica_mov AS a WHERE id=1 AND spavendas_vendedor_id=0 ) AS tmp_tbl22 |
| Comment by roberto spadim [ 2012-10-04 ] |
|
wrong result (HTML): right result (HTML): |
| Comment by roberto spadim [ 2012-10-04 ] |
|
files attached but it execute nice if order by is outside of |
| Comment by Elena Stepanova [ 2012-10-29 ] |
|
Hi Roberto, Please point at any MariaDB/MySQL documentation that suggests that the queries should provide identical results. |
| Comment by roberto spadim [ 2012-10-29 ] |
|
well 0000-00-00 00:00:00 is lower than 2010-08-30 14:36:05, right? check that the first query, is: in others words, the diference is the ORDER BY at the end of select, or inside the tmp_table check that the explain values are different, for the first (the right one, with order by outside tmp_table) mariadb is show in extra: "Using index condition; Using where; Using filesort" the wrong isn't well ordered since 0000-00-00 00:00:00 is the lowest possible datetime value (i don't have NULL values), it (0000-00-00 00:00:00) should be before any datetime value, and after it the others datetime values should continue ascending like this (from right result): and not like this (from wrong result): understood the wrong result of order by? i didn't found anything in manual that could explain this, the only situation that i can understand that a order by should reproduce diferent results is the random read of tmp_table instead the ordered result |
| Comment by Dmitriy Pavlichenko [ 2012-10-29 ] |
|
Hello! I got same situtaion. Our service got a bug after replacing MySQL with MariaDB. SELECT t.* FROM ( ... ) t LIMIT 0, 10 to extract a certain page of data. MariaDB is positioned like 100% replacement for MySQL. |
| Comment by roberto spadim [ 2012-10-29 ] |
|
in my opnion, it's not a bug, but a miss of documentation |
| Comment by Dmitriy Pavlichenko [ 2012-10-29 ] |
|
Sometimes documentation doesn't covery every particular case of functionality. This is the same situation as with browsers: different browsers implement a common standard, but web-programmer usually have to consider which browser is used by current user to show him more or less same interface |
| Comment by Elena Stepanova [ 2012-10-29 ] |
|
Hi Dmitriy, MariaDB must return the same result when the result is deterministic (and correct), while your query is non-deterministic by nature. The fact that it works in MySQL just like you want it to is your pure luck which will wear off sooner or later, and in your terms MySQL will stop being a drop-in replacement to itself. Requiring MariaDB to return the exact same result in this case is the same as claiming that it should always return the same order of records as MySQL for queries without order by, like select * from t1. |
| Comment by roberto spadim [ 2012-10-29 ] |
|
from mysql manual: http://dev.mysql.com/doc/refman/5.6/en/select.html If ORDER BY occurs within a subquery and also is applied in the outer query, the outermost ORDER BY takes precedence. For example, results for the following statement are sorted in descending order, not ascending order: (SELECT ... ORDER BY a) ORDER BY a DESC; ok the outermost order by works, but the manual don't explain what happen without the outermost order by, i think it should work with the inner most don't? if you try they are the same explain and the same data, there's no order by... |
| Comment by roberto spadim [ 2012-10-29 ] |
|
the point elena is: select * from ( a table where the data is explicity ordered) should return and not check that we are talking about: the first one should be the same as but it's not, why? that's a miss documented thing that i think could be a otimization of how mariadb 'read' results from the (select * from t) part i don't know if mysql is wrong or right, but it's more human readable to undestant that ok theres a work around rewriting the query to but users must rewrite queries... (that's the problem) |
| Comment by Dmitriy Pavlichenko [ 2012-10-29 ] |
|
I agree, that If you look from the point of view of the pure SQL, But can someone tell me what to do in following cases? 1. I got an arbitrary SQL-query. I need to extract a given range (LIMIT x, dx) of data from it. 2. Let we have a query like this: I know then while grouping MySQL uses first met value for those columns that are not grouped. Thanks |
| Comment by roberto spadim [ 2012-10-29 ] |
|
yes, that's the point but i seed this feature as a easy way to execute html page $SQL="SELECT * FROM ( $USER_SQL ) LIMIT $page_start,30"; that's why is more human readable to see the subquery materialization, like: a work around to my sql example could be |
| Comment by Elena Stepanova [ 2012-10-29 ] |
|
>> I know then while grouping MySQL uses first met value for those columns that are not grouped. No, it doesn't. Again, you rely on pure luck; and now, this is explicitly documented in MySQL manual: MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. <..> You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. http://dev.mysql.com/doc/refman/5.6/en/group-by-hidden-columns.html |
| Comment by Dmitriy Pavlichenko [ 2012-10-29 ] |
|
Elena. First of all thank you, because I really didn't know that. Roberto suggests this way: create temporary table tmp select * from t order by c The last SELECT really gives an ordered set, but now I'm not sure - maybe I can't rely on it too? |
| Comment by roberto spadim [ 2012-10-29 ] |
|
no, it's not guaranteed that select will return in the phisical order, who can tell this is the table engine |
| Comment by roberto spadim [ 2012-10-29 ] |
|
may be the point here should be a option to user to tune optimizer to materialize the $USER_SQL and execute it in the 'right' orders instead of execute it in last SELECT order (no order) |
| Comment by Elena Stepanova [ 2012-10-29 ] |
|
Hi Dmitriy, >> Is it guaranteed that SELECT will return rows from a table in their phisical order? In general, you cannot rely on it, either. I think it's better to make an effort and convert the logic so it's truly deterministic and you don't have to worry about it again. |
| Comment by Dmitriy Pavlichenko [ 2012-10-29 ] |
|
Ok, I see, thanks... But now i've got a really hard work to do And I don't see a way to do minimum modifications without syntactic analysis, |
| Comment by roberto spadim [ 2012-10-30 ] |
|
from other comment... in other words, doesn't matter the "order by b", since you put a 'explicit' group by without order, in this case a 'implicit' order by is assumed like the group by order but for the others cases where we don't have group by or order by outside the select... well the inside order by should work... |
| Comment by roberto spadim [ 2012-10-30 ] |
|
for now... |
| Comment by Dmitriy Pavlichenko [ 2012-10-30 ] |
|
Iteresting that this query work exactly as expected: SELECT * FROM Obviously, because GROUP BY is not "optimized" by optimizer |
| Comment by roberto spadim [ 2012-10-30 ] |
|
i know it´s not right place, but could you test it with so we could check if the order by is know by the LIMIT 0,10 query |
| Comment by Dmitriy Pavlichenko [ 2012-10-30 ] |
|
Works fine. But in fact there's no need for addional ORDER BY with GROUP BY in this case. From docs (http://dev.mysql.com/doc/refman/5.6/en/select.html): If you use GROUP BY, output rows are sorted according to the GROUP BY columns as if you had an ORDER BY for the same columns. To avoid the overhead of sorting that GROUP BY produces, add ORDER BY NULL: SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL; MySQL extends the GROUP BY clause so that you can also specify ASC and DESC after columns named in the clause: SELECT a, COUNT(b) FROM test_table GROUP BY a DESC; |
| Comment by roberto spadim [ 2012-10-30 ] |
|
and it returned with order by b? or by a? if it returned from by b, the parser didn't ignored the implicity order by of group by... in other words why a order by is ignored and a order by with group by isn't? |
| Comment by Dmitriy Pavlichenko [ 2012-10-30 ] |
|
SELECT * FROM Result is ordered by "b ASC". So ORDER BY is not ignored. |
| Comment by roberto spadim [ 2012-10-30 ] |
|
now try this and test if it's ordered by a,b desc or b asc |
| Comment by Dmitriy Pavlichenko [ 2012-10-30 ] |
|
It's ordered by a,b desc. |
| Comment by roberto spadim [ 2012-10-30 ] |
|
that's the point |