[Solved] Converting a correlated sub-query to JOIN

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 OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  2. Avoid Subqueries (query line: 15): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Avoid Subqueries (query line: 52): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  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'.
  5. Use UNION ALL instead of UNION (query line: 44): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
The optimized query:
SELECT
        s2_area_code,
        s_due_date,
        rate 
    FROM
        ((SELECT
            s2.Area_CODE AS s2_area_code,
            s.due_DATE AS s_due_date,
            RATE AS rate,
            s.EFF_DATE AS s_eff_date 
        FROM
            SCHED S 
        LEFT JOIN
            (
                SELECT
                    NVL(MIN(s1.due_DATE),
                    to_date('31-DEC-2999',
                    'DD-MM-YYYY')) AS min_date,
                    s1.AREA_CODE AS a_code 
                FROM
                    SCHED s1 
                WHERE
                    s1.COMPANY_CODE = 'c' 
                GROUP BY
                    s1.AREA_code 
                ORDER BY
                    NULL
            ) s2 
                ON s2.A_CODE = s.area_code 
        WHERE
            (
                s.area_code = 11001 
                AND (
                    (
                        s.COMP_CODE = 'b' 
                        AND s.due_DATE < s2.min_date
                    )
                )
            ) 
        ORDER BY
            s_eff_date ASC,
            s.due_DATE ASC) 
        UNION
        DISTINCT (SELECT
            s2.Area_CODE AS s2_area_code,
            s.due_DATE AS s_due_date,
            RATE AS rate,
            s.EFF_DATE AS s_eff_date 
        FROM
            SCHED S 
        LEFT JOIN
            (SELECT
                NVL(MIN(s1.due_DATE),
                to_date('31-DEC-2999',
                'DD-MM-YYYY')) AS min_date,
                s1.AREA_CODE AS a_code 
            FROM
                SCHED s1 
            WHERE
                s1.COMPANY_CODE = 'c' 
            GROUP BY
                s1.AREA_code 
            ORDER BY
                NULL) s2 
                ON s2.A_CODE = s.area_code 
        WHERE
            (s.area_code = 11001 
            AND (s.COMP_CODE = 'a')) 
        ORDER BY
            s_eff_date ASC,
            s.due_DATE ASC)
    ) AS union1 
ORDER BY
    union1.s_eff_date ASC,
    union1.s_due_date ASC

Related Articles



* original question posted on StackOverflow here.