Skip to content
Advertisement

MySQL – Slow query

Sorry for the long post, but the tables involved are quite big.

When I run the query bellow it normally takes about 1m to run. However, when I remove the correlated sub-query I get the query down to 15 seconds. So I think that’s the actual problem.

Problem is, I don’t really know how to get the SUM results without the correlated query.

QUERY I’m trying to run

CL Clients create table syntax

VIEW being used in the query

CL finances create table syntax

The explain result from this query is quite big

Image with the entire EXPLAIN result

Advertisement

Answer

I believe the subquery is being executed once per row in the group by clause. Could you try the following if it makes it any better.

Change i have done is to perform the correlated query as a inline-view and then join with the main table/view — company.vat

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement