[MDEV-4595] GROUP BY in subqueries removed too aggressively Created: 2013-05-28 Updated: 2022-09-08 |
|
| Status: | Open |
| Project: | MariaDB Server |
| Component/s: | None |
| Affects Version/s: | 10.0.2, 5.5.31, 5.3.12 |
| Fix Version/s: | 5.5 |
| Type: | Bug | Priority: | Minor |
| Reporter: | Sergei Golubchik | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Description |
|
Since 5.3 MariaDB supports this optimization: This optimization, although correct in strict SQL standard sense, does not always work when the subquery uses non-aggregated columns in the SELECT clause. This is non-standard MariaDB extension, but as long as it is supported, the optimizer should take it into account.
As a possible fix, the optimizer could disable this optimization when non-aggregated columns are selected. It already performs all necessary checks for the ONLY_FULL_GROUP_BY sql mode. |
| Comments |
| Comment by Sergei Petrunia [ 2013-05-28 ] |
|
The manual says http://dev.mysql.com/doc/refman/5.6/en/group-by-extensions.html "The server is free to choose any value from each group". Let's remember that and look at the query and dataset from this bugreport: select a from t1 group by b a=1 is a valid result. a=2 is also a valid result. Then, lets consider a query: select count t1 has two records: for record R1, "a in (select a from t1)" can be satisfied. hence, "select count |
| Comment by Sergei Petrunia [ 2013-05-28 ] |
|
Maybe, you've meant to say that "The server is free to choose any value from each group". should be read as "The server is free to choose any value from each group. The choice must be made once and then remain fixed for the duration of the query" ? |
| Comment by Sergei Golubchik [ 2013-05-28 ] |
|
In a sense, yes. This query select a from t1 group by b can return either 1 or 2. Thus, select a from t1 where a in (select a from t1 group by b) will be one of select a from t1 where a in (1) how many times the optimizer will reevaluate the subquery is its internal implementation detail. It can do that as many times as it wants, but without breaking the semantics of the query. |