[Solved] Oracle SQL Newbie needs help finding similar records

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: 15): 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 `CLM_SERV_STRT` is indexed, the index won’t be used as it’s wrapped with the function `TRUNC`. 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: 16): 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 `CLM_SERV_STRT` is indexed, the index won’t be used as it’s wrapped with the function `TRUNC`. 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: 17): 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 `CLM_COST_CTR_NBR` is indexed, the index won’t be used as it’s wrapped with the function `TRIM`. 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.
The optimized query:
SELECT
        ItemA.CLM_SSN,
        ItemA.CLM_SERV_STRT Service_Date,
        ItemA.CLM_COST_CTR_NBR,
        ItemA.CLM_RECV_AMT,
        ItemB.CLM_COST_CTR_NBR RES_Cost_Center,
        ItemB.CLM_RECV_AMT,
        CST_SERV.GroupCode,
        Service 
    FROM
        DDIS.PTS_MV_CLM_STAT ItemA,
        DDIS.PTS_MV_CLM_STAT ItemB,
        DDIS.CST_SERV 
    WHERE
        TRUNC(ItemA.CLM_SERV_STRT) BETWEEN to_date('01-07-2013', 'dd-mm-yyyy') AND to_date('31-    07-2013', 'dd-mm-yyyy') 
        AND TRUNC(ItemA.CLM_SERV_STRT) = TRUNC(ItemB.CLM_SERV_STRT) 
        AND TRIM(ItemA.CLM_COST_CTR_NBR) = '5P311' 
        AND ITEMB.FK_SERV = CST_SERV.PKSERVICE 
        AND CST_SERV.GroupCode = 'RES' 
        AND Itema.CLM_SSN = ItemB.CLM_SSN 
        AND ItemA.CLM_RECV_AMT <> 0 
        AND ItemB.CLM_RECV_AMT <> 0 
    ORDER BY
        ItemA.CLM_SSN,
        ItemA.CLM_SERV_STRT

Related Articles



* original question posted on StackOverflow here.