Say you have a query like
SELECT
t1.c1,
t2.c2,
... more fields from joins ...
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
... more joins ...
GROUP BY t1.field
WHERE ...
And you're having performance problems and you think you've covered all the common optimizations for this type of query, like defining indexes on the columns in your ON
, GROUP BY
, WHERE
etc.. but it's still really slow.
Check that all of your joins are to actual tables and not views!
I know this is rather stating the obvious, but it stumped be for a while because I has just forgotten that one of the joins was to a view.
This is a question that I didn't get round to asking before I found the answer myself, but I wanted to post to help anyone else with the same issue as it wasn't mentioned in any of the other posts about optimizing GROUP BY queries with JOINs. I'm not sure if this is accepted on SO, but I have also added to the accepted answer on this post, so I won't be offended if the administrators want to delete this post.