[Solved] Validating optimization of an Oracle 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: client_no) 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 Selecting Unnecessary Columns (query line: 6): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  3. Avoid Subqueries (query line: 5): 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.
The optimized query:
SELECT
        some_field,
        some_other_field 
    FROM
        (SELECT
            * 
        FROM
            some_view a 
        WHERE
            a.some_criteria 
            AND a.client_no || ':' || a.engagement_no || ':' || a.registered_date = (
                SELECT
                    b.client_no || ':' || b.engagement_no || ':' || MAX(b.registered_date) 
                FROM
                    some_view b 
                JOIN
                    some_engagement_view e 
                        ON e.client_no = b.client_no 
                        AND e.engagement_no = b.engagement_no 
                JOIN
                    some_client_view c 
                        ON c.client_no = b.client_no 
                WHERE
                    some_other_criteria 
                    AND b.client_no = a.client_no 
                    AND b.engagement_no = a.engagement_no 
                GROUP BY
                    b.client_no,
                    b.engagement_no
            )
        )

Related Articles



* original question posted on StackOverflow here.