[Solved] How do I go about optimizing 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 Calling Functions With Indexed Columns (query line: 21): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `first_name_txt` is indexed, the index won’t be used as it’s wrapped with the function `lower`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 22): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `last_name_txt` is indexed, the index won’t be used as it’s wrapped with the function `lower`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid Calling Functions With Indexed Columns (query line: 23): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `birth_dt` is indexed, the index won’t be used as it’s wrapped with the function `to_char`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  4. 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.
  5. 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.
  6. Use UNION ALL instead of UNION (query line: 30): 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.
Optimal indexes for this query:
CREATE INDEX credit_request_idx_evaluator_id_request_id ON credit_request (evaluator_id,request_id);
CREATE INDEX evaluator_idx_evaluator_id ON evaluator (evaluator_id);
CREATE INDEX requestor_idx_request_id_last_txt ON requestor (request_id,last_name_txt);
CREATE INDEX requestor_idx_soc_txt_request_id ON requestor (soc_sec_num_txt,request_id);
The optimized query:
SELECT
        cr_client_app_id,
        cr_personal_flg,
        r_requestor_type_id 
    FROM
        ((SELECT
            cr.client_app_id AS cr_client_app_id,
            cr.personal_flg AS cr_personal_flg,
            r.requestor_type_id AS r_requestor_type_id 
        FROM
            credit_request cr,
            requestor r,
            evaluator e 
        WHERE
            cr.evaluator_id = 96 
            AND cr.request_id = r.request_id 
            AND cr.evaluator_id = e.evaluator_id 
            AND cr.request_id != 143462 
            AND (
                (
                    lower(r.first_name_txt) = 'test' 
                    AND lower(r.last_name_txt) = 'newprogram' 
                    AND to_char(r.birth_dt, 'MM/DD/YYYY') = '01/02/1960' 
                    AND r.last_name_txt IS NOT NULL 
                    AND r.first_name_txt IS NOT NULL 
                    AND r.birth_dt IS NOT NULL
                )
            )) 
    UNION
    DISTINCT (SELECT
        cr.client_app_id AS cr_client_app_id,
        cr.personal_flg AS cr_personal_flg,
        r.requestor_type_id AS r_requestor_type_id 
    FROM
        credit_request cr,
        requestor r,
        evaluator e 
    WHERE
        cr.evaluator_id = 96 
        AND cr.request_id = r.request_id 
        AND cr.evaluator_id = e.evaluator_id 
        AND cr.request_id != 143462 
        AND ((r.soc_sec_num_txt = 'xxxxxxxxx' 
        AND r.soc_sec_num_txt IS NOT NULL)))
) AS union1

Related Articles



* original question posted on StackOverflow here.