[Solved] Correctness of the 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. Avoid Correlated Subqueries (query line: 12): A correlated subquery is a subquery that contains a reference (column: BookingId) 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 (query line: 35): A correlated subquery is a subquery that contains a reference (column: BookingId) 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.
  3. Avoid Subselect When Selecting MAX/MIN Per Group (query line: 19): 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 `TC33_AuditTrial` ADD INDEX `tc33_audittrial_idx_auditid_transactio_bookingid` (`AuditId`,`TransactionType`,`BookingId`);
ALTER TABLE `TC33_AuditTrial` ADD INDEX `tc33_audittrial_idx_transactiontype_bookingid` (`TransactionType`,`BookingId`);
The optimized query:
SELECT
        a.BookingId AS BookingId,
        CAST(b.TransactionDateTime AS DATE) AS TransactionDate 
    FROM
        TC33_AuditTrial A 
    JOIN
        TC33_AuditTrial b 
            ON a.AuditId = b.AuditId 
    WHERE
        a.TransactionType = 'S' 
        AND CAST(a.TransactionDateTime AS DATE) = (
            SELECT
                CAST(TC33_AuditTrial2.TransactionDateTime AS DATE) 
            FROM
                TC33_AuditTrial AS TC33_AuditTrial2 
            LEFT JOIN
                TC33_AuditTrial AS TC33_AuditTrial3 
                    ON (
                        a.BookingId = TC33_AuditTrial3.BookingId 
                        AND TC33_AuditTrial3.TransactionType = 'R'
                    ) 
                    AND (
                        TC33_AuditTrial2.AuditId < TC33_AuditTrial3.AuditId
                    ) 
            WHERE
                (
                    TC33_AuditTrial2.BookingId = a.BookingId 
                    AND 1 = 1
                ) 
                AND (
                    TC33_AuditTrial3.AuditId IS NULL
                )
        ) 
        AND a.TransactionValue = (
            SELECT
                SUM(b.TransactionValue) 
            FROM
                TC33_AuditTrial b 
            WHERE
                a.BookingId = b.BookingId 
                AND b.TransactionType = 'R'
        )

Related Articles



* original question posted on StackOverflow here.