I recently came across in my inherited program a long SQL query that joined 8 tables and 3 views. Using EXPLAIN
, I saw it was 7 Unique Key lookups and 4 non-unique key lookups. On average it took 18 seconds to fetch 350 rows (there's a number of reasons for that, each of those 3 views is made up of other views for one) Then, I noticed a GROUP BY tableone.id
without any aggregate. I deleted that and now it runs in milliseconds.
Now the confusing part is that I then looked up why MySQL allows a GROUP BY
statement without an aggregate function and learned it's actually an optimizing technique (Why does MySQL allow "group by" queries WITHOUT aggregate functions?).
This was obviously NOT the case in my situation. So why is that? When is a dangling GROUP BY
a hindrance and not an optimizer?