Your problem is the combination of GROUP BY
with ORDER BY
. There is no single index that would cover both!
To make it work without sorting, you'd need one index that satisfies the order required for GROUP BY
(p.id
) as as well as the order required for the ORDER BY
(p.added_utc_date ASC, p.added_utc_time ASC, p.hash ASC
). These two order requirements don't share a common prefix, hence you cannot have a single index that supports both.
If, however, you could do with ORDER BY p.id, p.added_utc_date, ...
then you could create an index for that and it should work for both (still I don't know if MySQL is smart enough to do it!).
To make a long story short: the execution plan you are thinking of is impossible for your query.
References: