[MDEV-3926] Wrong result with GROUP BY ... WITH ROLLUP Created: 2012-12-10 Updated: 2021-05-19 Resolved: 2013-03-21 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 5.5.28a |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major |
| Reporter: | P Ka | Assignee: | Unassigned |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Windows server 2003, 64-bit |
||
| Issue Links: |
|
||||||||||||||||
| Description |
|
The following query returns different result between MYSQL and MARIADB (5.5.28):
MariaDB seems to ignore ORDER BY statement after subquery having GROUP BY ... WITH ROLLUP. Optimizer_switch settings as per default. |
| Comments |
| Comment by Sergei Golubchik [ 2012-12-10 ] |
|
MariaDB isn't wrong here. A "table" (and subquery in the FROM clause too) is - according to the SQL standard - an unordered set of rows. Rows in a table (or in a subquery in the FROM clause) do not come in any specific order. That's why the optimizer can ignore the ORDER BY clause that you have specified. In fact, SQL standard does not even allow the ORDER BY clause to appear in this subquery (we allow it, because ORDER BY ... LIMIT ... changes the result, the set of rows, not only their order). You need to treat the subquery in the FROM clause, as a set of rows in some unspecified and undefined order, and put the ORDER BY on the top-level SELECT. |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-21 ] |
|
I'm concerned by this behavior too. And i think it's a real problem because of MySQL is working good with this kind of subqueries with a special order. SELECT * => MySQL 5.1.62 does the right order, and returns the first occurence needed : the latest in time Why MySQL is working, and not MariaDB on this ? |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-21 ] |
|
I see that you already have closed this bug, do i need to create a new bug or can you please reopen this one ? |
| Comment by Sergei Golubchik [ 2013-03-21 ] |
|
Please, read my comment above. This MariaDB's behavior is not a bug. In your query you rely on the specific implementation glitch in MySQL 5.1. MariaDB has a better subquery optimizer and it ignores ORDER BY in the subquery in the FROM clause. MySQL 5.6 has got an improved optimizer too, so I suppose, your query won't work in MySQL 5.6 either. To have stable result on all MySQL/MariaDB versions (and other databases) you need to rewrite the query to conform to the SQL Standard semantics. |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-21 ] |
|
Thank you for your quick precision. |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-21 ] |
|
Fortunately, we are using this kind of subqueries for more than 5 years, and there has been always "deterministic" (not tried on MySQL 5.6). I'm not saying we are right and you are wrong, i'm just very surprised about what i'm reading on this page, because we are using this (bad standard SQL) successfully for a very long time without any drawbacks. And now we are testing MariaDB for few months, and this is the first difference of behavior. According to you words, we had a lot of chance for years : fine, we have been lucky, but it means we need to change a lot of queries to adapt our softwares to the future. Good for you it's not a bug, but then it means we are a lot in the world with bugs to fix now. Thank you for reading, ================= SELECT * FROM tableX ORDER BY timeX DESC SELECT * FROM ( SELECT * FROM tableX ORDER BY timeX DESC ) x GROUP BY keyX |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-21 ] |
|
I think this thread on MySQL illustrates our discussion : http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html And the last post confirms what you explained : "that can break in any future update". |
| Comment by Elena Stepanova [ 2013-03-21 ] |
|
It's not hypothetical, things do change, and in MySQL too, sometimes you don't even need a major upgrade for that. Here is just one fairly recent example for you: http://bugs.mysql.com/bug.php?id=67846 |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-22 ] |
|
Yes and the last post on your link says it's beginning from the 5.1.66 of MySQL. Thank you again for all, |
| Comment by Yann-Gaël GAUTHERON [ 2013-03-22 ] |
|
We tried a standard workaround on a concrete example : Method A = SELECT SQL_NO_CACHE * FROM ( SELECT * FROM t ORDER BY d DESC ) x GROUP BY id; Method A on Production machine, MySQL 5.1.62, Linux Gentoo, 400 rows, 0.0068 sec, NOT SQL Standard Times are the average of 5 trials with SQL_NO_CACHE enabled. 1. Is the method B the STANDARD SQL method to do the same ? Thank you for your help ! For information, here are the respective explains : Method B on Production machine, MySQL 5.1.62, Linux Gentoo, 400 rows, 0.0675 sec : Method A on a beta machine, MariaDB 5.5.28, Linux Gentoo, 400 rows, 0.0043 sec, NOT SQL Standard, ORDER BY ignored, NOT the result expected : Method B on a beta machine, MariaDB 5.5.28, Linux Gentoo, 400 rows, 0.0043 sec : |
| Comment by Sergei Golubchik [ 2013-03-22 ] |
|
google for "groupwise max" - you will see lots of different solutions to this problem. you can also ask your question on irc, freenode, #maria channel. Or email your question to maria-discuss@lists.launchpad.net |