SQL Tuning. (union all)

Hi,
I am working in Oracle 10g R2 /RAC - Solaris
I have sql which is getting executed more than 1million times during peak hours.
+++++++++++++++++++
SQL Statement
+++++++++++++++++++
select location_id -- (,longitude, latitude)
from ph_location pl
where (longitude between :long1 and :long2)
and (latitude between :lat1 and :lat2)
and catalog_id = :catalog
and location_id in (select location_id
from ph_location_cats
where category_id = :cat
union all
select location_id
from ph_location_prd
where product_id = :prod)
and location_id not in (select location_id
from ph_loc_prd_excluded
where product_id = :prod);
XPLAN
SQL> SET LINESIZE 130
SET PAGESIZE 0
SELECT *
FROM   TABLE(DBMS_XPLAN.DISPLAY);
SQL>   Plan hash value: 3391784670
| Id  | Operation                     | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT              |                        |     1 |    85 |    23   (0)| 00:00:01 |
|*  1 |  FILTER                       |                        |       |       |            |          |
|   2 |   NESTED LOOPS ANTI           |                        |     1 |    85 |    18   (0)| 00:00:01 |
|*  3 |    TABLE ACCESS BY INDEX ROWID| PH_LOCATION            |     1 |    56 |    17   (0)| 00:00:01 |
|*  4 |     INDEX RANGE SCAN          | ID_PH_LOCATION_CALO    |    96 |       |    13   (0)| 00:00:01 |
|   5 |      UNION-ALL                |                        |       |       |            |          |
|*  6 |       INDEX UNIQUE SCAN       | PK_PH_LOCATION_CATS    |     1 |    38 |     3   (0)| 00:00:01 |
|*  7 |       INDEX UNIQUE SCAN       | PK_PH_LOCATION_PRD     |     1 |    30 |     2   (0)| 00:00:01 |
|*  8 |    INDEX UNIQUE SCAN          | PK_PH_LOC_PRD_EXCLUDED |   370 | 10730 |     1   (0)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter(TO_NUMBER(:LAT1)<=TO_NUMBER(:LAT2) AND TO_NUMBER(:LONG1)<=TO_NUMBER(:LONG2))
   3 - filter("LONGITUDE">=TO_NUMBER(:LONG1) AND "LONGITUDE"<=TO_NUMBER(:LONG2) AND
              "LATITUDE">=TO_NUMBER(:LAT1) AND "LATITUDE"<=TO_NUMBER(:LAT2))
   4 - access("CATALOG_ID"=:CATALOG)
       filter( EXISTS ( (SELECT /*+ */ "LOCATION_ID" FROM "PH_LOCATION_CATS" "PH_LOCATION_CATS"
              WHERE "CATEGORY_ID"=:CAT AND "LOCATION_ID"=:B1) UNION ALL  (SELECT /*+ */ "LOCATION_ID" FROM
              "PH_LOCATION_PRD" "PH_LOCATION_PRD" WHERE "PRODUCT_ID"=:PROD AND "LOCATION_ID"=:B2)))
   6 - access("LOCATION_ID"=:B1 AND "CATEGORY_ID"=:CAT)
   7 - access("LOCATION_ID"=:B1 AND "PRODUCT_ID"=:PROD)
   8 - access("LOCATION_ID"="LOCATION_ID" AND "PRODUCT_ID"=:PROD)
29 rows selected.
How to tune this query? Is there any way to re-write this sql without unionall? If so, How? Please help.
thanks
Raj

Hi
Thanks for your reply.
The cost of this query is more compared to original query posted.
{noformat}
SQL> explain plan for select location_id -- (,longitude, latitude)
  2  from ph_location pl
  3  where (longitude between :long1 and :long2)
  4  and (latitude between :lat1 and :lat2)
  5  and catalog_id = :catalog
  6  and (
  7  location_id in (select location_id
  8  from ph_location_cats
  9  where category_id = :cat
10  )
11  or
12  location_id in (
13  select location_id
from ph_location_prd
14   15  where product_id = :prod
16  )
17  )
18  and location_id not in (select location_id
19  from ph_loc_prd_excluded
20  where product_id = :prod);
Explained.
SQL> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
Plan hash value: 2662773144
| Id  | Operation                      | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT               |                        |     1 |    71 |    82   (0)| 00:00:01 |
|*  1 |  FILTER                        |                        |       |       |            |          |
|*  2 |   FILTER                       |                        |       |       |            |          |
|   3 |    NESTED LOOPS ANTI           |                        |     1 |    71 |    82   (0)| 00:00:01 |
|*  4 |     TABLE ACCESS BY INDEX ROWID| PH_LOCATION            |     1 |    42 |    81   (0)| 00:00:01 |
|*  5 |      INDEX RANGE SCAN          | ID_PH_LOCATION_CI      |  1923 |       |    16   (0)| 00:00:01 |
|*  6 |     INDEX UNIQUE SCAN          | PK_PH_LOC_PRD_EXCLUDED |   370 | 10730 |     1   (0)| 00:00:01 |
|*  7 |   INDEX UNIQUE SCAN            | PK_PH_LOCATION_CATS    |     1 |    38 |     3   (0)| 00:00:01 |
|*  8 |    INDEX UNIQUE SCAN           | PK_PH_LOCATION_PRD     |     1 |    30 |     2   (0)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter( EXISTS (SELECT /*+ */ 0 FROM "PH_LOCATION_CATS" "PH_LOCATION_CATS" WHERE
              "CATEGORY_ID"=:CAT AND "LOCATION_ID"=:B1) OR  EXISTS (SELECT /*+ */ 0 FROM "PH_LOCATION_PRD"
              "PH_LOCATION_PRD" WHERE "PRODUCT_ID"=:PROD AND "LOCATION_ID"=:B2))
   2 - filter(TO_NUMBER(:LAT1)<=TO_NUMBER(:LAT2) AND TO_NUMBER(:LONG1)<=TO_NUMBER(:LONG2))
   4 - filter("LONGITUDE">=TO_NUMBER(:LONG1) AND "LONGITUDE"<=TO_NUMBER(:LONG2) AND
              "LATITUDE">=TO_NUMBER(:LAT1) AND "LATITUDE"<=TO_NUMBER(:LAT2))
   5 - access("CATALOG_ID"=:CATALOG)
   6 - access("LOCATION_ID"="LOCATION_ID" AND "PRODUCT_ID"=:PROD)
   7 - access("LOCATION_ID"=:B1 AND "CATEGORY_ID"=:CAT)
   8 - access("LOCATION_ID"=:B1 AND "PRODUCT_ID"=:PROD)
29 rows selected.
{noformat}

