Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimize group by sub query in Mysql

It's quite a straight-forward query.

If I run the queries separately it is not that slow but when I combine them its very slow.

I'm not sure how to optimise it. I'm basically only wanting to show multiple refunds. So where faultid exists more than once.

SELECT 
    r.* 
FROM 
    faultrefunds_v2 r
WHERE 
    r.id IN (SELECT r1.id 
             FROM faultrefunds_v2 r1 
             GROUP BY faultid
             HAVING count(r1.faultid) > 1);

The results from explain are have been attached as an image

enter image description here

like image 934
Robbo_UK Avatar asked Apr 01 '12 15:04

Robbo_UK


People also ask

How do I make a GROUP BY query faster?

GROUP BY performs better if you keep the number of grouping columns small. Avoid grouping redundant columns by using set functions. When you're grouping joined tables, reduce before you expand. You can make a join happen late by replacing it with a set operator.

Does subquery reduce performance?

A Sub-Query Does Not Hurt Performance.

Does GROUP BY slow down a query?

Conclusion. GROUP BY is a powerful statement, but it tends to slow down queries.


2 Answers

IN clause the way you used it would be very slow, use JOIN instead:

SELECT r.* FROM ( 
  SELECT r1.id AS id
  FROM faultrefunds_v2 r1 
  GROUP BY faultid
  HAVING count(r1.faultid) > 1
) AS ids
LEFT JOIN faultrefunds_v2 AS r
ON( ids.id = r.id )
like image 170
nobody Avatar answered Sep 17 '22 21:09

nobody


I guess, this qualifies rather as a re-writing than as an optimisation, but this is what I would try instead, anyway:

SELECT 
  r.* 
FROM faultrefunds_v2 r
WHERE EXISTS (
  SELECT *
  FROM faultrefunds_v2 r1 
  WHERE r1.faultid = r.faultid
    AND r1.id <> r.id
);
like image 40
Andriy M Avatar answered Sep 17 '22 21:09

Andriy M