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
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.
A Sub-Query Does Not Hurt Performance.
Conclusion. GROUP BY is a powerful statement, but it tends to slow down queries.
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 )
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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With