[Solved] WHERE in Sql, combining two fast conditions multiplies costs many times

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: 82): A correlated subquery is a subquery that contains a reference (column: SSN_Number) 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.
The optimized query:
WITH fpcRepairs AS (SELECT
        FPC_Row = ROW_NUMBER() OVER (PARTITION 
    BY
        t.SSN_Number 
    ORDER BY
        t.Received_Date,
        t.Claim_Creation_Date,
        t.Repair_Completion_Date,
        t.Claim_Submitted_Date),
        needToChange.idData,
        t.Repair_Completion_Date,
        t.Received_Date,
        fpcRepairs.Work_Order,
        fpcRepairs.SSN_number,
        fpcRepairs.fiMaxActionCode,
        fpcRepairs.idModel,
        fpcRepairs.ModelName,
        SP = (SELECT
            TOP 1 Reused_Indicator 
        FROM
            tabDataDetail td 
        INNER JOIN
            tabSparePart sp 
                ON td.fiSparePart = sp.idSparePart 
        WHERE
            td.fiData = t.idData 
            AND (td.Material_Quantity <> 0) 
            AND (sp.SparePartName = '1254-3751')) 
    FROM
        tabData AS t 
    INNER JOIN
        modModel AS m 
            ON t.fiModel = m.idModel 
    WHERE
        (m.ModelName = 'LT26i') 
        AND EXISTS (SELECT
            NULL 
        FROM
            tabDataDetail AS td 
        INNER JOIN
            tabSparePart AS sp 
                ON td.fiSparePart = sp.idSparePart 
        WHERE
            (td.fiData = t.idData) 
            AND (td.Material_Quantity <> 0) 
            AND (sp.SparePartName = '1254-3751'))), needToChange AS (SELECT
            needToChange.idData 
        FROM
            tabData AS t 
        INNER JOIN
            modModel AS m 
                ON t.fiModel = m.idModel 
        WHERE
            (m.ModelName = 'LT26i') 
            AND EXISTS (SELECT
                NULL 
            FROM
                tabDataDetail AS td 
            INNER JOIN
                tabSparePart AS sp 
                    ON td.fiSparePart = sp.idSparePart 
            WHERE
                (td.fiData = t.idData) 
                AND (td.Material_Quantity <> 0) 
                AND (sp.SparePartName IN ('1257-2741', '1257-2742', '1248-2338', '1254-7035', '1248-2345', '1254-7042')))) SELECT
                t.idData 
            FROM
                tabData AS t 
            INNER JOIN
                modModel AS m 
                    ON t.fiModel = m.idModel 
            INNER JOIN
                needToChange 
                    ON t.idData = needToChange.idData 
            LEFT OUTER JOIN
                fpcRepairs rep 
                    ON t.idData = rep.idData 
            WHERE
                rep.idData IS NOT NULL 
                AND rep.FPC_Row > 1 
                AND (
                    SELECT
                        lastRep.SP 
                    FROM
                        fpcRepairs lastRep 
                    WHERE
                        lastRep.SSN_Number = rep.SSN_Number 
                        AND lastRep.FPC_Row = rep.FPC_Row - 1
                ) = rep.SP 
                OR rep.idData IS NOT NULL 
                AND rep.FPC_Row = 1 
                AND rep.SP = 0 
            ORDER BY
                t.idData

Related Articles



* original question posted on StackOverflow here.