Similar Messages

  • SQL 2008 union all numeric column

    Hi guys, I'm struggling with an union all. I tried to union a column float with a null column:
    SELECT column 1,
    column 2,
    '' AS 'column 3'
    FROM table a
    UNION 
    SELECT column 1,
    column 2,
    column 3 FROM
    table b
    If column 3 is varchar everything ok, if ( as in my case) is numeric I got an error. Any sugegstion guys? Thanks as always

    even the below would work, but your solution looks better as column3 is a numeric column...
    SELECT column 1, column 2, '' AS 'column
    3' FROM table a
    UNION  
    SELECT column 1, column 2, Cast(column3
    as varchar(100)) FROM table b

  • Simplify the UNION ALL SQL

    Hi Guys,
    Oracle Version: 10g.
    I got a materialized having the following sql. Is there anyway, the SQL can be simplified by removing the unnecessary
    aggregate function and group by clause. Any pointers will be highly appreciated. Thanks in advance!
    SELECT P.PRD_ID,
             D.MKT_ID,
             D.FY_WK_END,
             D.WK_TAG_CD,
             SUM (D.P_QTY) ,
             SUM (D.C_QTY) ,
             SUM (D.SLS_AMT) ,
             SUM (D.E_QTY) ,
             SUM (D.CALC) ,
             SUM (D.PKG_QTY * P.FCTR) ,
             SUM (D.PKG_QTY * P.U_FCTR) ,
             SUM(D.PNDS)
        FROM (  SELECT P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD,
                       SUM (D.PKG) AS P_QTY,
                       SUM (D.BILL) AS C_QTY,
                       SUM (D.SLS_AMT) AS SLS_AMT,
                       SUM (D.E_QTY) AS E_QTY,
                       SUM (D.BILL * P.FCTR) AS CALC,
                       SUM (D.BILL * BWM.GROSS_WT) AS PNDS
                  FROM DEL_F D,
                       PRD_D P,
                       CUST_D T,
                       CALD C,
                       (SELECT MKT_ID,
                               MKT_CD,
                               SORG
                          FROM MKT_M
                         WHERE SORG = '803'
                               AND UPPER (GRP_TY) = 'CGS') M,
                       MV_MAT BWM
                 WHERE     D.PRD_ID = P.PRD_ID
                       AND D.CUST_ID = T.CUST_ID
                       AND T.CUST_CD = M.MKT_CD
                       AND M.SELORG = D.SELORG
                       AND D.WK_ENDING_DT = C.CAL_DY_DT
                       AND P.MATL_CD = BWM.MAT
              GROUP BY P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD) D,
            MV_UP P
       WHERE P.FIN_ID = D.PRD_ID
    GROUP BY P.PRD_ID,
             D.MKT_ID,
             D.WK_ENDING_DT,
             D.WK_TAG_CD
    UNION ALL
    SELECT P.PRD_ID,
             D.MKT_ID,
             D.FY_WK_END,
             D.WK_TAG_CD,
             SUM (D.PKG_QTY) ,
             SUM (D.CASE_QTY) ,
             SUM (D.SLS_AMT) ,
             SUM (D.EQC_QTY) ,
             SUM (D.CALC_QTY) ,
             SUM (D.PKG_QTY * P.FCTR) ,
             SUM (D.PKG_QTY * P.U_FCTR) ,
             SUM(D.PNDS)
        FROM (  SELECT P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD,
                       SUM (D.PKG) AS P_QTY,
                       SUM (D.BILL) AS C_QTY,
                       SUM (D.SLS_AMT) AS SLS_AMT,
                       SUM (D.E_QTY) AS E_QTY,
                       SUM (D.BILL * P.FCTR) AS CALC,
                       SUM (D.BILL * BWM.GROSS_WT) AS PNDS
                  FROM DEL_F D,
                       PRD_D P,
                       SELD S,
                       CALD C,
                       (SELECT MKT_ID,
                               MKT_CD,
                               SORG
                          FROM MKT_M
                         WHERE SORG = '803'
                               AND UPPER (GRP_TY) = 'SR') M,
                       MV_MAT BWM
                 WHERE     D.PRD_ID = P.PRD_ID
                       AND D.SLL_ID = S.SLL_ID
                       AND S.RGN_CD = M.MKT_CD
                       AND M.SORG = D.SORG
                       AND D.WK_ENDING_DT = C.CAL_DT
                       AND P.MATL_CD = BWM.MATL
              GROUP BY P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD) D,
             MV_UP P
       WHERE P.FIN_ID = D.PRD_ID
    GROUP BY  P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD;

    SELECT P.PRD_ID,
             D.MKT_ID,
             D.FY_WK_END,
             D.WK_TAG_CD,
             SUM (D.P_QTY) ,
             SUM (D.C_QTY) ,
             SUM (D.SLS_AMT) ,
             SUM (D.E_QTY) ,
             SUM (D.CALC) ,
             SUM (D.PKG_QTY * P.FCTR) ,
             SUM (D.PKG_QTY * P.U_FCTR) ,
             SUM(D.PNDS)
        FROM (  SELECT P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD,
                       SUM (D.PKG) AS P_QTY,
                       SUM (D.BILL) AS C_QTY,
                       SUM (D.SLS_AMT) AS SLS_AMT,
                       SUM (D.E_QTY) AS E_QTY,
                       SUM (D.BILL * P.FCTR) AS CALC,
                       SUM (D.BILL * BWM.GROSS_WT) AS PNDS
                  FROM DEL_F D,
                       PRD_D P,
                       CUST_D T,
                       CALD C,
                       (SELECT MKT_ID,
                               MKT_CD,
                               SORG
                          FROM MKT_M
                         WHERE SORG = '803'
                               AND UPPER (GRP_TY) = 'CGS') M,
                       MV_MAT BWM
                 WHERE     D.PRD_ID = P.PRD_ID
                       AND D.CUST_ID = T.CUST_ID
                       AND T.CUST_CD = M.MKT_CD
                       AND M.SELORG = D.SELORG
                       AND D.WK_ENDING_DT = C.CAL_DY_DT
                       AND P.MATL_CD = BWM.MAT
              GROUP BY P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD) D,
            MV_UP P
       WHERE P.FIN_ID = D.PRD_ID
    GROUP BY P.PRD_ID,
             D.MKT_ID,
             D.WK_ENDING_DT,
             D.WK_TAG_CD
    UNION ALL
    SELECT P.PRD_ID,
             D.MKT_ID,
             D.FY_WK_END,
             D.WK_TAG_CD,
             SUM (D.PKG_QTY) ,
             SUM (D.CASE_QTY) ,
             SUM (D.SLS_AMT) ,
             SUM (D.EQC_QTY) ,
             SUM (D.CALC) ,
             SUM (D.PKG_QTY * P.FCTR) ,
             SUM (D.PKG_QTY * P.U_FCTR) ,
             SUM(D.PNDS)
        FROM (  SELECT P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD,
                       SUM (D.PKG) AS P_QTY,
                       SUM (D.BILL) AS C_QTY,
                       SUM (D.SLS_AMT) AS SLS_AMT,
                       SUM (D.E_QTY) AS E_QTY,
                       SUM (D.BILL * P.FCTR) AS CALC,
                       SUM (D.BILL * BWM.GROSS_WT) AS PNDS
                  FROM DEL_F D,
                       PRD_D P,
                       SELD S,
                       CALD C,
                       (SELECT MKT_ID,
                               MKT_CD,
                               SORG
                          FROM MKT_M
                         WHERE SORG = '803'
                               AND UPPER (GRP_TY) = 'SR') M,
                       MV_MAT BWM
                 WHERE     D.PRD_ID = P.PRD_ID
                       AND D.SLL_ID = S.SLL_ID
                       AND S.RGN_CD = M.MKT_CD
                       AND M.SORG = D.SORG
                       AND D.WK_ENDING_DT = C.CAL_DY_DT
                       AND P.MATL_CD = BWM.MAT
              GROUP BY P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD) D,
             MV_UP P
       WHERE P.FIN_ID = D.PRD_ID
    GROUP BY  P.PRD_ID,
                       M.MKT_ID,
                       C.WK_ENDING_DT,
                       C.WK_TAG_CD;Hi Hoek, Etbin:
    Thanks for the replies. As you can see above both the SQL, SQL1 UNION ALL SQL2 are almost alike except at a few places.
    1. Tables: CUST_D and SELD
    2. Filtering code for MKT_M
    3. JOIN conditions of INLINE VIEWS of SQL1 and SQL2.
    Problem 1: SQL1 and SQL2 has SUM function which aggregates the column that comes from the inline view when "WHERE P.FIN_ID = D.PRD_ID",
    this join condition is satisfied. I am thinking since outer SQL are aggregating the same columns which are aggregated in the inline view for example -
    SUM (D.CALC) <- This guy is in outer sql of both SQL1 and SQL2.
    SUM (D.BILL * P.FCTR) AS CALC <- This guy is in inner sql of SQL1 and SQL2.
    Does that mean, I can remove the SUM from outer sql and just write D.CALC?
    Can I do the same things for other SUMs too?
    OR
    Can I remove the aggregation from inner sql i.e. inline view e.g. SUM (D.BILL * P.FCTR) AS CALC becomes just D.BILL * P.FCTR and then aggregate it in
    outer sql?
    Problem 2. If the above things can be done than instead of grouping bys twice in SQL1 and SQL2, we can group by only once. Will it give the same result as the original SQL?
    Problem 3. Any other way of writing the sql in a simplified way? Can it be made simple?
    Please advice.
    Thanks in advance!
    Edited by: abyss on Mar 12, 2011 9:41 AM
    Edited by: abyss on Mar 12, 2011 9:43 AM
    Edited by: abyss on Mar 12, 2011 9:43 AM
    Edited by: abyss on Mar 12, 2011 9:44 AM

  • Union All is not bring the second part columns to the report

    I have 5 groups of series of Select statements and then I made a UNION ALL after the first group. Now it does run correctly and displays the output but it does not assign the second group' s field names.
    For example. I am greatly abridging
    Completed the SQL's needed to mimic a IBM i display screen. There are 5 product groups making up the screen. I was hoping to have one large Command SQL having the 5 SQLs using UNION ALL. This does 'compile' as a Command. However, it does only brings in the first part fields not the second. So this field is not included in the list of fields tree for COMMAND. PROGR2R2PST,
    Is there something not correct how doing the UNION ALL? OUTPUT assigns the column name
    of the first group to the second group. HLDGR1PUN 21454 87273
    so if i wanted to have one large SQL with union ALLs, it wont work. is there something other than UNION ALL I should use? One idea is if I can have 5 commands to my disposal. I guess  using subreports you can but it will be too slow.
    SELECT
    count(*) as PROGR1PST,
    SELECT COALESCE(SUM(OdQty#),0)
    FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
    WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
    AND TSSTAT IN('AEP','BGE')
    AND OHORDT IN('RTR','INT','SAM')
    AND OHREQD < replace(char(current date, iso), '-', '')
    AND OHHLDC = ' '
    AND ODPRLC = 'ENG'
    AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
    WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
    ) AS PROGR1PUN,
    SELECT count(*)
    FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTCCDTA.OETRA99
    WHERE OHCOM# = TSCOM# AND OHORD# = TSORD#
    AND (otCOM# = OHCOM# AND OTORD#= OHORD# AND ottrnc = 'AQC')
    AND TSSTAT IN('AEP','BGE')
    AND OHORDT IN('RTR','INT','SAM')
    AND OHREQD = replace(char(current date, iso), '-', '')  AND OHHLDC = ' ' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a
    WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
    ) AS PROGR1TOD,
    etc..
    UNION ALL
    SELECT
    count(*) as PROGR2R2PST,
    (SELECT COALESCE(SUM(OdQty#),0) FROM ASTCCDTA.OEORH48,ASTCCDTA.TRNSTAT2,ASTDTA.OEORD1
      WHERE OHCOM# = TSCOM# AND OHORD# = TSORD# AND OHCOM# = ODCOM# AND OHORD# = ODORD#
      AND TSSTAT IN('AEP','BGE')
      AND OHORDT IN('CUS','CIN','SMC','COC','DON')
      AND OHREQD < replace(char(current date, iso), '-', '')
      AND OHHLDC = ' '
      AND ODPRLC = 'ENG'
      AND substr(odprt#,1,5) <> 'NOENG' AND OHORD# in(SELECT a.TSORD# FROM ASTCCDTA.TRNSTAT2 a 
    WHERE a.tsstat IN('AEP','BGE','EAS','REL','STP'))
    ) AS PROGR2PUN,

    hi Paul,
    a union combines record sets like this
    field1     field2
    1          2
    union
    field3     field4
    2          8
    will produce
    field1     field2
    1          2
    2          8
    note that there are no more field3 & field4 names. that's the way a union works.
    if you need separate fields for your other values then you would need to use sub-queries instead. to do that delete "UNION ALL " and create sub-queries with anything that is beneath that statement.

  • Issue with union all in sql

    Hi All,
    I have a requirement as below.
    SELECT 'A' AS 'XXX' FROM DUAL
    UNION ALL
    SELECT 'B' AS 'XXX' FROM DUAL
    I need to check in such a way in my second sql query if 'B'='A' then i need to print 'A' else 'B' , means in my second query i need to compare the value of XXX with the first query value of XXX if it is same then i need to print first query's value of XXX or else need to print second queries XXX value.
    Please help me on this

    user13424229 wrote:
    Hi All,
    I have a requirement as below.
    SELECT 'A' AS 'XXX' FROM DUAL
    UNION ALL
    SELECT 'B' AS 'XXX' FROM DUAL
    I need to check in such a way in my second sql query if 'B'='A' then i need to print 'A' else 'B' , means in my second query i need to compare the value of XXX with the first query value of XXX if it is same then i need to print first query's value of XXX or else need to print second queries XXX value.
    Please help me on thisYour quesiton is not very clear, I would suggest you read {message:id=9360002}.
    If you are looking at a way to see the next or previous row from your current row then you can use LEAD or LAG analytical function. They are well documented. You can read all about it there.

  • Inconsistent SQL results when using View with UNION-ALL and table function

    Can any of you please execute the below scripts and check the output. In the table type variable, I am adding 4 distinct object ids, where as in the result, I get only the row pertaining to last id in the table type variable. Same row is returned 4 times (4= number of values in the table type).
    This scenario is occurring in our product with a SQL with exactly same pattern. I could simulate the same issue with the sample script I have provided.
    Database version: 11.2.0.3 Enterprise Edition, Single node
    Thank you.
    CREATE TABLE TEMP_T1 AS SELECT * FROM ALL_OBJECTS;
    CREATE TABLE TEMP_T2 AS SELECT * FROM ALL_OBJECTS;
    UPDATE TEMP_T2 SET OBJECT_ID = OBJECT_ID * 37;
    CREATE UNIQUE INDEX TEMP_T1_U1 ON TEMP_T1(OBJECT_ID);
    CREATE UNIQUE INDEX TEMP_T2_U1 ON TEMP_T2(OBJECT_ID);
    CREATE OR REPLACE VIEW TEMP_T1T2_V AS
    SELECT * FROM TEMP_T1 UNION ALL SELECT * FROM TEMP_T2;
    CREATE OR REPLACE TYPE TEMP_OBJ_TYPE AS OBJECT (OBJ_ID NUMBER);
    CREATE OR REPLACE TYPE TEMP_OBJ_TAB_TYPE IS TABLE OF TEMP_OBJ_TYPE;
    SET SERVEROUTPUT ON;
    DECLARE
    TYPE TEMP_T1T2_V_ROW_TAB_TYPE IS TABLE OF TEMP_T1T2_V%ROWTYPE;
    TEMP_T1T2_V_ROW_TAB TEMP_T1T2_V_ROW_TAB_TYPE;
    TEMP_OBJ_TAB TEMP_OBJ_TAB_TYPE := TEMP_OBJ_TAB_TYPE();
    PROCEDURE ADD_TO_TEMP_OBJ_TAB(OBJ_ID IN NUMBER) IS
    BEGIN
    TEMP_OBJ_TAB.EXTEND;
    TEMP_OBJ_TAB(TEMP_OBJ_TAB.LAST) := TEMP_OBJ_TYPE(OBJ_ID);
    END;
    BEGIN
    ADD_TO_TEMP_OBJ_TAB(100);
    ADD_TO_TEMP_OBJ_TAB(116);
    ADD_TO_TEMP_OBJ_TAB(279);
    ADD_TO_TEMP_OBJ_TAB(364);
    DBMS_OUTPUT.PUT_LINE('=====================');
    FOR I IN TEMP_OBJ_TAB.FIRST..TEMP_OBJ_TAB.LAST
    LOOP
    DBMS_OUTPUT.PUT_LINE('OBJ_ID = '||TEMP_OBJ_TAB(I).OBJ_ID);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('---------------------');
    SELECT * BULK COLLECT INTO TEMP_T1T2_V_ROW_TAB
    FROM TEMP_T1T2_V VW
    WHERE ((VW.OBJECT_ID) IN (SELECT OBJ_ID
    FROM TABLE(CAST(TEMP_OBJ_TAB AS TEMP_OBJ_TAB_TYPE))));
    FOR I IN TEMP_OBJ_TAB.FIRST..TEMP_OBJ_TAB.LAST
    LOOP
    DBMS_OUTPUT.PUT_LINE('OBJ_ID = '||TEMP_OBJ_TAB(I).OBJ_ID);
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('---------------------');
    IF TEMP_T1T2_V_ROW_TAB.COUNT > 0 THEN
    FOR I IN TEMP_T1T2_V_ROW_TAB.FIRST..TEMP_T1T2_V_ROW_TAB.LAST
    LOOP
    DBMS_OUTPUT.PUT_LINE(TEMP_T1T2_V_ROW_TAB(I).OBJECT_ID||' : '||TEMP_T1T2_V_ROW_TAB(I).OBJECT_NAME);
    END LOOP;
    ELSE
    DBMS_OUTPUT.PUT_LINE('NO ROWS RETURNED!');
    END IF;
    DBMS_OUTPUT.PUT_LINE('---------------------');
    END;
    /

    I can reproduce it:
    SQL*Plus: Release 11.2.0.3.0 Production on Tue Oct 30 14:05:39 2012
    Copyright (c) 1982, 2011, Oracle.  All rights reserved.
    Enter user-name: scott
    Enter password:
    Connected to:
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    SQL> select  *
      2    from  v$version
      3  /
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE    11.2.0.3.0      Production
    TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    SQL> CREATE TABLE TEMP_T1 AS SELECT * FROM ALL_OBJECTS;
    Table created.
    SQL>
    SQL> CREATE TABLE TEMP_T2 AS SELECT * FROM ALL_OBJECTS;
    Table created.
    SQL>
    SQL> UPDATE TEMP_T2 SET OBJECT_ID = OBJECT_ID * 37;
    72883 rows updated.
    SQL>
    SQL> CREATE UNIQUE INDEX TEMP_T1_U1 ON TEMP_T1(OBJECT_ID);
    Index created.
    SQL>
    SQL> CREATE UNIQUE INDEX TEMP_T2_U1 ON TEMP_T2(OBJECT_ID);
    Index created.
    SQL>
    SQL> CREATE OR REPLACE VIEW TEMP_T1T2_V AS
      2  SELECT * FROM TEMP_T1 UNION ALL SELECT * FROM TEMP_T2;
    View created.
    SQL>
    SQL> CREATE OR REPLACE TYPE TEMP_OBJ_TYPE AS OBJECT (OBJ_ID NUMBER)
      2  /
    Type created.
    SQL> CREATE OR REPLACE TYPE TEMP_OBJ_TAB_TYPE IS TABLE OF TEMP_OBJ_TYPE
      2  /
    Type created.
    SQL> SET SERVEROUTPUT ON;
    SQL>
    SQL> DECLARE
      2  TYPE TEMP_T1T2_V_ROW_TAB_TYPE IS TABLE OF TEMP_T1T2_V%ROWTYPE;
      3  TEMP_T1T2_V_ROW_TAB TEMP_T1T2_V_ROW_TAB_TYPE;
      4  TEMP_OBJ_TAB TEMP_OBJ_TAB_TYPE := TEMP_OBJ_TAB_TYPE();
      5  PROCEDURE ADD_TO_TEMP_OBJ_TAB(OBJ_ID IN NUMBER) IS
      6  BEGIN
      7  TEMP_OBJ_TAB.EXTEND;
      8  TEMP_OBJ_TAB(TEMP_OBJ_TAB.LAST) := TEMP_OBJ_TYPE(OBJ_ID);
      9  END;
    10  BEGIN
    11  ADD_TO_TEMP_OBJ_TAB(100);
    12  ADD_TO_TEMP_OBJ_TAB(116);
    13  ADD_TO_TEMP_OBJ_TAB(279);
    14  ADD_TO_TEMP_OBJ_TAB(364);
    15  DBMS_OUTPUT.PUT_LINE('=====================');
    16  FOR I IN TEMP_OBJ_TAB.FIRST..TEMP_OBJ_TAB.LAST
    17  LOOP
    18  DBMS_OUTPUT.PUT_LINE('OBJ_ID = '||TEMP_OBJ_TAB(I).OBJ_ID);
    19  END LOOP;
    20  DBMS_OUTPUT.PUT_LINE('---------------------');
    21  SELECT * BULK COLLECT INTO TEMP_T1T2_V_ROW_TAB
    22  FROM TEMP_T1T2_V VW
    23  WHERE ((VW.OBJECT_ID) IN (SELECT OBJ_ID
    24  FROM TABLE(CAST(TEMP_OBJ_TAB AS TEMP_OBJ_TAB_TYPE))));
    25  FOR I IN TEMP_OBJ_TAB.FIRST..TEMP_OBJ_TAB.LAST
    26  LOOP
    27  DBMS_OUTPUT.PUT_LINE('OBJ_ID = '||TEMP_OBJ_TAB(I).OBJ_ID);
    28  END LOOP;
    29  DBMS_OUTPUT.PUT_LINE('---------------------');
    30  IF TEMP_T1T2_V_ROW_TAB.COUNT > 0 THEN
    31  FOR I IN TEMP_T1T2_V_ROW_TAB.FIRST..TEMP_T1T2_V_ROW_TAB.LAST
    32  LOOP
    33  DBMS_OUTPUT.PUT_LINE(TEMP_T1T2_V_ROW_TAB(I).OBJECT_ID||' : '||TEMP_T1T2_V_ROW_TAB(I).OBJECT_NAME);
    34  END LOOP;
    35  ELSE
    36  DBMS_OUTPUT.PUT_LINE('NO ROWS RETURNED!');
    37  END IF;
    38  DBMS_OUTPUT.PUT_LINE('---------------------');
    39  END;
    40  /
    =====================
    OBJ_ID = 100
    OBJ_ID = 116
    OBJ_ID = 279
    OBJ_ID = 364
    OBJ_ID = 100
    OBJ_ID = 116
    OBJ_ID = 279
    OBJ_ID = 364
    364 : I_AUDIT
    364 : I_AUDIT
    364 : I_AUDIT
    364 : I_AUDIT
    PL/SQL procedure successfully completed.
    SQL> column object_name format a30
    SQL> select  object_id,
      2          object_name
      3    from  dba_objects
      4    where object_id in (100,116,279,364)
      5  /
    OBJECT_ID OBJECT_NAME
           100 ORA$BASE
           116 DUAL
           279 MAP_OBJECT
           364 I_AUDIT
    SQL>  Works fine in:
    =====================
    OBJ_ID = 100
    OBJ_ID = 116
    OBJ_ID = 279
    OBJ_ID = 364
    OBJ_ID = 100
    OBJ_ID = 116
    OBJ_ID = 279
    OBJ_ID = 364
    100 : ORA$BASE
    116 : DUAL
    364 : SYSTEM_PRIVILEGE_MAP
    279 : MAP_OBJECT
    PL/SQL procedure successfully completed.
    SQL> select  object_id,
      2          object_name
      3    from  dba_objects
      4    where object_id in (100,116,279,364)
      5  /
    OBJECT_ID OBJECT_NAME
          100 ORA$BASE
          116 DUAL
          364 SYSTEM_PRIVILEGE_MAP
          279 MAP_OBJECT
    SQL> select  *
      2    from  v$version
      3  /
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE    11.2.0.1.0      Production
    TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    SQL>SY.
    Edited by: Solomon Yakobson on Oct 30, 2012 2:14 PM

  • Need sql query to remove duplicates using UNION ALL clause

    Hi,
    I have a sql query which has UNION clause.But the UNION clause is causing some performance issues.
    To overcome that I have used UNION ALL to improve performance but its returning duplicates.
    Kindly anyone send a sample SQL query where my primary objective is used to use UNION ALL clause and to consider unique rows (elimating duplicate
    ones)
    Any help will be needful for me
    Thanks and Regards

    why not UNION? :(
    another way also use MINUS
    SQL>
    SQL> with t as
      2  (
      3  select 1 if from dual union all
      4  select 2 if from dual union all
      5  select 1 if from dual union all
      6  select 3 if from dual union all
      7  select 3 if from dual
      8  )
      9  ,t2 as
    10  (
    11  select 1 if from dual union all
    12  select 2 if from dual union all
    13  select 3 if from dual union all
    14  select 4 if from dual union all
    15  select 5 if from dual
    16  )
    17  (select if from t
    18  union all
    19  select if from t2)
    20  /
            IF
             1
             2
             1
             3
             3
             1
             2
             3
             4
             5
    10 rows selected
    SQL> so
    SQL>
    SQL> with t as
      2  (
      3  select 1 if from dual union all
      4  select 2 if from dual union all
      5  select 1 if from dual union all
      6  select 3 if from dual union all
      7  select 3 if from dual
      8  )
      9  ,t2 as
    10  (
    11  select 1 if from dual union all
    12  select 2 if from dual union all
    13  select 3 if from dual union all
    14  select 4 if from dual union all
    15  select 5 if from dual
    16  )
    17  (select if from t
    18  union all
    19  select if from t2)
    20  minus
    21  select -99 from dual
    22  /
            IF
             1
             2
             3
             4
             5
    SQL>

  • SQL Union all VS SSIS Union All

    I have 4 source SQL queries which is pointing different db's in the same server. All 4 queries results need to stored in single SQLServer table of different server. which one is best approach mentioned in below ?
    1) Writing SP using union all option with all four source queries and use this sp in single data flow task[single source and destination tasks].
    2) 4 source tasks to execute the 4 sql queries and then use Union all transformation to club the result and then one destination.
    3) 4 different data flow task. Each data flow task point the different souurce queries and the same destination.
    Please suggest in all aspects.

    Prabu,
    You are right in your observations from your previous post. Based on my experience, number 2 would be the best option.
    First of all, it might be best to parallelize the extraction because the time taken to read from all sources equals the time to read from the source with the highest volume of data, whereas reading from sources serially would result in a sum of the time
    taken to read from each of them individually.
    Secondly, BULK LOADS with table locks are very fast, but you can only have one process writting to the same destination at the same time, because a table level lock is placed on the object. This will probably offset the performance gains of a parallel insert
    into the same destination, but it is worth testing.
    IF, and only IF, your process uses a lot of CPU time for some reason (transformations are performed), then there is a chance option number 3 will outperform option number 2 because CPU workload would generally be split between multiple processors, but I
    dont think this is your case.
    Just because there are clouds in the sky it doesn't mean it isn't blue. Some people would disagree.

  • Select extra row without using UNION ALL in pl/sql

    Hi,
    Can anyone tell me how to select extra row without using UNION or UNION ALL in pl/sql. Actually I want to have my o/p of query as partitioned by designation and ordered by salary and than one extra row which will contain the highest salary in a particular salary. My table has first_name,emp_id,designation and salary column. And I wnt the o/p as.
    Mohinish,12212,SI,46000
    Ram,11212,SSI,47000
    Shyam,12133,SI,48000
    Rick,9898,SI,46000
    Rocky,12312,SSI,56000
    Sariq,23948,SI,43000
    Suman,12789,HR,49000
    Sampy,12780,SI,46000
    Parna,11111,HR,50000
    Now the o/p should be.
    Mohinish,12212,SI,46000
    Rick,9898,SI,46000
    Sariq,23948,SI,43000
    Shyam,12133,SI,48000
    Shyam,12133,SI,48000
    Ram,11212,SSI,47000
    Rocky,12312,SSI,56000
    Rocky,12312,SSI,56000
    Suman,12789,HR,49000
    Parna,11111,HR,50000
    Parna,11111,HR,50000
    Thanks in Advance

    You don't have to do a UNION or UNION ALL in PL/SQL but you would need to in SQL to get the desired output:
    with data_recs
    as (select 'Mohinish' first_name,12212 emp_id,'SI' designation,46000 salary from dual union
         select 'Ram',11212,'SSI',47000 from dual union
         select 'Shyam',12133,'SI',48000 from dual union
         select 'Rick',9898,'SI',46000 from dual union
         select 'Rocky',12312,'SSI',56000 from dual union
         select 'Sariq',23948,'SI',43000 from dual union
         select 'Suman',12789,'HR',49000 from dual union
         select 'Sampy',12780,'SI',46000 from dual union
         select 'Parna',11111,'HR',50000 from dual)
    select first_name, emp_id, designation, salary from data_recs union all
    select s.first_name, s.emp_id, s.designation, s.salary
      from (select first_name,
                   emp_id,
                   designation,
                   salary,
                   row_number() over (partition by designation order by salary desc) high_salary
              from data_recs
             order by designation, salary) s
    where s.high_salary = 1
    order by designation, salary;
    FIRST_NAME  EMP_ID DESIGNATION   SALARY
    Suman        12789 HR             49000
    Parna        11111 HR             50000
    Parna        11111 HR             50000
    Sariq        23948 SI             43000
    Rick          9898 SI             46000
    Mohinish     12212 SI             46000
    Sampy        12780 SI             46000
    Shyam        12133 SI             48000
    Shyam        12133 SI             48000
    Ram          11212 SSI            47000
    Rocky        12312 SSI            56000
    Rocky        12312 SSI            56000

  • UNION ALL inthe sql stament

    Hola everyboy,
    I want that one user who hasn`t permiss for edit sql, can do a select with UNION ALL when he want.
    Sometimes he want do selects with UNION, only UNION:
    Select adress FROM TABLE WHERE Column='PEPE'
    UNION
    Select adress FROM TABLE2 WHERE Column='PEPE'
    and sometimes he want do selects with UNION ALL:
    Select adress FROM TABLE WHERE Column='PEPE'
    UNION ALL
    Select adress FROM TABLE2 WHERE Column='PEPE'
    I have seen that if I edit the parameter <Parameter Name="UNION">UNION</Parameter>  in sqlsrv.prm in local machine I could use UNION ALL but this is for ALL selects that I do.
    Is there any other way that I can use?
    Thanks!!

    Hello Cesar,
    please post in what product you are working that we can point youi to teh right forum.
    Crystal Reports Designer ? Universe Designer ?
    Thanks
    Falk

  • Help : UNION ALL Query tuning

    Hi all,
    I want to fetch records from mview m_view1 joining the columns in table1 and table2.
    select mv1.col1,mv2.col2.., from mview1 mv1
    where mv1.col1 in (
    SELECT t1.col1
    FROM table1 t1
    WHERE t1.col2 = SUBSTR(user,3)
    UNION ALL
    SELECT t2.col1
    FROM table2 t2
    WHERE t2.col2 = SUBSTR(user,3)
    UNION ALL
    SELECT t1.col1
    FROM table1 t1, table2 t2
    WHERE t2.col2 = SUBSTR(user,3)
    AND t2.col1 = t1.col2
    Below is the explain plan I got :
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 616 | 2690 (1)| 00:00:33 |
    | 1 | NESTED LOOPS | | 1 | 616 | 2690 (1)| 00:00:33 |
    | 2 | VIEW | VW_NSO_1 | 2657 | 34541 | 30 (0)| 00:00:01 |
    | 3 | HASH UNIQUE | | 2657 | 61164 | 30 (90)| 00:00:01 |
    | 4 | UNION-ALL | | | | | |
    |* 5 | INDEX RANGE SCAN | table1_indx1      | 176 | 1760 | 3 (0)| 00:00:01 |
    |* 6 | INDEX RANGE SCAN | table2_prim1      | 14 | 196 | 2 (0)| 00:00:01 |
    | 7 | NESTED LOOPS | | 2467 | 59208 | 25 (0)| 00:00:01 |
    |* 8 | INDEX RANGE SCAN | table2_prim1      | 14 | 196 | 2 (0)| 00:00:01 |
    |* 9 | INDEX RANGE SCAN | table1_indx2      | 176 | 1760 | 2 (0)| 00:00:01 |
    |* 10 | MAT_VIEW ACCESS BY INDEX ROWID| mview1                     | 1 | 603 | 1 (0)| 00:00:01 |
    |* 11 | INDEX UNIQUE SCAN | indx1                              | 1 | | 0 (0)| 00:00:01 |
    table1_indx2 - index on col1 & col2 of table1.
    table1_indx1 - index on col1 & col2 & col3 of table1.
    Im new to oracle tuning. Could you please help me in tuning this.
    Thanks in advance.

    Hi Rob,
    Given below the explain plan for the same:
    | Id  | Operation                       | Name                     | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT                |                          |     1 |   616 |  2690   (1)| 00:00:33 |
    |   1 |  NESTED LOOPS                   |                          |     1 |   616 |  2690   (1)| 00:00:33 |
    |   2 |   VIEW                          | VW_NSO_1                 |  2657 | 34541 |    30   (0)| 00:00:01 |
    |   3 |    HASH UNIQUE                  |                          |  2657 | 61164 |    30  (90)| 00:00:01 |
    |   4 |     UNION-ALL                   |                          |       |       |            |          |
    |*  5 |      INDEX RANGE SCAN           | table1_indx1                 |   176 |  1760 |     3   (0)| 00:00:01 |
    |*  6 |      INDEX RANGE SCAN           | table2_prim1                |    14 |   196 |     2   (0)| 00:00:01 |
    |   7 |      NESTED LOOPS               |                          |  2467 | 59208 |    25   (0)| 00:00:01 |
    |*  8 |       INDEX RANGE SCAN          | table2_prim1                |    14 |   196 |     2   (0)| 00:00:01 |
    |*  9 |       INDEX RANGE SCAN          | table1_indx2                 |   176 |  1760 |     2   (0)| 00:00:01 |
    |* 10 |   MAT_VIEW ACCESS BY INDEX ROWID| mview1                        |     1 |   603 |     1   (0)| 00:00:01 |
    |* 11 |    INDEX UNIQUE SCAN            | indx1                               |     1 |       |     0   (0)| 00:00:01 |
    ------------------------------------------------------------------------------------------------------------

  • Alternate for union all oracle sql

    hi
    is there any alternate for union all ? becaz its verly slow...
    Regards
    raj

    CharlesRoos wrote:
    I think you can create a materialized view over the union query, and then index that view and put the view refresh rate to 1 second or something. Theoretically sounds like it is doable.my car engine is mis-firing and making my car slow, shall I attach a propeller to the boot of the car to push it allong a bit?
    better to fix the poorly performing engine

  • How to make the sql with unions to run faster

    I have the following sql which takes long time to run in sqlplus. This is in oracle 10, can I use hints to speed up the process. Union all brings all rows, but I want to eliminate the duplicates.
    select A.EMPLID,S.OPRID,A.LASTUPDDTTM,A.EMPL_RCD_NBR,A.LAST_NAME,A.FIRST_NAME,A.DEPTID,B.COMPANY,B.PAYGROUP,B.PAY_END_DT,B.OFF_CYCLE,
    B.ERNCD_REG_HRS,B.REG_HRS,0,B.REG_HRLY_EARNS
    from PS_EMPLOYEES A,PS_PAY_EARNINGS B,PS_SCRTY_TBL_DEPT S,PSTREENODE T
    where S.ACCESS_CD = 'Y' and T.SETID = ' ' and T.TREE_NAME = 'DEPT_SECURITY'
    and T.EFFDT = S.TREE_EFFDT and T.TREE_NODE = A.DEPTID and T.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END
    and not exists (select 'X' from PS_SCRTY_TBL_DEPT S1
    where S.OPRID = S1.OPRID
    and S.TREE_NODE_NUM <> S1.TREE_NODE_NUM
    and T.TREE_NODE_NUM between S1.TREE_NODE_NUM
    and S1.TREE_NODE_NUM_END
    and S1.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END)
    and B.EMPLID = A.EMPLID and B.EMPL_RCD_NBR = A.EMPL_RCD_NBR
    and B.SINGLE_CHECK_USE IN ('C', 'N') and (B.REG_HRS<>0 or B.REG_HRLY_EARNS<>0)
    and B.PAY_LINE_STATUS in ('C','F')
    union
    select A.EMPLID,S.OPRID,A.LASTUPDDTTM,A.EMPL_RCD_NBR,A.LAST_NAME,A.FIRST_NAME,A.DEPTID,B.COMPANY,B.PAYGROUP,B.PAY_END_DT,B.OFF_CYCLE,
    B.ERNCD_REG_HRS,B.REG_HRS,0,B.REG_EARNS
    from PS_EMPLOYEES A,PS_PAY_EARNINGS B,PS_SCRTY_TBL_DEPT S,PSTREENODE T
    where S.ACCESS_CD = 'Y' and T.SETID = ' ' and T.TREE_NAME = 'DEPT_SECURITY'
    and T.EFFDT = S.TREE_EFFDT and T.TREE_NODE = A.DEPTID and T.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END
    and not exists (select 'X' from PS_SCRTY_TBL_DEPT S1
    where S.OPRID = S1.OPRID
    and S.TREE_NODE_NUM <> S1.TREE_NODE_NUM
    and T.TREE_NODE_NUM between S1.TREE_NODE_NUM
    and S1.TREE_NODE_NUM_END
    and S1.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END)
    and B.EMPLID = A.EMPLID and B.EMPL_RCD_NBR = A.EMPL_RCD_NBR
    and B.SINGLE_CHECK_USE IN ('C', 'N') and (B.REG_HRS<>0 or B.REG_HRLY_EARNS<>0)
    and B.PAY_LINE_STATUS in ('C','F')
    union
    select A.EMPLID,S.OPRID,A.LASTUPDDTTM,A.EMPL_RCD_NBR,A.LAST_NAME,A.FIRST_NAME,A.DEPTID,B.COMPANY,B.PAYGROUP,B.PAY_END_DT,B.OFF_CYCLE,
    B.ERNCD_OT_HRS,B.OT_HRS,0,B.OT_HRLY_EARNS
    from PS_EMPLOYEES A,PS_PAY_EARNINGS B,PS_SCRTY_TBL_DEPT S,PSTREENODE T
    where S.ACCESS_CD = 'Y' and T.SETID = ' ' and T.TREE_NAME = 'DEPT_SECURITY'
    and T.EFFDT = S.TREE_EFFDT and T.TREE_NODE = A.DEPTID and T.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END
    and not exists (select 'X' from PS_SCRTY_TBL_DEPT S1
    where S.OPRID = S1.OPRID
    and S.TREE_NODE_NUM <> S1.TREE_NODE_NUM
    and T.TREE_NODE_NUM between S1.TREE_NODE_NUM
    and S1.TREE_NODE_NUM_END
    and S1.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END)
    and B.EMPLID = A.EMPLID and B.EMPL_RCD_NBR = A.EMPL_RCD_NBR
    and B.SINGLE_CHECK_USE IN ('C', 'N')
    and (B.OT_HRS<>0 or B.OT_HRLY_EARNS<>0)
    union
    select
    A.EMPLID,S.OPRID,A.LASTUPDDTTM,A.EMPL_RCD_NBR,A.LAST_NAME,A.FIRST_NAME,A.DEPTID,B.COMPANY,B.PAYGROUP,B.PAY_END_DT,B.OFF_CYCLE,
    C.ERNCD,C.OTH_HRS,C.OTH_PAY,C.OTH_EARNS
    from PS_EMPLOYEES A,PS_PAY_EARNINGS B,PS_PAY_OTH_EARNS C,PS_SCRTY_TBL_DEPT S,PSTREENODE T
    where S.ACCESS_CD = 'Y' and T.SETID = ' ' and T.TREE_NAME = 'DEPT_SECURITY'
    and T.EFFDT = S.TREE_EFFDT and T.TREE_NODE = A.DEPTID and T.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END
    and not exists (select 'X' from PS_SCRTY_TBL_DEPT S1
    where S.OPRID = S1.OPRID
    and S.TREE_NODE_NUM <> S1.TREE_NODE_NUM
    and T.TREE_NODE_NUM between S1.TREE_NODE_NUM
    and S1.TREE_NODE_NUM_END
    and S1.TREE_NODE_NUM between S.TREE_NODE_NUM
    and S.TREE_NODE_NUM_END)
    and B.EMPLID = A.EMPLID and B.EMPL_RCD_NBR = A.EMPL_RCD_NBR
    and C.COMPANY = B.COMPANY and C.PAYGROUP = B.PAYGROUP
    and C.PAY_END_DT = B.PAY_END_DT and C.OFF_CYCLE = B.OFF_CYCLE
    and C.PAGE_NBR = B.PAGE_NBR and C.LINE_NBR = B.LINE_NBR
    and C.ADDL_NBR = B.ADDL_NBR and B.SINGLE_CHECK_USE IN ('C', 'N')
    and (C.OTH_HRS<>0 or C.OTH_PAY<>0 or C.OTH_EARNS<>0)
    and B.PAY_LINE_STATUS in ('C','F')

    UNION will eliminate the duplicates. If you are seeing rows that look like duplicates, there must be something different about them that you are missing.
    If there is some set of rows that is common to all of the UNION queries and expensive to retrieve, you might try subquery factoring, e.g:
    WITH common_set AS
        ( SELECT somecols
          FROM   sometab x, othertab y
          WHERE  y.key = x.key
          AND    etc )
    SELECT a,b,c
    FROM   common_set c, table1 t
    WHERE  t.key = c.key
    UNION
    SELECT a,b,c
    FROM   common_set c, table2 t
    WHERE  t.key = c.key
    UNION
    SELECT a,b,c
    FROM   common_set c, table2 t
    WHERE  t.key = c.keyThe usual advice applies for tuning requests applies though:
    <ul><li>{thread:id=863295}</li>
    <li>{thread:id=501834}</li>
    <li>Troubleshooting Bad Execution Plans</li>
    <li>Writing Good SQL</li></ul>

  • Help on SQL tuning

    SQL is behaving different for different inputs. Its taking 20mins to execute when oraganization_id=382 is passed adn with other organization_id's are passed it is taking less than 2 minutes. Execution plan is different in both the cases. I will provide bith sql and execution plans.
    I tried rebuilding the indexes and gather stats for the tables but it did not help out.
    Please help me in tuning the sql.
    1) SQL
    SELECT DISTINCT mp . organization_name , mp . organization_code , mp .
    organization_name , wpsv . pick_slip_number , wpsv .
    from
    subinventory , csu . location shipto_location , wpsv . from_locator_id ,
    wpsv . to_subinventory , wpsv . to_locator_id , mtrh . request_number
    mo_number , wpsv . detailing_date , mtrl . line_id mo_line_id , wpsv .
    transaction_id , wdd . move_order_line_id , to_char ( wdd .
    source_header_id ) order_n_header_char , to_char ( wdd . source_line_id )
    order_n_line_char , wdd . source_header_id , wdd . source_line_id , mtrl .
    line_number mo_line_number , wtr . trip_id , wtr . name rip_name , wnd .
    delivery_id , wnd . name delivery_name , wdd . shipping_instructions , wdd
    . packing_instructions , r . customer_name , wdd . source_header_number ,
    ol . line_number sales_line_number , ol . schedule_ship_date , sum ( NVL (
    wdd . requested_quantity , 0 ) ) requested_quantity , sum ( NVL ( wdd .
    requested_quantity2 , 0 ) ) requested_quantity2 , sum ( NVL ( wdd .
    shipped_quantity , 0 ) ) shipped_quantity , sum ( NVL ( wdd .
    shipped_quantity2 , 0 ) ) shipped_quantity2 , wdd . ship_tolerance_above ,
    wdd . ship_tolerance_below , msi.description item_info , msi . segment1 ,
    msi . description item_description , wdd . inventory_item_id , os .
    set_name , wdd . requested_quantity_uom , wdd . requested_quantity_uom2 ,
    wpsv . transaction_id , wpsv . line_status , wdd . revision , wpsv .
    primary_qty , wpsv . trans_um1 , wpsv . secondary_qty , wpsv . trans_um2 ,
    wpsv . qc_grade , wpsv . lot_id , wpsv . lot_no lot_number , iim .
    attribute19 min_max_stor_temp , wpsv . location , SUBSTR ( wpsv . location ,
    1 , 3 ) lot_location_group FROM IC_ITEM_MST_B iim , WSH_TRIPS wtr ,
    WSH_TRIP_STOPS wts , WSH_DELIVERY_LEGS wlg , WSH_NEW_DELIVERIES wnd ,
    WSH_DELIVERY_ASSIGNMENTS wda , WSH_DELIVERY_DETAILS wdd ,
    GMI_WSH_PICK_SLIP_V wpsv , OE_ORDER_LINES_ALL ol , OE_SETS os ,
    RA_CUSTOMERS r , IC_TXN_REQUEST_HEADERS mtrh , IC_TXN_REQUEST_LINES mtrl ,
    MTL_SYSTEM_ITEMS msi , HZ_CUST_SITE_USES_ALL csu , OE_ORDER_HEADERS_ALL ooh
    , ( SELECT mp . organization_id , mp . organization_code , hou . name
    organization_name FROM HR_ORGANIZATION_UNITS hou , MTL_PARAMETERS mp where
    mp . organization_id = hou . organization_id ) mp WHERE wtr.trip_id (+) =
    wts.trip_id AND wts.stop_id (+) = wlg.pick_up_stop_id AND wlg.delivery_id
    (+) = wnd.delivery_id AND wnd.delivery_id (+) = wda.delivery_id AND
    wdd.source_header_id = ooh.header_id AND csu.site_use_id =
    ooh.ship_to_org_id AND os.set_id (+) = wdd.ship_set_id AND
    wda.delivery_detail_id = wdd.delivery_detail_id AND ol.line_id =
    wdd.source_line_id AND wpsv.transaction_id = ol.line_id AND
    wpsv.transaction_id = wdd.source_line_id AND r.customer_id =
    wdd.customer_id AND mtrl.header_id = mtrh.header_id AND mtrl.line_id =
    wdd.move_order_line_id AND mtrl.inventory_item_id = wpsv.inventory_item_id
    AND mtrl.line_id = wpsv.move_order_line_id AND msi.inventory_item_id =
    wdd.inventory_item_id AND msi.organization_id = mtrl.organization_id AND
    iim.item_no = msi.segment1 AND mtrl.organization_id = : p_organization_id
    AND mtrl.organization_id = mp.organization_id and mtrh.request_number
    between : p_move_order_low and : p_move_order_high and wpsv.line_status =
    'UNPICKED' GROUP BY mp.organization_name , mp.organization_code ,
    csu.location , wpsv.pick_slip_number , wpsv.from_subinventory ,
    wpsv.from_locator_id , wpsv.to_subinventory , wpsv.to_locator_id ,
    mtrh.request_number , wpsv.detailing_date , mtrl.line_id ,
    wpsv.transaction_id , wdd.move_order_line_id , to_char (
    wdd.source_header_id ) , to_char ( wdd.source_line_id ) ,
    wdd.source_header_id , wdd.source_line_id , mtrl.line_number , wtr.trip_id ,
    wtr.name , wnd.delivery_id , wnd.name , wdd.shipping_instructions ,
    wdd.packing_instructions , r.customer_name , wdd.source_header_number ,
    ol.line_number , ol.schedule_ship_date , wdd.ship_tolerance_above ,
    wdd.ship_tolerance_below , msi.description , msi.segment1 , msi.description
    , wdd.inventory_item_id , os.set_name , wdd.requested_quantity_uom ,
    wdd.requested_quantity_uom2 , wpsv.transaction_id , wpsv.line_status ,
    wdd.revision , wpsv.primary_qty , wpsv.trans_um1 , wpsv.secondary_qty ,
    wpsv.trans_um2 , wpsv.qc_grade , wpsv.lot_id , wpsv.lot_no ,
    iim.attribute19 , wpsv.location , SUBSTR ( wpsv.location , 1 , 3 ) ORDER BY
    1 ASC,2 ASC,10 ASC,6 ASC,4 ASC,17 ASC,5 ASC,7 ASC,8 ASC,9 ASC,13 ASC,12 ASC,
    55 ASC,43 ASC,14 ASC,19 ASC,18 ASC,39 ASC,53 ASC,29 ASC,36 ASC,37 ASC,38
    ASC,30 ASC,41 ASC,31 ASC,42 ASC,40 ASC,33 ASC,34 ASC,35 ASC,27 ASC,28 ASC,
    20 ASC,21 ASC,22 ASC,23 ASC,26 ASC,24 ASC,25 ASC , mp.organization_code ,
    mo_number , pick_slip_number , mo_line_number , item_info
    2) Explain plan when the query is taking less than a minute
    Rows Row Source Operation
    1 SORT UNIQUE (cr=1299 r=87 w=0 time=50693 us)
    1 SORT GROUP BY (cr=1299 r=87 w=0 time=50475 us)
    1 FILTER (cr=1299 r=87 w=0 time=50009 us)
    1 NESTED LOOPS (cr=1299 r=87 w=0 time=50002 us)
    1 NESTED LOOPS (cr=1296 r=87 w=0 time=49937 us)
    1 NESTED LOOPS (cr=1293 r=87 w=0 time=49873 us)
    1 NESTED LOOPS (cr=1290 r=87 w=0 time=49799 us)
    1 NESTED LOOPS (cr=1287 r=87 w=0 time=49740 us)
    1 NESTED LOOPS OUTER (cr=1284 r=87 w=0 time=49676 us)
    1 NESTED LOOPS OUTER (cr=1284 r=87 w=0 time=49654 us)
    1 NESTED LOOPS OUTER (cr=1284 r=87 w=0 time=49633 us)
    1 NESTED LOOPS OUTER (cr=1282 r=87 w=0 time=49566 us)
    1 NESTED LOOPS (cr=1279 r=87 w=0 time=49490 us)
    1 NESTED LOOPS (cr=1276 r=87 w=0 time=49381 us)
    1 NESTED LOOPS (cr=1273 r=87 w=0 time=49287 us)
    1 NESTED LOOPS OUTER (cr=1270 r=87 w=0 time=49209 us)
    1 NESTED LOOPS (cr=1270 r=87 w=0 time=49182 us)
    1 NESTED LOOPS (cr=1267 r=87 w=0 time=49043 us)
    1 NESTED LOOPS (cr=19 r=0 w=0 time=4308 us)
    1 NESTED LOOPS (cr=16 r=0 w=0 time=4207 us)
    1 NESTED LOOPS (cr=13 r=0 w=0 time=4076 us)
    1 NESTED LOOPS (cr=5 r=0 w=0 time=781 us)
    1 TABLE ACCESS BY INDEX ROWID MTL_PARAMETERS (cr=2 r=0 w=0 time=111 us)
    1 INDEX UNIQUE SCAN MTL_PARAMETERS_U1 (cr=1 r=0 w=0 time=60 us)(object id 37657)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS_TL (cr=3 r=0 w=0 time=636 us)
    1 INDEX UNIQUE SCAN HR_ALL_ORGANIZATION_UNTS_TL_PK (cr=2 r=0 w=0 time=605 us)(object id 44637)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS (cr=8 r=0 w=0 time=3278 us)
    1 INDEX UNIQUE SCAN HR_ORGANIZATION_UNITS_PK (cr=1 r=0 w=0 time=31 us)(object id 43498)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_HEADERS (cr=3 r=0 w=0 time=104 us)
    1 INDEX RANGE SCAN IC_TXN_REQUEST_HEADERS_U1 (cr=2 r=0 w=0 time=67 us)(object id 637615)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=3 r=0 w=0 time=84 us)
    1 INDEX RANGE SCAN IC_TXN_REQUEST_LINES_U1 (cr=2 r=0 w=0 time=53 us)(object id 637626)
    1 VIEW (cr=1248 r=87 w=0 time=44702 us)
    1 UNION-ALL PARTITION (cr=1248 r=87 w=0 time=44669 us)
    1 NESTED LOOPS (cr=1248 r=87 w=0 time=44561 us)
    1 NESTED LOOPS (cr=1244 r=87 w=0 time=44429 us)
    1 NESTED LOOPS (cr=13 r=0 w=0 time=322 us)
    1 NESTED LOOPS (cr=11 r=0 w=0 time=281 us)
    1 NESTED LOOPS (cr=9 r=0 w=0 time=228 us)
    1 NESTED LOOPS (cr=6 r=0 w=0 time=156 us)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=3 r=0 w=0 time=64 us)
    1 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=2 r=0 w=0 time=38 us)(object id 637620)
    1 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=3 r=0 w=0 time=74 us)
    1 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=2 r=0 w=0 time=41 us)(object id 38017)
    1 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=3 r=0 w=0 time=60 us)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=2 r=0 w=0 time=38 us)(object id 637202)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_TL_PK (cr=2 r=0 w=0 time=33 us)(object id 637049)
    1 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=2 r=0 w=0 time=33 us)(object id 637610)
    1 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=1231 r=87 w=0 time=44078 us)
    1907 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=10 r=0 w=0 time=3000 us)(object id 222381)
    1 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=4 r=0 w=0 time=90 us)
    1 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=3 r=0 w=0 time=60 us)(object id 637681)
    0 FILTER (cr=0 r=0 w=0 time=2 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=0 r=0 w=0 time=0 us)(object id 637620)
    0 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=0 r=0 w=0 time=0 us)(object id 38017)
    0 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=0 r=0 w=0 time=0 us)(object id 637202)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=0 r=0 w=0 time=0 us)(object id 637610)
    0 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=0 r=0 w=0 time=0 us)
    0 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=0 r=0 w=0 time=0 us)(object id 222381)
    0 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=0 r=0 w=0 time=0 us)(object id 637681)
    1 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_DETAILS (cr=3 r=0 w=0 time=106 us)
    1 INDEX RANGE SCAN WSH_DELIVERY_DETAILS_N3 (cr=2 r=0 w=0 time=65 us)(object id 46264)
    0 TABLE ACCESS BY INDEX ROWID OE_SETS (cr=0 r=0 w=0 time=3 us)
    0 INDEX UNIQUE SCAN OE_SETS_U1 (cr=0 r=0 w=0 time=1 us)(object id 43138)
    1 TABLE ACCESS BY INDEX ROWID OE_ORDER_LINES_ALL (cr=3 r=0 w=0 time=58 us)
    1 INDEX UNIQUE SCAN OE_ORDER_LINES_U1 (cr=2 r=0 w=0 time=35 us)(object id 42102)
    1 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=3 r=0 w=0 time=67 us)
    1 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=2 r=0 w=0 time=41 us)(object id 38017)
    1 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_ASSIGNMENTS (cr=3 r=0 w=0 time=87 us)
    1 INDEX RANGE SCAN WSH_DELIVERY_ASSIGNMENTS_N3 (cr=2 r=0 w=0 time=65 us)(object id 46295)
    1 TABLE ACCESS BY INDEX ROWID WSH_NEW_DELIVERIES (cr=3 r=0 w=0 time=49____
    1 INDEX UNIQUE SCAN WSH_NEW_DELIVERIES_U1 (cr=2 r=0 w=0 time=28 us)(object id 46306)
    0 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_LEGS (cr=2 r=0 w=0 time=37 us)
    0 INDEX RANGE SCAN WSH_DELIVERY_LEGS_N1 (cr=2 r=0 w=0 time=35 us)(object id 46235)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIP_STOPS (cr=0 r=0 w=0 time=3 us)
    0 INDEX UNIQUE SCAN WSH_TRIP_STOPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46088)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIPS (cr=0 r=0 w=0 time=2 us)
    0 INDEX UNIQUE SCAN WSH_TRIPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46057)
    1 TABLE ACCESS BY INDEX ROWID OE_ORDER_HEADERS_ALL (cr=3 r=0 w=0 time=47 us)
    1 INDEX UNIQUE SCAN OE_ORDER_HEADERS_U1 (cr=2 r=0 w=0 time=27 us)(object id 41952)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=3 r=0 w=0 time=44 us)
    1 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=2 r=0 w=0 time=29 us)(object id 236588)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=3 r=0 w=0 time=52 us)
    1 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=2 r=0 w=0 time=34 us)(object id 81600)
    1 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=3 r=0 w=0 time=46 us)
    1 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=2 r=0 w=0 time=28 us)(object id 237346)
    1 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=3 r=0 w=0 time=41 us)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=2 r=0 w=0 time=24 us)(object id 637202)
    3) Explain plan when the query is taking 22mins (for org_id=382)
    Rows Row Source Operation
    1 SORT UNIQUE (cr=72660499 r=624 w=0 time=1327230739 us)
    1 SORT GROUP BY (cr=72660499 r=624 w=0 time=1327230336 us)
    1 FILTER (cr=72660499 r=624 w=0 time=1327229578 us)
    1 NESTED LOOPS (cr=72660499 r=624 w=0 time=1327229570 us)
    1 NESTED LOOPS (cr=72660496 r=623 w=0 time=1327217298 us)
    1 NESTED LOOPS (cr=72660493 r=623 w=0 time=1327217219 us)
    1 NESTED LOOPS OUTER (cr=72660490 r=623 w=0 time=1327217140 us)
    1 NESTED LOOPS OUTER (cr=72660490 r=623 w=0 time=1327217118 us)
    1 NESTED LOOPS OUTER (cr=72660490 r=623 w=0 time=1327217092 us)
    1 NESTED LOOPS OUTER (cr=72660488 r=623 w=0 time=1327217006 us)
    1 NESTED LOOPS (cr=72660485 r=622 w=0 time=1327207555 us)
    1 NESTED LOOPS (cr=72660480 r=621 w=0 time=1327207177 us)
    1 NESTED LOOPS (cr=72660477 r=621 w=0 time=1327207115 us)
    1 NESTED LOOPS (cr=72660474 r=621 w=0 time=1327207034 us)
    1 NESTED LOOPS (cr=72660471 r=621 w=0 time=1327206951 us)
    7 NESTED LOOPS (cr=72660455 r=619 w=0 time=1327206132 us)
    7 NESTED LOOPS OUTER (cr=72660439 r=613 w=0 time=1327203302 us)
    7 NESTED LOOPS (cr=72660439 r=613 w=0 time=1327203073 us)
    14 NESTED LOOPS (cr=72660407 r=610 w=0 time=1327200108 us)
    90549 NESTED LOOPS (cr=3359 r=455 w=0 time=1818230 us)
    1 NESTED LOOPS (cr=13 r=0 w=0 time=4463 us)
    1 NESTED LOOPS (cr=5 r=0 w=0 time=962 us)
    1 TABLE ACCESS BY INDEX ROWID MTL_PARAMETERS (cr=2 r=0 w=0 time=107 us)
    1 INDEX UNIQUE SCAN MTL_PARAMETERS_U1 (cr=1 r=0 w=0 time=55 us)(object id 37657)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS_TL (cr=3 r=0 w=0 time=820 us)
    1 INDEX UNIQUE SCAN HR_ALL_ORGANIZATION_UNTS_TL_PK (cr=2 r=0 w=0 time=786 us)(object id 44637)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS (cr=8 r=0 w=0 time=3479 us)
    1 INDEX UNIQUE SCAN HR_ORGANIZATION_UNITS_PK (cr=1 r=0 w=0 time=23 us)(object id 43498)
    90549 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=3346 r=455 w=0 time=1704106 us)
    90549 INDEX RANGE SCAN IC_TXN_REQUEST_LINES_N1 (cr=210 r=41 w=0 time=694480 us)(object id 133389)
    14 VIEW (cr=72657048 r=155 w=0 time=1324006502 us)
    14 UNION-ALL PARTITION (cr=72657048 r=155 w=0 time=1322889352 us)
    14 NESTED LOOPS (cr=72657048 r=155 w=0 time=1318179989 us)
    14 NESTED LOOPS (cr=72656992 r=151 w=0 time=1318005647 us)
    90549 NESTED LOOPS (cr=1177138 r=52 w=0 time=20959096 us)
    90549 NESTED LOOPS (cr=996040 r=51 w=0 time=18410001 us)
    90549 NESTED LOOPS (cr=814942 r=51 w=0 time=15523206 us)
    90549 NESTED LOOPS (cr=543294 r=50 w=0 time=10944790 us)
    90549 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=271647 r=1 w=0 time=5063466 us)
    90549 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=181098 r=1 w=0 time=2863447 us)(object id 637620)
    90549 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=271647 r=49 w=0 time=4794430 us)
    90549 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=181098 r=1 w=0 time=2553802 us)(object id 38017)
    90549 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=271648 r=1 w=0 time=3786267 us)
    90549 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=181098 r=0 w=0 time=2164425 us)(object id 637202)
    90549 INDEX UNIQUE SCAN IC_ITEM_MST_TL_PK (cr=181098 r=0 w=0 time=2260794 us)(object id 637049)
    90549 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=181098 r=1 w=0 time=1902881 us)(object id 637610)
    14 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=71479854 r=99 w=0 time=1295671259 us)
    101213210 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=676173 r=21 w=0 time=136382307 us)(object id 222381)
    14 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=56 r=4 w=0 time=2052 us)
    14 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=42 r=2 w=0 time=1265 us)(object id 637681)
    0 FILTER (cr=0 r=0 w=0 time=59787 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=0 r=0 w=0 time=0 us)(object id 637620)
    0 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=0 r=0 w=0 time=0 us)(object id 38017)
    0 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=0 r=0 w=0 time=0 us)(object id 637202)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=0 r=0 w=0 time=0 us)(object id 637610)
    0 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=0 r=0 w=0 time=0 us)
    0 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=0 r=0 w=0 time=0 us)(object id 222381)
    0 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=0 r=0 w=0 time=0 us)(object id 637681)
    7 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_DETAILS (cr=32 r=3 w=0 time=2586 us)
    14 INDEX RANGE SCAN WSH_DELIVERY_DETAILS_N3 (cr=16 r=1 w=0 time=1221 us)(object id 46264)
    0 TABLE ACCESS BY INDEX ROWID OE_SETS (cr=0 r=0 w=0 time=32 us)
    0 INDEX UNIQUE SCAN OE_SETS_U1 (cr=0 r=0 w=0 time=6 us)(object id 43138)
    7 TABLE ACCESS BY INDEX ROWID OE_ORDER_LINES_ALL (cr=16 r=6 w=0 time=2715 us)
    7 INDEX UNIQUE SCAN OE_ORDER_LINES_U1 (cr=9 r=2 w=0 time=1399 us)(object id 42102)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_HEADERS (cr=16 r=2 w=0 time=679 us)
    7 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=9 r=0 w=0 time=124 us)(object id 637610)
    1 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=3 r=0 w=0 time=50 us)
    1 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=2 r=0 w=0 time=23 us)(object id 38017)
    1 TABLE ACCESS BY INDEX ROWID OE_ORDER_HEADERS_ALL (cr=3 r=0 w=0 time=58 us)
    1 INDEX UNIQUE SCAN OE_ORDER_HEADERS_U1 (cr=2 r=0 w=0 time=36 us)(object id 41952)
    1 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=3 r=0 w=0 time=33 us)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=2 r=0 w=0 time=14 us)(object id 637202)
    1 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_ASSIGNMENTS (cr=5 r=1 w=0 time=358 us)
    1 INDEX RANGE SCAN WSH_DELIVERY_ASSIGNMENTS_N3 (cr=2 r=0 w=0 time=68 us)(object id 46295)
    1 TABLE ACCESS BY INDEX ROWID WSH_NEW_DELIVERIES (cr=3 r=1 w=0 time=9428 us)
    1 INDEX UNIQUE SCAN WSH_NEW_DELIVERIES_U1 (cr=2 r=1 w=0 time=9405 us)(object id 46306)
    0 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_LEGS (cr=2 r=0 w=0 time=58 us)
    0 INDEX RANGE SCAN WSH_DELIVERY_LEGS_N1 (cr=2 r=0 w=0 time=55 us)(object id 46235)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIP_STOPS (cr=0 r=0 w=0 time=2 us)
    0 INDEX UNIQUE SCAN WSH_TRIP_STOPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46088)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIPS (cr=0 r=0 w=0 time=1 us)
    0 INDEX UNIQUE SCAN WSH_TRIPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46057)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=3 r=0 w=0 time=55 us)
    1 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=2 r=0 w=0 time=34 us)(object id 81600)
    1 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=3 r=0 w=0 time=48 us)
    1 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=2 r=0 w=0 time=26 us)(object id 237346)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=3 r=1 w=0 time=12241 us)
    1 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=2 r=1 w=0 time=12187 us)(object id 236588)

    SQL is behaving different for different inputs. Its taking 20mins to execute when oraganization_id=382 is passed adn with other organization_id's are passed it is taking less than 2 minutes. Execution plan is different in both the cases. I will provide bith sql and execution plans.
    I tried rebuilding the indexes and gather stats for the tables but it did not help out.
    Please help me in tuning the sql.
    1) SQL
    SELECT DISTINCT mp . organization_name , mp . organization_code , mp .
    organization_name , wpsv . pick_slip_number , wpsv .
    from
    subinventory , csu . location shipto_location , wpsv . from_locator_id ,
    wpsv . to_subinventory , wpsv . to_locator_id , mtrh . request_number
    mo_number , wpsv . detailing_date , mtrl . line_id mo_line_id , wpsv .
    transaction_id , wdd . move_order_line_id , to_char ( wdd .
    source_header_id ) order_n_header_char , to_char ( wdd . source_line_id )
    order_n_line_char , wdd . source_header_id , wdd . source_line_id , mtrl .
    line_number mo_line_number , wtr . trip_id , wtr . name rip_name , wnd .
    delivery_id , wnd . name delivery_name , wdd . shipping_instructions , wdd
    . packing_instructions , r . customer_name , wdd . source_header_number ,
    ol . line_number sales_line_number , ol . schedule_ship_date , sum ( NVL (
    wdd . requested_quantity , 0 ) ) requested_quantity , sum ( NVL ( wdd .
    requested_quantity2 , 0 ) ) requested_quantity2 , sum ( NVL ( wdd .
    shipped_quantity , 0 ) ) shipped_quantity , sum ( NVL ( wdd .
    shipped_quantity2 , 0 ) ) shipped_quantity2 , wdd . ship_tolerance_above ,
    wdd . ship_tolerance_below , msi.description item_info , msi . segment1 ,
    msi . description item_description , wdd . inventory_item_id , os .
    set_name , wdd . requested_quantity_uom , wdd . requested_quantity_uom2 ,
    wpsv . transaction_id , wpsv . line_status , wdd . revision , wpsv .
    primary_qty , wpsv . trans_um1 , wpsv . secondary_qty , wpsv . trans_um2 ,
    wpsv . qc_grade , wpsv . lot_id , wpsv . lot_no lot_number , iim .
    attribute19 min_max_stor_temp , wpsv . location , SUBSTR ( wpsv . location ,
    1 , 3 ) lot_location_group FROM IC_ITEM_MST_B iim , WSH_TRIPS wtr ,
    WSH_TRIP_STOPS wts , WSH_DELIVERY_LEGS wlg , WSH_NEW_DELIVERIES wnd ,
    WSH_DELIVERY_ASSIGNMENTS wda , WSH_DELIVERY_DETAILS wdd ,
    GMI_WSH_PICK_SLIP_V wpsv , OE_ORDER_LINES_ALL ol , OE_SETS os ,
    RA_CUSTOMERS r , IC_TXN_REQUEST_HEADERS mtrh , IC_TXN_REQUEST_LINES mtrl ,
    MTL_SYSTEM_ITEMS msi , HZ_CUST_SITE_USES_ALL csu , OE_ORDER_HEADERS_ALL ooh
    , ( SELECT mp . organization_id , mp . organization_code , hou . name
    organization_name FROM HR_ORGANIZATION_UNITS hou , MTL_PARAMETERS mp where
    mp . organization_id = hou . organization_id ) mp WHERE wtr.trip_id (+) =
    wts.trip_id AND wts.stop_id (+) = wlg.pick_up_stop_id AND wlg.delivery_id
    (+) = wnd.delivery_id AND wnd.delivery_id (+) = wda.delivery_id AND
    wdd.source_header_id = ooh.header_id AND csu.site_use_id =
    ooh.ship_to_org_id AND os.set_id (+) = wdd.ship_set_id AND
    wda.delivery_detail_id = wdd.delivery_detail_id AND ol.line_id =
    wdd.source_line_id AND wpsv.transaction_id = ol.line_id AND
    wpsv.transaction_id = wdd.source_line_id AND r.customer_id =
    wdd.customer_id AND mtrl.header_id = mtrh.header_id AND mtrl.line_id =
    wdd.move_order_line_id AND mtrl.inventory_item_id = wpsv.inventory_item_id
    AND mtrl.line_id = wpsv.move_order_line_id AND msi.inventory_item_id =
    wdd.inventory_item_id AND msi.organization_id = mtrl.organization_id AND
    iim.item_no = msi.segment1 AND mtrl.organization_id = : p_organization_id
    AND mtrl.organization_id = mp.organization_id and mtrh.request_number
    between : p_move_order_low and : p_move_order_high and wpsv.line_status =
    'UNPICKED' GROUP BY mp.organization_name , mp.organization_code ,
    csu.location , wpsv.pick_slip_number , wpsv.from_subinventory ,
    wpsv.from_locator_id , wpsv.to_subinventory , wpsv.to_locator_id ,
    mtrh.request_number , wpsv.detailing_date , mtrl.line_id ,
    wpsv.transaction_id , wdd.move_order_line_id , to_char (
    wdd.source_header_id ) , to_char ( wdd.source_line_id ) ,
    wdd.source_header_id , wdd.source_line_id , mtrl.line_number , wtr.trip_id ,
    wtr.name , wnd.delivery_id , wnd.name , wdd.shipping_instructions ,
    wdd.packing_instructions , r.customer_name , wdd.source_header_number ,
    ol.line_number , ol.schedule_ship_date , wdd.ship_tolerance_above ,
    wdd.ship_tolerance_below , msi.description , msi.segment1 , msi.description
    , wdd.inventory_item_id , os.set_name , wdd.requested_quantity_uom ,
    wdd.requested_quantity_uom2 , wpsv.transaction_id , wpsv.line_status ,
    wdd.revision , wpsv.primary_qty , wpsv.trans_um1 , wpsv.secondary_qty ,
    wpsv.trans_um2 , wpsv.qc_grade , wpsv.lot_id , wpsv.lot_no ,
    iim.attribute19 , wpsv.location , SUBSTR ( wpsv.location , 1 , 3 ) ORDER BY
    1 ASC,2 ASC,10 ASC,6 ASC,4 ASC,17 ASC,5 ASC,7 ASC,8 ASC,9 ASC,13 ASC,12 ASC,
    55 ASC,43 ASC,14 ASC,19 ASC,18 ASC,39 ASC,53 ASC,29 ASC,36 ASC,37 ASC,38
    ASC,30 ASC,41 ASC,31 ASC,42 ASC,40 ASC,33 ASC,34 ASC,35 ASC,27 ASC,28 ASC,
    20 ASC,21 ASC,22 ASC,23 ASC,26 ASC,24 ASC,25 ASC , mp.organization_code ,
    mo_number , pick_slip_number , mo_line_number , item_info
    2) Explain plan when the query is taking less than a minute
    Rows Row Source Operation
    1 SORT UNIQUE (cr=1299 r=87 w=0 time=50693 us)
    1 SORT GROUP BY (cr=1299 r=87 w=0 time=50475 us)
    1 FILTER (cr=1299 r=87 w=0 time=50009 us)
    1 NESTED LOOPS (cr=1299 r=87 w=0 time=50002 us)
    1 NESTED LOOPS (cr=1296 r=87 w=0 time=49937 us)
    1 NESTED LOOPS (cr=1293 r=87 w=0 time=49873 us)
    1 NESTED LOOPS (cr=1290 r=87 w=0 time=49799 us)
    1 NESTED LOOPS (cr=1287 r=87 w=0 time=49740 us)
    1 NESTED LOOPS OUTER (cr=1284 r=87 w=0 time=49676 us)
    1 NESTED LOOPS OUTER (cr=1284 r=87 w=0 time=49654 us)
    1 NESTED LOOPS OUTER (cr=1284 r=87 w=0 time=49633 us)
    1 NESTED LOOPS OUTER (cr=1282 r=87 w=0 time=49566 us)
    1 NESTED LOOPS (cr=1279 r=87 w=0 time=49490 us)
    1 NESTED LOOPS (cr=1276 r=87 w=0 time=49381 us)
    1 NESTED LOOPS (cr=1273 r=87 w=0 time=49287 us)
    1 NESTED LOOPS OUTER (cr=1270 r=87 w=0 time=49209 us)
    1 NESTED LOOPS (cr=1270 r=87 w=0 time=49182 us)
    1 NESTED LOOPS (cr=1267 r=87 w=0 time=49043 us)
    1 NESTED LOOPS (cr=19 r=0 w=0 time=4308 us)
    1 NESTED LOOPS (cr=16 r=0 w=0 time=4207 us)
    1 NESTED LOOPS (cr=13 r=0 w=0 time=4076 us)
    1 NESTED LOOPS (cr=5 r=0 w=0 time=781 us)
    1 TABLE ACCESS BY INDEX ROWID MTL_PARAMETERS (cr=2 r=0 w=0 time=111 us)
    1 INDEX UNIQUE SCAN MTL_PARAMETERS_U1 (cr=1 r=0 w=0 time=60 us)(object id 37657)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS_TL (cr=3 r=0 w=0 time=636 us)
    1 INDEX UNIQUE SCAN HR_ALL_ORGANIZATION_UNTS_TL_PK (cr=2 r=0 w=0 time=605 us)(object id 44637)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS (cr=8 r=0 w=0 time=3278 us)
    1 INDEX UNIQUE SCAN HR_ORGANIZATION_UNITS_PK (cr=1 r=0 w=0 time=31 us)(object id 43498)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_HEADERS (cr=3 r=0 w=0 time=104 us)
    1 INDEX RANGE SCAN IC_TXN_REQUEST_HEADERS_U1 (cr=2 r=0 w=0 time=67 us)(object id 637615)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=3 r=0 w=0 time=84 us)
    1 INDEX RANGE SCAN IC_TXN_REQUEST_LINES_U1 (cr=2 r=0 w=0 time=53 us)(object id 637626)
    1 VIEW (cr=1248 r=87 w=0 time=44702 us)
    1 UNION-ALL PARTITION (cr=1248 r=87 w=0 time=44669 us)
    1 NESTED LOOPS (cr=1248 r=87 w=0 time=44561 us)
    1 NESTED LOOPS (cr=1244 r=87 w=0 time=44429 us)
    1 NESTED LOOPS (cr=13 r=0 w=0 time=322 us)
    1 NESTED LOOPS (cr=11 r=0 w=0 time=281 us)
    1 NESTED LOOPS (cr=9 r=0 w=0 time=228 us)
    1 NESTED LOOPS (cr=6 r=0 w=0 time=156 us)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=3 r=0 w=0 time=64 us)
    1 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=2 r=0 w=0 time=38 us)(object id 637620)
    1 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=3 r=0 w=0 time=74 us)
    1 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=2 r=0 w=0 time=41 us)(object id 38017)
    1 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=3 r=0 w=0 time=60 us)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=2 r=0 w=0 time=38 us)(object id 637202)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_TL_PK (cr=2 r=0 w=0 time=33 us)(object id 637049)
    1 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=2 r=0 w=0 time=33 us)(object id 637610)
    1 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=1231 r=87 w=0 time=44078 us)
    1907 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=10 r=0 w=0 time=3000 us)(object id 222381)
    1 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=4 r=0 w=0 time=90 us)
    1 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=3 r=0 w=0 time=60 us)(object id 637681)
    0 FILTER (cr=0 r=0 w=0 time=2 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=0 r=0 w=0 time=0 us)(object id 637620)
    0 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=0 r=0 w=0 time=0 us)(object id 38017)
    0 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=0 r=0 w=0 time=0 us)(object id 637202)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=0 r=0 w=0 time=0 us)(object id 637610)
    0 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=0 r=0 w=0 time=0 us)
    0 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=0 r=0 w=0 time=0 us)(object id 222381)
    0 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=0 r=0 w=0 time=0 us)(object id 637681)
    1 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_DETAILS (cr=3 r=0 w=0 time=106 us)
    1 INDEX RANGE SCAN WSH_DELIVERY_DETAILS_N3 (cr=2 r=0 w=0 time=65 us)(object id 46264)
    0 TABLE ACCESS BY INDEX ROWID OE_SETS (cr=0 r=0 w=0 time=3 us)
    0 INDEX UNIQUE SCAN OE_SETS_U1 (cr=0 r=0 w=0 time=1 us)(object id 43138)
    1 TABLE ACCESS BY INDEX ROWID OE_ORDER_LINES_ALL (cr=3 r=0 w=0 time=58 us)
    1 INDEX UNIQUE SCAN OE_ORDER_LINES_U1 (cr=2 r=0 w=0 time=35 us)(object id 42102)
    1 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=3 r=0 w=0 time=67 us)
    1 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=2 r=0 w=0 time=41 us)(object id 38017)
    1 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_ASSIGNMENTS (cr=3 r=0 w=0 time=87 us)
    1 INDEX RANGE SCAN WSH_DELIVERY_ASSIGNMENTS_N3 (cr=2 r=0 w=0 time=65 us)(object id 46295)
    1 TABLE ACCESS BY INDEX ROWID WSH_NEW_DELIVERIES (cr=3 r=0 w=0 time=49____
    1 INDEX UNIQUE SCAN WSH_NEW_DELIVERIES_U1 (cr=2 r=0 w=0 time=28 us)(object id 46306)
    0 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_LEGS (cr=2 r=0 w=0 time=37 us)
    0 INDEX RANGE SCAN WSH_DELIVERY_LEGS_N1 (cr=2 r=0 w=0 time=35 us)(object id 46235)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIP_STOPS (cr=0 r=0 w=0 time=3 us)
    0 INDEX UNIQUE SCAN WSH_TRIP_STOPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46088)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIPS (cr=0 r=0 w=0 time=2 us)
    0 INDEX UNIQUE SCAN WSH_TRIPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46057)
    1 TABLE ACCESS BY INDEX ROWID OE_ORDER_HEADERS_ALL (cr=3 r=0 w=0 time=47 us)
    1 INDEX UNIQUE SCAN OE_ORDER_HEADERS_U1 (cr=2 r=0 w=0 time=27 us)(object id 41952)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=3 r=0 w=0 time=44 us)
    1 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=2 r=0 w=0 time=29 us)(object id 236588)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=3 r=0 w=0 time=52 us)
    1 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=2 r=0 w=0 time=34 us)(object id 81600)
    1 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=3 r=0 w=0 time=46 us)
    1 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=2 r=0 w=0 time=28 us)(object id 237346)
    1 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=3 r=0 w=0 time=41 us)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=2 r=0 w=0 time=24 us)(object id 637202)
    3) Explain plan when the query is taking 22mins (for org_id=382)
    Rows Row Source Operation
    1 SORT UNIQUE (cr=72660499 r=624 w=0 time=1327230739 us)
    1 SORT GROUP BY (cr=72660499 r=624 w=0 time=1327230336 us)
    1 FILTER (cr=72660499 r=624 w=0 time=1327229578 us)
    1 NESTED LOOPS (cr=72660499 r=624 w=0 time=1327229570 us)
    1 NESTED LOOPS (cr=72660496 r=623 w=0 time=1327217298 us)
    1 NESTED LOOPS (cr=72660493 r=623 w=0 time=1327217219 us)
    1 NESTED LOOPS OUTER (cr=72660490 r=623 w=0 time=1327217140 us)
    1 NESTED LOOPS OUTER (cr=72660490 r=623 w=0 time=1327217118 us)
    1 NESTED LOOPS OUTER (cr=72660490 r=623 w=0 time=1327217092 us)
    1 NESTED LOOPS OUTER (cr=72660488 r=623 w=0 time=1327217006 us)
    1 NESTED LOOPS (cr=72660485 r=622 w=0 time=1327207555 us)
    1 NESTED LOOPS (cr=72660480 r=621 w=0 time=1327207177 us)
    1 NESTED LOOPS (cr=72660477 r=621 w=0 time=1327207115 us)
    1 NESTED LOOPS (cr=72660474 r=621 w=0 time=1327207034 us)
    1 NESTED LOOPS (cr=72660471 r=621 w=0 time=1327206951 us)
    7 NESTED LOOPS (cr=72660455 r=619 w=0 time=1327206132 us)
    7 NESTED LOOPS OUTER (cr=72660439 r=613 w=0 time=1327203302 us)
    7 NESTED LOOPS (cr=72660439 r=613 w=0 time=1327203073 us)
    14 NESTED LOOPS (cr=72660407 r=610 w=0 time=1327200108 us)
    90549 NESTED LOOPS (cr=3359 r=455 w=0 time=1818230 us)
    1 NESTED LOOPS (cr=13 r=0 w=0 time=4463 us)
    1 NESTED LOOPS (cr=5 r=0 w=0 time=962 us)
    1 TABLE ACCESS BY INDEX ROWID MTL_PARAMETERS (cr=2 r=0 w=0 time=107 us)
    1 INDEX UNIQUE SCAN MTL_PARAMETERS_U1 (cr=1 r=0 w=0 time=55 us)(object id 37657)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS_TL (cr=3 r=0 w=0 time=820 us)
    1 INDEX UNIQUE SCAN HR_ALL_ORGANIZATION_UNTS_TL_PK (cr=2 r=0 w=0 time=786 us)(object id 44637)
    1 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS (cr=8 r=0 w=0 time=3479 us)
    1 INDEX UNIQUE SCAN HR_ORGANIZATION_UNITS_PK (cr=1 r=0 w=0 time=23 us)(object id 43498)
    90549 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=3346 r=455 w=0 time=1704106 us)
    90549 INDEX RANGE SCAN IC_TXN_REQUEST_LINES_N1 (cr=210 r=41 w=0 time=694480 us)(object id 133389)
    14 VIEW (cr=72657048 r=155 w=0 time=1324006502 us)
    14 UNION-ALL PARTITION (cr=72657048 r=155 w=0 time=1322889352 us)
    14 NESTED LOOPS (cr=72657048 r=155 w=0 time=1318179989 us)
    14 NESTED LOOPS (cr=72656992 r=151 w=0 time=1318005647 us)
    90549 NESTED LOOPS (cr=1177138 r=52 w=0 time=20959096 us)
    90549 NESTED LOOPS (cr=996040 r=51 w=0 time=18410001 us)
    90549 NESTED LOOPS (cr=814942 r=51 w=0 time=15523206 us)
    90549 NESTED LOOPS (cr=543294 r=50 w=0 time=10944790 us)
    90549 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=271647 r=1 w=0 time=5063466 us)
    90549 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=181098 r=1 w=0 time=2863447 us)(object id 637620)
    90549 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=271647 r=49 w=0 time=4794430 us)
    90549 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=181098 r=1 w=0 time=2553802 us)(object id 38017)
    90549 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=271648 r=1 w=0 time=3786267 us)
    90549 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=181098 r=0 w=0 time=2164425 us)(object id 637202)
    90549 INDEX UNIQUE SCAN IC_ITEM_MST_TL_PK (cr=181098 r=0 w=0 time=2260794 us)(object id 637049)
    90549 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=181098 r=1 w=0 time=1902881 us)(object id 637610)
    14 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=71479854 r=99 w=0 time=1295671259 us)
    101213210 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=676173 r=21 w=0 time=136382307 us)(object id 222381)
    14 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=56 r=4 w=0 time=2052 us)
    14 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=42 r=2 w=0 time=1265 us)(object id 637681)
    0 FILTER (cr=0 r=0 w=0 time=59787 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 NESTED LOOPS (cr=0 r=0 w=0 time=0 us)
    0 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_LINES (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_LINES_PK (cr=0 r=0 w=0 time=0 us)(object id 637620)
    0 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=0 r=0 w=0 time=0 us)(object id 38017)
    0 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=0 r=0 w=0 time=0 us)(object id 637202)
    0 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=0 r=0 w=0 time=0 us)(object id 637610)
    0 TABLE ACCESS BY INDEX ROWID IC_TRAN_PND (cr=0 r=0 w=0 time=0 us)
    0 INDEX RANGE SCAN IC_TRAN_PNDI6 (cr=0 r=0 w=0 time=0 us)(object id 222381)
    0 TABLE ACCESS BY INDEX ROWID IC_LOTS_MST (cr=0 r=0 w=0 time=0 us)
    0 INDEX UNIQUE SCAN IC_LOTS_MST_PK (cr=0 r=0 w=0 time=0 us)(object id 637681)
    7 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_DETAILS (cr=32 r=3 w=0 time=2586 us)
    14 INDEX RANGE SCAN WSH_DELIVERY_DETAILS_N3 (cr=16 r=1 w=0 time=1221 us)(object id 46264)
    0 TABLE ACCESS BY INDEX ROWID OE_SETS (cr=0 r=0 w=0 time=32 us)
    0 INDEX UNIQUE SCAN OE_SETS_U1 (cr=0 r=0 w=0 time=6 us)(object id 43138)
    7 TABLE ACCESS BY INDEX ROWID OE_ORDER_LINES_ALL (cr=16 r=6 w=0 time=2715 us)
    7 INDEX UNIQUE SCAN OE_ORDER_LINES_U1 (cr=9 r=2 w=0 time=1399 us)(object id 42102)
    1 TABLE ACCESS BY INDEX ROWID IC_TXN_REQUEST_HEADERS (cr=16 r=2 w=0 time=679 us)
    7 INDEX UNIQUE SCAN IC_TXN_REQUEST_HEADERS_PK (cr=9 r=0 w=0 time=124 us)(object id 637610)
    1 TABLE ACCESS BY INDEX ROWID MTL_SYSTEM_ITEMS_B (cr=3 r=0 w=0 time=50 us)
    1 INDEX UNIQUE SCAN MTL_SYSTEM_ITEMS_B_U1 (cr=2 r=0 w=0 time=23 us)(object id 38017)
    1 TABLE ACCESS BY INDEX ROWID OE_ORDER_HEADERS_ALL (cr=3 r=0 w=0 time=58 us)
    1 INDEX UNIQUE SCAN OE_ORDER_HEADERS_U1 (cr=2 r=0 w=0 time=36 us)(object id 41952)
    1 TABLE ACCESS BY INDEX ROWID IC_ITEM_MST_B (cr=3 r=0 w=0 time=33 us)
    1 INDEX UNIQUE SCAN IC_ITEM_MST_B_UNQ1 (cr=2 r=0 w=0 time=14 us)(object id 637202)
    1 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_ASSIGNMENTS (cr=5 r=1 w=0 time=358 us)
    1 INDEX RANGE SCAN WSH_DELIVERY_ASSIGNMENTS_N3 (cr=2 r=0 w=0 time=68 us)(object id 46295)
    1 TABLE ACCESS BY INDEX ROWID WSH_NEW_DELIVERIES (cr=3 r=1 w=0 time=9428 us)
    1 INDEX UNIQUE SCAN WSH_NEW_DELIVERIES_U1 (cr=2 r=1 w=0 time=9405 us)(object id 46306)
    0 TABLE ACCESS BY INDEX ROWID WSH_DELIVERY_LEGS (cr=2 r=0 w=0 time=58 us)
    0 INDEX RANGE SCAN WSH_DELIVERY_LEGS_N1 (cr=2 r=0 w=0 time=55 us)(object id 46235)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIP_STOPS (cr=0 r=0 w=0 time=2 us)
    0 INDEX UNIQUE SCAN WSH_TRIP_STOPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46088)
    0 TABLE ACCESS BY INDEX ROWID WSH_TRIPS (cr=0 r=0 w=0 time=1 us)
    0 INDEX UNIQUE SCAN WSH_TRIPS_U1 (cr=0 r=0 w=0 time=1 us)(object id 46057)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=3 r=0 w=0 time=55 us)
    1 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=2 r=0 w=0 time=34 us)(object id 81600)
    1 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=3 r=0 w=0 time=48 us)
    1 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=2 r=0 w=0 time=26 us)(object id 237346)
    1 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=3 r=1 w=0 time=12241 us)
    1 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=2 r=1 w=0 time=12187 us)(object id 236588)

  • SQL tuning suggestions.

    Hi
    I am not a sql programmer and developers have given me this sql to have a look. I made the following recommendations after going through the sql. Is there anything else that can be added . I did not add about stats because they are representative and up to date.
    SELECT /*+ PARALLEL(q1,4) */ *
    FROM
    (SELECT /*+ FIRST_ROWS(20) */
    br.resource_id,
    br.resource_code,
    x.resource_seq_num employee_resource_number,
    br.organization_id,
    bd.department_id,
    bd.department_code,
    pf.full_name employee_name,
    (SELECT xxdl_eam_util_pkg.xxdl_eam_get_resource_code(pf.person_id, bd.department_id)
    FROM dual)
    maximum_cost_resource,
    pf.person_id,
    x.wip_entity_id wo_id,
    (SELECT weo1.wip_entity_name
    FROM wip_eam_work_orders_v weo1
    WHERE weo1.wip_entity_id = x.wip_entity_id)
    wo_number,
    CAST(x.start_date AS
    TIMESTAMP) start_date,
    CAST(x.completion_date AS
    TIMESTAMP) completion_date,
    wor.operation_seq_num wo_operation_number,
    wor.resource_seq_num wo_resource_number,
    wor.usage_rate_or_amount HOURS,
    BRE.effective_start_date instance_start_date,
    BRE.effective_end_date instance_end_date,
    BRE.instance_id,
    crc.resource_rate AS
    resource_cost,
    (SELECT xxdl_eam_util_pkg.xxdl_eam_get_all_res_code(pf.person_id, bd.department_id)
    FROM dual)
    all_resources
    FROM per_all_people_f pf,
    wip_eam_work_orders_v weo,
    wip_operations wo,
    wip_operation_resources wor,
    (SELECT instance_id,
    wip_entity_id,
    operation_seq_num,
    resource_seq_num,
    start_date,
    completion_date
    FROM wip_op_resource_instances_v) x,
    bom_dept_res_instances bdri,
    bom_resource_employees BRE,
    bom_department_resources bdr,
    bom_resources br,
    cst_resource_costs crc,
    bom_departments bd
    WHERE br.organization_id = bd.organization_id
    AND bdr.resource_id = br.resource_id
    AND bdr.department_id = bd.department_id
    AND BRE.resource_id = br.resource_id
    AND pf.effective_start_date <=sysdate
    AND pf.effective_end_date >= sysdate
    AND pf.person_id = BRE.person_id
    AND wo.department_id = bd.department_id
    AND wor.operation_seq_num(+) = wo.operation_seq_num
    AND wor.wip_entity_id(+) = wo.wip_entity_id
    AND wor.organization_id(+) = wo.organization_id
    AND weo.wip_entity_id = wo.wip_entity_id
    AND weo.organization_id = wo.organization_id
    -- AND weo.organization_id = 6921
    AND DECODE(bd.disable_date,null, sysdate,bd.disable_date) >= sysdate
    AND DECODE(br.disable_date,null, sysdate,br.disable_date) >= sysdate
    AND DECODE(wo.disable_date,null, sysdate,wo.disable_date) >= sysdate
    AND crc.resource_id(+) = BRE.resource_id
    AND x.wip_entity_id = wor.wip_entity_id
    AND x.operation_seq_num = wor.operation_seq_num
    AND x.resource_seq_num = wor.resource_seq_num
    AND x.instance_id(+) = BRE.instance_id
    AND bdri.department_id = bd.department_id
    AND bdri.resource_id = br.resource_id
    AND weo.organization_id = 6921
    AND bdri.department_id = 5004
    UNION
    SELECT /*+ FIRST_ROWS(20) */ DISTINCT NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    pf.full_name employee_name,
    NULL,
    pf.person_id,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL HOURS,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
    FROM per_all_people_f pf,
    wip_eam_work_orders_v weo,
    wip_operations wo,
    wip_operation_resources wor,
    bom_dept_res_instances bdri,
    bom_resource_employees BRE,
    bom_department_resources bdr,
    bom_resources br,
    cst_resource_costs crc,
    bom_departments bd
    WHERE br.organization_id = bd.organization_id
    AND bdr.resource_id = br.resource_id
    AND bdr.department_id = bd.department_id
    AND BRE.resource_id = br.resource_id
    AND pf.effective_start_date <=sysdate
    AND pf.effective_end_date >= sysdate
    AND pf.person_id = BRE.person_id
    AND wo.department_id = bd.department_id
    AND wor.operation_seq_num(+) = wo.operation_seq_num
    AND wor.wip_entity_id(+) = wo.wip_entity_id
    AND wor.organization_id(+) = wo.organization_id
    AND weo.wip_entity_id = wo.wip_entity_id
    AND weo.organization_id = wo.organization_id
    AND DECODE(bd.disable_date,null, sysdate,bd.disable_date) >= sysdate
    AND DECODE(br.disable_date,null, sysdate,br.disable_date) >= sysdate
    AND DECODE(wo.disable_date,null, sysdate,wo.disable_date) >= sysdate
    AND crc.resource_id(+) = BRE.resource_id
    AND bdri.department_id = bd.department_id
    AND bdri.resource_id = br.resource_id
    AND weo.organization_id = 6921
    AND bdri.department_id = 5004
    AND NOT EXISTS
    (SELECT instance_id,
    wip_entity_id operation_seq_num,
    resource_seq_num
    FROM wip_op_resource_instances_v)
    ) q1
    ORDER BY department_id,
    resource_code,
    employee_name,
    wo_number
    My suggestions:
    . Try to use UNION ALL instead of UNION. If you can eliminate UNION all together and flatten the query that will be even better.
    2. You are using the function in a select statement xxdl_eam_util_pkg.xxdl_eam_get_resource_code(pf.person_id, bd.department_id)This will slow the performance. Try to get rid of this. Function calls in select are expensive.
    3. Dont use the parallel hint and optimize. It wont get you consistent results.
    4. Use of per_all_people_F is expensive. The UNION again complicates things. per_all_people_f has to be scanned 2x times.
    5. Where does the application get the values for department id? Whether user inputs a value or whether it is hard coded . Most likely user will input value and each time it may be different. If that is the case, then you may be hitting bind peeking. There is not much hope for this. Not much can be done. Whatever you do this can happen. Only way is to pin the plan if you can use literals instead of binds. But that is not possible I think.
    6. AND DECODE(bd.disable_date,null, sysdate,bd.disable_date) >= sysdate
    AND DECODE(br.disable_date,null, sysdate,br.disable_date) >= sysdate
    AND DECODE(wo.disable_date,null, sysdate,wo.disable_date) >= sysdate
    Those statements, if you can rewrite would be good. If there are indexes on any of those columns, they are more than likely not used.
    7. Are the outer joins really required. If not required remove them.
    8. There is a 'WITH' clause in 10g . Try to use that for your subqueries or main query where applicable. It will save some I/O.
    9. Try to tune without any hints. Remove the first rows as well and see the difference.
    I know that the sql is definately bad and can be rewritten but I am not able to exactly write it for them.
    Any inputs or thoughts?
    MSK

    Hi,
    Any suggestions for reading on Sql tuning
    I am looking for a practical book with solutions .
    And books showing the Sql internal workings ?Take a look on Amazon some Jonathan Lewis books.
    I will also recommend you to take a look on the following blogs:
    - http://jonathanlewis.wordpress.com/
    - http://www.juliandyke.com/
    - http://richardfoote.wordpress.com/
    - http://tkyte.blogspot.com/
    And also any good interview based good Oracle DBA books ?You can take a look on my blog for some common DBA interview questions.
    http://oraclenz.com/category/interview-tips/
    Regards,
    Francisco Munoz Alvarez
    www.oraclenz.com

Maybe you are looking for

  • HP printer issue with Mac OSX 10.5.8

    How do i set up/connect HP officejet 4635 printer to Mac  OSX 10.5.8

  • Still the report is taking too much time

    Hi All, When i refresh a webi report the report is taken too much time to refresh(open). In back end i have checked all the connections, contexts, cardinalities, joins, conditions...etc, in webi i have enabled the the check box 'query stripping'. but

  • Insconsistence between UI OIM and User.xml  oim 11g r1

    i have a big problem when i try to create a new attribute this is not possible. if a try to modify an attribute the error is : [2013-05-02T19:37:12.011-05:00] [oim_server1] [ERROR] [] [XELLERATE.DATABASE] [tid: [ACTIVE].ExecuteThread: '15' for queue:

  • Restriction on memory scope for UI components binding

    Hello I'm using JDeveloper 11g 10.1.1.3 I found in the Fusion Developer's Guide for Oracle ADF (14 Getting Started with ADF Task Flows) next thing: <i> Restrict the scope of the managed bean that you reference through a UI component's binding attribu

  • Tables in APO

    Hi all, I am trying to derive data(Line, source item, target item, set up time & cost etc) related to production in APO using SQVI. The table names I get using conventional methods when tried in SE17 says structure or data cannot be read directly. Ca