[Solved] Better optimized SELECT SQL query for 50,000+ records

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: 27): A correlated subquery is a subquery that contains a reference (column: account_num) 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 Subselect When Selecting MAX/MIN Per Group (query line: 16): Constant subquery results are usually not cached by the database, especially in non-recent database versions. Therefore, a constant subquery in a WHERE clause will be fully evaluated for every row the WHERE clause will examine, which can significantly impact query performance. Use the method mentioned in the example instead.
  3. Avoid Subselect When Selecting MAX/MIN Per Group (query line: 25): Constant subquery results are usually not cached by the database, especially in non-recent database versions. Therefore, a constant subquery in a WHERE clause will be fully evaluated for every row the WHERE clause will examine, which can significantly impact query performance. Use the method mentioned in the example instead.
  4. 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.
Optimal indexes for this query:
ALTER TABLE `bill` ADD INDEX `bill_idx_account_num_effective_date` (`account_num`,`effective_date`);
The optimized query:
SELECT
        bill1.account_num,
        bill1.effective_date AS ed1,
        bill1.amount AS am1,
        bill3.effective_date AS ed2,
        bill3.amount AS am2 
    FROM
        bill AS bill1 
    LEFT JOIN
        bill AS bill3 
            ON (
                bill1.account_num = bill3.account_num
            ) 
    LEFT JOIN
        bill AS bill2 
            ON (
                bill2.account_num = bill1.account_num
            ) 
            AND (
                bill1.effective_date < bill2.effective_date
            ) 
    LEFT JOIN
        bill AS bill4 
            ON (
                bill4.account_num = b1.account_num 
                AND bill4.effective_date < (
                    SELECT
                        max(bill.effective_date) 
                FROM
                    bill 
                WHERE
                    bill.account_num = b1.account_num
            )
        ) 
        AND (
            bill3.effective_date < bill4.effective_date
        ) 
    WHERE
        (
            (
                1 = 1 
                AND (
                    1 = 1 
                    OR bill3.effective_date IS NULL
                )
            ) 
            AND (
                bill2.effective_date IS NULL
            )
        ) 
        AND (
            bill4.effective_date IS NULL
        ) 
    ORDER BY
        bill1.effective_date DESC

Related Articles



* original question posted on StackOverflow here.