1

I'm investigating the difference in the results given by the same SQL query on two servers running slightly different versions of MariaDB (10.5 and 10.11).

Test query is:

SELECT a.Name 
FROM Artist a JOIN Album a2 ON a.ArtistId = a2.ArtistId 
GROUP BY a.ArtistId, a.Name 
HAVING COUNT(a2.AlbumId) IN (
 SELECT * FROM(
  SELECT COUNT(a2.AlbumId)
  FROM Artist a JOIN Album a2 ON a.ArtistId = a2.ArtistId 
  GROUP BY a.ArtistId
  ORDER BY COUNT(a2.AlbumId) DESC
  LIMIT 4) AS t
 )
ORDER BY a.Name;

against the publicly available Chinook database

Subquery returns (21,14,11,10).

Replacing the subquery by these results:

SELECT ar.Name 
FROM Artist ar
JOIN Album al ON ar.ArtistId = al.ArtistId 
GROUP BY ar.ArtistId, ar.Name 
HAVING COUNT(al.AlbumId) IN (21,14,11,10)
ORDER BY ar.Name;

gives:

Name        |
------------+
Deep Purple |
Iron Maiden |
Led Zeppelin|
Metallica   |
U2          |

which is the expected result.

Now, the problem is that when running the first query on MariaDB version 10.11.4 it gives no results (0 rows), but when running it in MariaDB version 10.5.21 it gives the 5 expected rows.

Note that the query in itself is not important. What I'm investigating here is the inconsistency in results between two servers that have similar software versions.

Also note that the following slightly modified query works (5 rows) on both versions:

SELECT ar.Name, COUNT(al.AlbumId) as num_albums
FROM Artist ar
JOIN Album al ON ar.ArtistId = al.ArtistId 
GROUP BY ar.ArtistId, ar.Name 
HAVING num_albums IN (
  SELECT * FROM (
    SELECT COUNT(al2.AlbumId)
    FROM Album al2 
    GROUP BY al2.ArtistId
    ORDER BY COUNT(al2.AlbumId) DESC
    LIMIT 4
  ) AS t
)
ORDER BY ar.Name;

Is there a recent change in MariaDB that explains the first query results? Or is this a bug in MariaDB that should be reported?

EDITED: results are back on server version 11.2. If it was a bug, seems it's solved now.

The affected version is the current default on Debian 12 bookworm, so if you encounter a similar problem, updating from the official repository may be a good solution.

4
  • Are you sure that ORDER BY COUNT(a2.AlbumId) DESC provides deterministic sorting? Does COUNT(a2.AlbumId) is really unique, and there are no 2 separate rows with the same count? If not then the sorting is not deterministic, and the output provided by LIMIT is unpredictable.
    – Akina
    Commented Nov 22, 2023 at 11:34
  • Yes, the results are 21,14,11,10,10, 6, 5, 4... The two tens can get swapped, but this doesn't change the result. Anyway, even if sorting by rand(), the query should return at least 4 rows, because the counting in the main query and the subquery is the same.
    – joanq
    Commented Nov 22, 2023 at 13:15
  • 1
    Did you check that the subquery also returned (21,14,11,10) on 10.11.4? Please provide EXPLAIN SELECT ... for both servers.
    – Rick James
    Commented Nov 22, 2023 at 19:58
  • on this source, I've tested 10.4.32 (latest) which returned no result. I've checked multiple 10.5 version include 10.5.19+ (every increment) and 10.5.15 and all latest release of 10.6, 10.10, 10.11, 11.0, 11.1, 11.2, 11.3-rc and all return the 5 row result.
    – danblack
    Commented Nov 28, 2023 at 6:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.