[Solved] Explain plan shows a table not in the 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: 29): 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 `sysdate` 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.
  2. Avoid Calling Functions With Indexed Columns (query line: 30): 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 `sysdate` 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.
  3. Use Numeric Column Types For Numeric Values (query line: 15): Referencing a numeric value (e.g. 100) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  4. Use Numeric Column Types For Numeric Values (query line: 17): Referencing a numeric value (e.g. 6802) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  5. Use Numeric Column Types For Numeric Values (query line: 23): Referencing a numeric value (e.g. 1) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  6. Use Numeric Column Types For Numeric Values (query line: 31): Referencing a numeric value (e.g. 3) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
The optimized query:
SELECT
        DISTINCT kb.FSSBD,
        kb.SSSBD,
        ca.AUFNR,
        ca.PLNBEZ,
        kt.maktx,
        ca.GAMNG,
        ca.GMEIN 
    FROM
        SAPR3.CRHD cr,
        SAPR3.KBED kb,
        SAPR3.CAUFV ca,
        SAPR3.MAKT kt 
    WHERE
        cr.MANDT = '100' 
        AND cr.OBJTY = 'A' 
        AND cr.WERKS = '6802' 
        AND cr.STAND = 'MPL06' 
        AND cr.LVORM = ' ' 
        AND cr.MANDT = kb.MANDT 
        AND cr.KAPID = kb.KAPID 
        AND kb.TYPKZ IN (
            '1', '7'
        ) 
        AND kb.PLNUM = ' ' 
        AND kb.MANDT = ca.MANDT 
        AND kb.BEDID = ca.BEDID 
        AND ca.LOEKZ = ' ' 
        AND kb.FSTAD <= to_char(sysdate + 10, 'YYYYMMDD') 
        AND kb.SENDD >= to_char(sysdate - 10, 'YYYYMMDD') 
        AND kb.BSTKZ = '3' 
        AND kt.mandt = ca.mandt 
        AND kt.matnr = ca.PLNBEZ 
        AND kt.SPRAS = 'E' 
    ORDER BY
        kb.FSSBD

Related Articles



* original question posted on StackOverflow here.