[Solved] MySQL - Slow query

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `cl_client_intervention_areas` ADD INDEX `cl_intervention_idx_city_id` (`city_id`);
ALTER TABLE `cl_clients` ADD INDEX `cl_clients_idx_vat` (`vat`);
ALTER TABLE `cl_clients_with_regions` ADD INDEX `cl_with_idx_section_nut_2_vat_encrypt` (`section`,`nut_2`,`vat`,`encrypted_vat`);
ALTER TABLE `cl_finances` ADD INDEX `cl_finances_idx_year_vat` (`year`,`vat`);
The optimized query:
SELECT
        cl_clients.vat AS association_vat,
        cl_clients.name AS association_name,
        cl_clients_with_regions.city_id,
        cl_clients_with_regions.nut_1,
        cl_clients_with_regions.nut_2,
        cl_clients_with_regions.nut_3,
        company.vat AS company_vat,
        company.name,
        company.section,
        company.division,
        company.cae,
        SUM((SELECT
            SUM((COALESCE((cl_finances.sales_community_market),
            0) + COALESCE((cl_finances.sales_extra_market),
            0))) 
        FROM
            cl_finances 
        WHERE
            cl_finances.vat = company.vat 
            AND cl_finances.year = 2018)) AS total_sum 
    FROM
        `cl_clients_with_regions` 
    JOIN
        `cl_client_intervention_areas` 
            ON `cl_client_intervention_areas`.`city_id` = `cl_clients_with_regions`.`city_id` 
    JOIN
        `cl_clients` 
            ON `cl_clients`.`vat` = `cl_client_intervention_areas`.`client_vat` 
    INNER JOIN
        cl_clients_with_regions company 
            ON cl_clients_with_regions.vat = company.vat 
    WHERE
        `cl_clients_with_regions`.`encrypted_vat` IS NOT NULL 
        AND `cl_clients_with_regions`.`country` IS NOT NULL 
        AND `cl_clients_with_regions`.`cae` IS NOT NULL 
        AND `cl_clients_with_regions`.`cae` != '' 
        AND `cl_clients_with_regions`.`division` IS NOT NULL 
        AND `cl_clients_with_regions`.`section` = 'A' 
        AND `cl_clients_with_regions`.`nut_2` = 'Centro' 
    GROUP BY
        association_vat 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.