[Solved] ORA-00904 - Invalid Identifier

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: 54): 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 `COL1` is indexed, the index won’t be used as it’s wrapped with the function `UPPER`. 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. 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.
  3. 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'.
  4. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `TAB1` ADD INDEX `tab1_idx_col4_col6` (`COL4`,`COL6`);
ALTER TABLE `TAB2` ADD INDEX `tab2_idx_col4_col5_col6` (`COL4`,`COL5`,`COL6`);
ALTER TABLE `TAB3` ADD INDEX `tab3_idx_col4_col5_col5a_col6` (`COL4`,`COL5`,`COL5A`,`COL6`);
ALTER TABLE `TAB4` ADD INDEX `tab4_idx_col6_pdr` (`COL6`,`PDR`);
The optimized query:
SELECT
        NVL(UPPER(T.COL1),
        'N.D.') COL1,
        V.SECO,
        'N' CL_MED,
        V.DEST_USO,
        (CASE 
            WHEN V.COL2 IS NULL 
            AND V.SECO IN ('B090',
            'B100') THEN '' 
            WHEN V.COL2 LIKE 'L-DEF%' 
            OR V.COL2 LIKE 'L-FUI%' 
            AND V.SECO IN ('B090',
            'B100') THEN 'FUI/DEF' 
            WHEN V.COL2 IS NULL 
            AND V.SECO = 'B080' 
            AND V.COL3 LIKE 'DEF%' 
            OR V.COL3 LIKE 'FUI%' THEN 'FUI/DEF' 
            ELSE '' END) FLAG_DEF_FUI 
FROM
TAB1 V 
JOIN
TAB2 C 
    ON (
        V.COL4 = C.COL4 
        AND V.COL5 = C.COL5 
        AND V.COL6 = C.COL6
    ) 
JOIN
TAB3 T 
    ON (
        V.COL4 = T.COL4 
        AND V.COL5 = T.COL5 
        AND V.COL5A = T.COL5A 
        AND T.COL6 = V.COL6
    ) 
WHERE
V.COL4 = :COL4 
AND V.COL6 = :COL6 
AND NOT EXISTS (
    SELECT
        1 
    FROM
        TAB4 gcm 
    WHERE
        (
            gcm.COL6 = :COL6
        ) 
        AND (
            V.COL5 = gcm.PDR
        )
) 
GROUP BY
(UPPER(T.COL1),
V.SECO,
V.DEST_USO,
FLAG_DEF_FUI) 
ORDER BY
NULL

Related Articles



* original question posted on StackOverflow here.