[Solved] How to optimize this SQL query summing amounts across related tables?

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. Avoid Correlated Subqueries (query line: 18): A correlated subquery is a subquery that contains a reference (column: v_id) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
  2. Avoid Correlated Subqueries In Select Clause (modified query below): The aggregation function located in a subquery inside the SELECT clause, is executed once for every matched row. Extracting this subquery to a temporary table will improve performance significantly.
  3. 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.
  4. 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 `es_temp1` ADD INDEX `es_temp1_idx_voucher` (`voucher`);
ALTER TABLE `es_temp2` ADD INDEX `es_temp2_idx_voucher` (`voucher`);
ALTER TABLE `es_temp3` ADD INDEX `es_temp3_idx_v_id` (`v_id`);
ALTER TABLE `es_temp4` ADD INDEX `es_temp4_idx_voucher` (`voucher`);
ALTER TABLE `es_temp5` ADD INDEX `es_temp5_idx_voucher` (`voucher`);
ALTER TABLE `es_temp6` ADD INDEX `es_temp6_idx_voucher` (`voucher`);
ALTER TABLE `es_temp7` ADD INDEX `es_temp7_idx_voucher` (`voucher`);
ALTER TABLE `es_temp8` ADD INDEX `es_temp8_idx_voucher` (`voucher`);
ALTER TABLE `es_temp9` ADD INDEX `es_temp9_idx_voucher` (`voucher`);
ALTER TABLE `rec_electronic_edits_test` ADD INDEX `rec_edits_idx_voucher` (`voucher`);
ALTER TABLE `rec_electronic_files_test` ADD INDEX `rec_files_idx_voucher` (`voucher`);
ALTER TABLE `rec_vouchers` ADD INDEX `rec_vouchers_idx_v_id_voucher` (`v_id`,`voucher`);
The optimized query:
SELECT
        a.v_id,
        a.voucher,
        es_temp1.GL,
        es_temp2.DB,
        es_temp3.GLcount,
        es_temp4.DBcount,
        es_temp5.ED,
        es_temp6.EFT,
        es_temp7.CHK,
        es_temp8.LBX,
        es_temp9.LCDL,
        ((SELECT
            sum(i.payment_amount) 
        FROM
            rec_electronic_files_test i 
        WHERE
            a.v_id = i.voucher) + (SELECT
            sum(j.amount) 
        FROM
            rec_electronic_edits_test j 
        WHERE
            a.v_id = j.voucher)) AS Elec 
    FROM
        rec_vouchers a 
    LEFT JOIN
        es_temp1 
            ON es_temp1.voucher = a.v_id 
    LEFT JOIN
        es_temp2 
            ON es_temp2.voucher = a.v_id 
    LEFT JOIN
        es_temp3 
            ON a.v_id = es_temp3.v_id 
    LEFT JOIN
        es_temp4 
            ON a.v_id = es_temp4.voucher 
    LEFT JOIN
        es_temp5 
            ON a.v_id = es_temp5.voucher 
    LEFT JOIN
        es_temp6 
            ON a.v_id = es_temp6.voucher 
    LEFT JOIN
        es_temp7 
            ON a.v_id = es_temp7.voucher 
    LEFT JOIN
        es_temp8 
            ON a.v_id = es_temp8.voucher 
    LEFT JOIN
        es_temp9 
            ON a.v_id = es_temp9.voucher 
    GROUP BY
        a.v_id,
        a.voucher 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.