First, do not use either of your two queries. Both have a group by some column (GROUP BY group_id
) and then select other columns, non-aggregated (SELECT id, name
). This may give you wrong and unexpected results, despite that it may work in your tests, with some small sized table.
Second, the UNION ALL
is not a problem. If the two subqueries perform efficiently, then the final union is ok, too. If you need a sort, the efficiency will depend on how that sort differs from the indexes used.
Now, the problem of "groupwise-max" or "greatest-n-per-group""groupwise-max" or "greatest-n-per-group" has many solutions (and even a tag, both at SO and here). There are two sub-problems, depending on whether ties can happen and what the wanted results are in those cases.
If you want all the tied rows, the solution with GROUP BY
inside a derived table is usually good. In your case, that you want just one row returned per group, another approach is easier to write and usually performs very well when there is a small number of group overall:
SELECT id, name, price
FROM items
WHERE group_id IS NULL
UNION ALL
SELECT i.id, i.name, i.price
FROM
( SELECT DISTINCT group_id
FROM items
WHERE group_id IS NOT NULL
) AS di
JOIN
items AS i
ON i.id =
( SELECT id
FROM items
WHERE group_id = di.group_id
ORDER BY price, id -- order for resolving ties
LIMIT 1
)
ORDER BY
<some_columns> ; -- final order
An index on (group_id, price, id)
will be helpful