Manual Tunning of SQL

Hi Forum Friends,
I have a concern about how to do manual Tunning of SQL .
I have a query:
SELECT /*+ FIRST_ROWS (1) */ * FROM "NRIV" WHERE "CLIENT" = :A0 AND "OBJECT" = :A1 AND "SUBOBJECT" = :A2 AND "NRRANGENR" = :A3 AND "TOYEAR" = :A4 FOR UPDATE;
Pls let me know the way .
Regards,
Vishal Raina

974882 wrote:
I have a query:
SELECT /*+ FIRST_ROWS (1) */ * FROM "NRIV" WHERE "CLIENT" = :A0 AND "OBJECT" = :A1 AND "SUBOBJECT" = :A2 AND "NRRANGENR" = :A3 AND "TOYEAR" = :A4 FOR UPDATE;
Start by getting an execution plan for the  query. Evaluate the plan and consider how different plans might affect performance.  Verify that the new plan will be  used always, always test the changes for performance - it is possible that the new plan will not perform as well as the original one.  Testing should be done by both timing the new and old SQL multiple times and examining run statistics, most easily acquired by the AUTOTRACE utility (Oracle Trace does these things too but is a bit more work to set up and use; AUTOTRACE provides run statistics or a single SQL).  Timing SQL is tricky because after the first execution data is cached in memory and subsequent runs will probably be faster because disk access is unnecessary.
Reading execution plans requires attention to detail.  The bytes, cardinality, and cost are all estimates and can be incorrect.  It is possible for a query with a higher cost to run faster and use fewer resources than a lower cost version of the same query - this is why testing is necessary.  No listed operation is always good or bad although some like FULL [table|index] SCAN and MERGE JOIN CARTESIAN should be investigated - both can under some circumstances be used by the optimizer to improve performance under some conditions.
Your query had some points of interest.  I personally have not seen the FIRST_ROWS hint to be effective for tuning since working with Oracle Forms long ago - hints can help, hurt, or not affect performance.  If your query selects less that about 20% of the rows in the table (this depends on your Oracle version) a composite index on all of the columns in the WHERE clause could allow a fast indexed lookup instead of a more expensive full table scan.  The FOR UPDATE clause always adds overhead to SQL by forcing rows to be locked one by one as they are selected instead of when modified by DML - if you need to lock the rows on SELECT to prevent another process from doing so use it but remember that bulk SQL which avoids context switching is usually more efficient than select loops with DML inside.
Good luck!
Good luck!

Similar Messages

  • Tips to tunning - attached sql

    Hi,
    I have this situation to find the top 2 salesman for last 3 and 12 months and I have written the sql to bring it but the sql takes too much time process. I had used explain it seems the sql does not use any index I created. If anyone gets any idea to best tune this sql probably breaking into two views or tables please do post it.
    Here is the table,
    sudh_top2_analyst_mv
    ACCOUNT_ID VARCHAR2(10)
    ANALYST VARCHAR2(80)
    IMONTH NUMBER
    CNT NUMBER
    Here is my sql,
    select
    distinct A.account_id Account,A.analyst Analyst1,C.analyst Analyst2,A.cnt TotCnt, 3 as Imonth from sudh_top2_analyst_mv A,
    (select analyst,account_id,cnt from sudh_top2_analyst_mv where cnt in (select max(E1.cnt) From sudh_top2_analyst_mv E1 Where
    (1) in (Select Count(Distinct(E2.cnt)) From sudh_top2_analyst_mv E2 Where
    E2.cnt > E1.cnt and imonth = 3 group by E2.account_id) and imonth = 3 group by E1.account_id) and imonth = 3) C
    where A.cnt in (select max(B.cnt) from sudh_top2_analyst_mv B
    where b.imonth = 3 group by B.account_id) and
    A.account_id = C.account_id and A.imonth = 3
    union all
    select distinct A.account_id,A.analyst,C.analyst,A.cnt,12 from sudh_top2_analyst_mv A,
    (select analyst,account_id,cnt from sudh_top2_analyst_mv where cnt in (select max(E1.cnt) From sudh_top2_analyst_mv E1 Where
    (1) in (Select Count(Distinct(E2.cnt)) From sudh_top2_analyst_mv E2 Where
    E2.cnt > E1.cnt and imonth = 12 group by E2.account_id) and imonth = 12 group by E1.account_id) and imonth = 12) C
    where A.cnt in (select max(B.cnt) from sudh_top2_analyst_mv B
    where b.imonth = 12 group by B.account_id) and
    A.account_id = C.account_id and A.imonth = 12
    Thanks,
    Sudhakar

    Sudhakar,
    Your query is accessing your table (or materialized view?) 10 times. See the output below:
    SQL> create table sudh_top2_analyst_mv
      2  (ACCOUNT_ID VARCHAR2(10)
      3  ,ANALYST VARCHAR2(80)
      4  ,IMONTH NUMBER
      5  ,CNT NUMBER
      6  )
      7  /
    Tabel is aangemaakt.
    SQL> explain plan for
      2  select
      3  distinct A.account_id Account,A.analyst Analyst1,C.analyst Analyst2,A.cnt TotCnt, 3 as Imonth from sudh_top2_analyst_mv A,
      4  (select analyst,account_id,cnt from sudh_top2_analyst_mv where cnt in (select max(E1.cnt) From sudh_top2_analyst_mv E1 Where
      5  (1) in (Select Count(Distinct(E2.cnt)) From sudh_top2_analyst_mv E2 Where
      6  E2.cnt > E1.cnt and imonth = 3 group by E2.account_id) and imonth = 3 group by E1.account_id) and imonth = 3) C
      7  where A.cnt in (select max(B.cnt) from sudh_top2_analyst_mv B
      8  where b.imonth = 3 group by B.account_id) and
      9  A.account_id = C.account_id and A.imonth = 3
    10  union all
    11  select distinct A.account_id,A.analyst,C.analyst,A.cnt,12 from sudh_top2_analyst_mv A,
    12  (select analyst,account_id,cnt from sudh_top2_analyst_mv where cnt in (select max(E1.cnt) From sudh_top2_analyst_mv E1 Where
    13  (1) in (Select Count(Distinct(E2.cnt)) From sudh_top2_analyst_mv E2 Where
    14  E2.cnt > E1.cnt and imonth = 12 group by E2.account_id) and imonth = 12 group by E1.account_id) and imonth = 12) C
    15  where A.cnt in (select max(B.cnt) from sudh_top2_analyst_mv B
    16  where b.imonth = 12 group by B.account_id) and
    17  A.account_id = C.account_id and A.imonth = 12
    18  /
    Uitleg is gegeven.
    SQL> select * from table(dbms_xplan.display)
      2  /
    PLAN_TABLE_OUTPUT
    | Id  | Operation                   |  Name                 | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT            |                       |       |       |       |
    |   1 |  UNION-ALL                  |                       |       |       |       |
    |   2 |   SORT UNIQUE               |                       |       |       |       |
    |   3 |    MERGE JOIN               |                       |       |       |       |
    |   4 |     SORT JOIN               |                       |       |       |       |
    |   5 |      MERGE JOIN             |                       |       |       |       |
    |   6 |       SORT JOIN             |                       |       |       |       |
    |   7 |        MERGE JOIN           |                       |       |       |       |
    |   8 |         SORT JOIN           |                       |       |       |       |
    |*  9 |          TABLE ACCESS FULL  | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 10 |         SORT JOIN           |                       |       |       |       |
    |* 11 |          TABLE ACCESS FULL  | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 12 |       SORT JOIN             |                       |       |       |       |
    |  13 |        VIEW                 | VW_NSO_3              |       |       |       |
    |  14 |         SORT UNIQUE         |                       |       |       |       |
    |  15 |          SORT GROUP BY      |                       |       |       |       |
    |* 16 |           TABLE ACCESS FULL | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 17 |     SORT JOIN               |                       |       |       |       |
    |  18 |      VIEW                   | VW_NSO_4              |       |       |       |
    |  19 |       SORT UNIQUE           |                       |       |       |       |
    |  20 |        SORT GROUP BY        |                       |       |       |       |
    |* 21 |         FILTER              |                       |       |       |       |
    |* 22 |          TABLE ACCESS FULL  | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 23 |          FILTER             |                       |       |       |       |
    |  24 |           SORT GROUP BY     |                       |       |       |       |
    |* 25 |            TABLE ACCESS FULL| SUDH_TOP2_ANALYST_MV  |       |       |       |
    |  26 |   SORT UNIQUE               |                       |       |       |       |
    |  27 |    MERGE JOIN               |                       |       |       |       |
    |  28 |     SORT JOIN               |                       |       |       |       |
    |  29 |      MERGE JOIN             |                       |       |       |       |
    |  30 |       SORT JOIN             |                       |       |       |       |
    |  31 |        MERGE JOIN           |                       |       |       |       |
    |  32 |         SORT JOIN           |                       |       |       |       |
    |* 33 |          TABLE ACCESS FULL  | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 34 |         SORT JOIN           |                       |       |       |       |
    |* 35 |          TABLE ACCESS FULL  | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 36 |       SORT JOIN             |                       |       |       |       |
    |  37 |        VIEW                 | VW_NSO_1              |       |       |       |
    |  38 |         SORT UNIQUE         |                       |       |       |       |
    |  39 |          SORT GROUP BY      |                       |       |       |       |
    |* 40 |           TABLE ACCESS FULL | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 41 |     SORT JOIN               |                       |       |       |       |
    |  42 |      VIEW                   | VW_NSO_2              |       |       |       |
    |  43 |       SORT UNIQUE           |                       |       |       |       |
    |  44 |        SORT GROUP BY        |                       |       |       |       |
    |* 45 |         FILTER              |                       |       |       |       |
    |* 46 |          TABLE ACCESS FULL  | SUDH_TOP2_ANALYST_MV  |       |       |       |
    |* 47 |          FILTER             |                       |       |       |       |
    |  48 |           SORT GROUP BY     |                       |       |       |       |
    |* 49 |            TABLE ACCESS FULL| SUDH_TOP2_ANALYST_MV  |       |       |       |
    Predicate Information (identified by operation id):
       9 - filter("SUDH_TOP2_ANALYST_MV"."IMONTH"=3)
      10 - access("A"."ACCOUNT_ID"="SUDH_TOP2_ANALYST_MV"."ACCOUNT_ID")
           filter("A"."ACCOUNT_ID"="SUDH_TOP2_ANALYST_MV"."ACCOUNT_ID")
      11 - filter("A"."IMONTH"=3)
      12 - access("A"."CNT"="VW_NSO_3"."$nso_col_1")
           filter("A"."CNT"="VW_NSO_3"."$nso_col_1")
      16 - filter("B"."IMONTH"=3)
      17 - access("SUDH_TOP2_ANALYST_MV"."CNT"="VW_NSO_4"."$nso_col_1")
           filter("SUDH_TOP2_ANALYST_MV"."CNT"="VW_NSO_4"."$nso_col_1")
      21 - filter( EXISTS (SELECT 0 FROM "SUDH_TOP2_ANALYST_MV" "E2" WHERE
                  "E2"."IMONTH"=3 AND "E2"."CNT">:B1 GROUP BY "E2"."ACCOUNT_ID" HAVING COUNT(DISTINCT
                  "E2"."CNT")=1))
      22 - filter("SYS_ALIAS_1"."IMONTH"=3)
      23 - filter(COUNT(DISTINCT "E2"."CNT")=1)
      25 - filter("E2"."IMONTH"=3 AND "E2"."CNT">:B1)
      33 - filter("SUDH_TOP2_ANALYST_MV"."IMONTH"=12)
      34 - access("A"."ACCOUNT_ID"="SUDH_TOP2_ANALYST_MV"."ACCOUNT_ID")
           filter("A"."ACCOUNT_ID"="SUDH_TOP2_ANALYST_MV"."ACCOUNT_ID")
      35 - filter("A"."IMONTH"=12)
      36 - access("A"."CNT"="VW_NSO_1"."$nso_col_1")
           filter("A"."CNT"="VW_NSO_1"."$nso_col_1")
      40 - filter("B"."IMONTH"=12)
      41 - access("SUDH_TOP2_ANALYST_MV"."CNT"="VW_NSO_2"."$nso_col_1")
           filter("SUDH_TOP2_ANALYST_MV"."CNT"="VW_NSO_2"."$nso_col_1")
      45 - filter( EXISTS (SELECT 0 FROM "SUDH_TOP2_ANALYST_MV" "E2" WHERE
                  "E2"."IMONTH"=12 AND "E2"."CNT">:B1 GROUP BY "E2"."ACCOUNT_ID" HAVING COUNT(DISTINCT
                  "E2"."CNT")=1))
      46 - filter("SYS_ALIAS_1"."IMONTH"=12)
      47 - filter(COUNT(DISTINCT "E2"."CNT")=1)
      49 - filter("E2"."IMONTH"=12 AND "E2"."CNT">:B1)
    Note: rule based optimization
    91 rijen zijn geselecteerd.Analytic functions are a way to prevent this from happening. But it's very hard for us to tell how it should be written. Maybe you could post some sample data (insert scripts) and the expected output, so we can help better.
    Regards,
    Rob.

  • How to tune this SQL?

    What this SQL did is to generate a report like how many rows for each tp_stts when ims_toms_msge_type='TOMS NEW' (or 'TOMS CNCLO'). also, its corresponding total number for each ims_toms_msge_type.
    Here is the sample:
    Rownum    H1            TP_STTS                          COUNT        IMS_TOMS_MSGE_TYPE    H   FILTERTYPE
    1     Trades     Block Key Data Error                      594           TOMS NEW             A     MSL
    2          Allocation Data Error                      334           TOMS NEW             A     MSL
    3          Manual Processing Required                29           TOMS NEW             A      MSL
    4          Manual Removal No Processing Required     67           TOMS NEW             A      MSL
    5          Waiting For Authorization                 2            TOMS NEW             A     MSL
    6           Auto NAK                             19764          TOMS NEW             B     SLS
    7           Validated                             165023         TOMS NEW             B     SLS
    8     GRAND TOTAL                                    185813          TOMS NEW              H     MSL
    9     Cancel     Auto NAK                              37             TOMS CNCLO        X       MSL
    10          Manual Processing Required   114                    TOMS CNCLO          X      MSL
    11          Manually Processed              278                   TOMS CNCLO     X     MSL
    12     CANCEL GRAND TOTAL                        429                TOMS CNCLO           Z      MSLSQL statement as below:
    SELECT ROWNUM, MSGS.* FROM (with FTMReport as (
           select tp_stts, ims_toms_msge_type, count(*) count  from ims_trde, ims_toms_msge
                    where  ( IMS_TRDE.PRCSG_GRP_ID  = 5  )  AND  ( IMS_TRDE.IMS_TRDE_RCPT_DTTM  >= TO_DATE('12/01/2009 00:00', 'MM/DD/YYYY HH24:MI') AND IMS_TRDE.IMS_TRDE_RCPT_DTTM <= (TO_DATE('12/28/2009 23:59', 'MM/DD/YYYY HH24:MI'))  )  AND (IMS_TRDE.GRS_TRX_TYPE NOT IN ('INJECTION','WITHDRAWAL','PAYMENT') OR IMS_TRDE.GRS_TRX_TYPE IS NULL) AND (IMS_TRDE.SSC_INVST_TYPE != 'FC' OR IMS_TRDE.SSC_INVST_TYPE IS NULL) AND (IMS_TRDE.SERVICE_TYPE='FS' OR IMS_TRDE.SERVICE_TYPE='CO')  AND 1=1 
        and IMS_TRDE.SERVICE_TYPE='FS'    and  1=1  and ims_trde.ims_trde_oid = ims_toms_msge.ims_trde_oid
           group by tp_stts, ims_toms_msge_type
          select 'GRAND TOTAL' H1,  null tp_stts, sum(Count) count , ims_toms_msge_type ,'H', 'MSL' FilterType
                   from FTMReport  where  ims_toms_msge_type in ('TOMS NEW') group by ims_toms_msge_type
        union
          select 'CANCEL GRAND TOTAL' H1,  null tp_stts, sum(Count) count, ims_toms_msge_type ,'Z', 'MSL' FilterType 
                   from FTMReport where  ims_toms_msge_type in ('TOMS CNCLO') group by ims_toms_msge_type
        union
          select DECODE(rownum, 1, 'Trades') H1, tp_stts, count, ims_toms_msge_type , 'A' , 'MSL' FilterType
                   from FTMReport  where  ims_toms_msge_type in ('TOMS NEW') and tp_stts not in ('Validated','Auto NAK','Manual NAK')
        union
          select ' ' H1, tp_stts, count, ims_toms_msge_type , 'B' , 'SLS' FilterType
                   from FTMReport where  ims_toms_msge_type in ('TOMS NEW') and tp_stts in ('Validated','Auto NAK','Manual NAK')
        union
          select DECODE(rownum, 1, 'Cancel') H1, tp_stts, count, ims_toms_msge_type , 'X' , 'MSL' FilterType
                   from FTMReport where  ims_toms_msge_type in ('TOMS CNCLO') order by 5,6
        ) MSGS;Explain plan as below:
    | Id  | Operation                           |  Name                        | Rows  | Bytes | Cost  |
    |   0 | SELECT STATEMENT                    |                              |   193 | 12738 |    32 |
    |   1 |  COUNT                              |                              |       |       |       |
    |   2 |   VIEW                              |                              |   193 | 12738 |    32 |
    |   4 |    TEMP TABLE TRANSFORMATION        |                              |       |       |       |
    |   3 |     RECURSIVE EXECUTION             | SYS_LE_4_0                   |       |       |       |
    |   0 |      INSERT STATEMENT               |                              |    61 |  4575 |  9264 |
    |   1 |       LOAD AS SELECT                |                              |       |       |       |
    |   2 |        SORT GROUP BY                |                              |    61 |  4575 |  9264 |
    |   3 |         NESTED LOOPS                |                              |  1604 |   117K|  9251 |
    |*  4 |          TABLE ACCESS BY INDEX ROWID| IMS_TRDE                     |  1603 | 80150 |  6045 |
    |*  5 |           INDEX RANGE SCAN          | IMS_TRDE_INDX4               |   539K|       |  3917 |
    |*  6 |          INDEX RANGE SCAN           | IMS_TOMS_MSGE_INDX1          |     1 |    25 |     2 |
    |   5 |     SORT UNIQUE                     |                              |   193 |  8831 |    30 |
    |   6 |      UNION-ALL                      |                              |       |       |       |
    |   7 |       SORT GROUP BY NOSORT          |                              |     5 |   115 |     6 |
    |*  8 |        VIEW                         |                              |    61 |  1403 |     2 |
    |   9 |         TABLE ACCESS FULL           | SYS_TEMP_0FD9D660F_B198D56F  |    61 |  2074 |     2 |
    |  10 |       SORT GROUP BY NOSORT          |                              |     5 |   115 |     6 |
    |* 11 |        VIEW                         |                              |    61 |  1403 |     2 |
    |  12 |         TABLE ACCESS FULL           | SYS_TEMP_0FD9D660F_B198D56F  |    61 |  2074 |     2 |
    |  13 |       COUNT                         |                              |       |       |       |
    |* 14 |        VIEW                         |                              |    61 |  2867 |     2 |
    |  15 |         TABLE ACCESS FULL           | SYS_TEMP_0FD9D660F_B198D56F  |    61 |  2074 |     2 |
    |* 16 |       VIEW                          |                              |    61 |  2867 |     2 |
    |  17 |        TABLE ACCESS FULL            | SYS_TEMP_0FD9D660F_B198D56F  |    61 |  2074 |     2 |
    |  18 |       COUNT                         |                              |       |       |       |
    |* 19 |        VIEW                         |                              |    61 |  2867 |     2 |
    |  20 |         TABLE ACCESS FULL           | SYS_TEMP_0FD9D660F_B198D56F  |    61 |  2074 |     2 |
    Predicate Information (identified by operation id):
       4 - filter(("IMS_TRDE"."GRS_TRX_TYPE"<>'INJECTION' AND "IMS_TRDE"."GRS_TRX_TYPE"<>'WITHDRAWAL'
                  AND "IMS_TRDE"."GRS_TRX_TYPE"<>'PAYMENT' OR "IMS_TRDE"."GRS_TRX_TYPE" IS NULL) AND
                  ("IMS_TRDE"."SSC_INVST_TYPE"<>'FC' OR "IMS_TRDE"."SSC_INVST_TYPE" IS NULL))
       5 - access("IMS_TRDE"."IMS_TRDE_RCPT_DTTM">=TO_DATE(' 2009-12-01 00:00:00', 'syyyy-mm-dd
                  hh24:mi:ss') AND "IMS_TRDE"."PRCSG_GRP_ID"=5 AND "IMS_TRDE"."SERVICE_TYPE"='FS' AND
                  "IMS_TRDE"."IMS_TRDE_RCPT_DTTM"<=TO_DATE(' 2009-12-28 23:59:00', 'syyyy-mm-dd hh24:mi:ss'))
           filter("IMS_TRDE"."PRCSG_GRP_ID"=5 AND "IMS_TRDE"."SERVICE_TYPE"='FS')
       6 - access("IMS_TRDE"."IMS_TRDE_OID"="IMS_TOMS_MSGE"."IMS_TRDE_OID")
       8 - filter("FTMREPORT"."IMS_TOMS_MSGE_TYPE"='TOMS NEW')
      11 - filter("FTMREPORT"."IMS_TOMS_MSGE_TYPE"='TOMS CNCLO')
      14 - filter("FTMREPORT"."IMS_TOMS_MSGE_TYPE"='TOMS NEW' AND "FTMREPORT"."TP_STTS"<>'Validated'
                  AND "FTMREPORT"."TP_STTS"<>'Auto NAK' AND "FTMREPORT"."TP_STTS"<>'Manual NAK')
      16 - filter("FTMREPORT"."IMS_TOMS_MSGE_TYPE"='TOMS NEW' AND ("FTMREPORT"."TP_STTS"='Auto NAK' OR
                  "FTMREPORT"."TP_STTS"='Manual NAK' OR "FTMREPORT"."TP_STTS"='Validated'))
      19 - filter("FTMREPORT"."IMS_TOMS_MSGE_TYPE"='TOMS CNCLO')
    Note: cpu costing is offCould you guys tell me what is wrong with this sql and how to tune it?
    Is there any way to replace the UNION? Is using UNION a good idea?
    Can I use DECODE or CASE WHEN to simplify it? How?
    Also, could you help explain why there is SYS_LE_4_0, SYS_TEMP_0FD9D660F_B198D56F temporary segment? Is it caused by with FTMReport as ?
    Thanks
    Edited by: PhoenixBai on Dec 31, 2009 9:58 AM
    Edited by: PhoenixBai on Dec 31, 2009 12:08 PM
    Edited by: PhoenixBai on Dec 31, 2009 12:17 PM

    I consider using GROUP BY ROLLUP together with DECODE, CASE WHEN, but haven`t come out with a working sql yet:-(Some sample data to work with would have been much helpful. But we dont get that most of the time :(
    I came up with this.
                    select case when ims_toms_msge_type = 'TOMS NEW' then
                                     case when grouping(tp_stts) = 1 then 'GRAND TOTAL'
                                          when tp_stts not in ('Validated','Auto NAK','Manual NAK') then 'Trades'
                                     end
                                when ims_toms_msge_type = 'TOMS CNCLO' then
                                     case when grouping(tp_stts) = 1 then 'CANCEL GRAND TOTAL'
                                          when tp_stts in ('Validated','Auto NAK','Manual NAK') then 'Cancel'
                                     end
                           end h1,
                           tp_stts,
                           count(*) count,
                           ims_toms_msge_type,
                           case when ims_toms_msge_type = 'TOMS NEW' then
                                     case when grouping(tp_stts) = 1 then 'H'
                                          when tp_stts not in ('Validated','Auto NAK','Manual NAK') then 'A'
                                     end
                                when ims_toms_msge_type = 'TOMS CNCLO' then
                                     case when grouping(tp_stts) = 1 then 'Z'
                                          when tp_stts in ('Validated','Auto NAK','Manual NAK') then 'B'
                                     end
                           end flag,
                           'MSL' FilterType
                   from ims_trde,
                        ims_toms_msge
                     where IMS_TRDE.PRCSG_GRP_ID  = 5 
                       AND IMS_TRDE.IMS_TRDE_RCPT_DTTM  >= TO_DATE('12/01/2009 00:00', 'MM/DD/YYYY HH24:MI')
                       AND IMS_TRDE.IMS_TRDE_RCPT_DTTM <=  TO_DATE('12/28/2009 23:59', 'MM/DD/YYYY HH24:MI') 
                       AND (IMS_TRDE.GRS_TRX_TYPE NOT IN ('INJECTION','WITHDRAWAL','PAYMENT')
                        OR IMS_TRDE.GRS_TRX_TYPE IS NULL)
                       AND (IMS_TRDE.SSC_INVST_TYPE != 'FC'
                        OR IMS_TRDE.SSC_INVST_TYPE IS NULL)
                       AND (IMS_TRDE.SERVICE_TYPE = 'FS'
                        OR IMS_TRDE.SERVICE_TYPE = 'CO')
                       AND 1=1 
                     and IMS_TRDE.SERVICE_TYPE = 'FS'   
                     and  1=1 
                     and ims_trde.ims_trde_oid = ims_toms_msge.ims_trde_oid
                      group by rollup(ims_toms_msge_type, tp_stts)Not sure if this code works. Because i dint have the table or the data to test it. So if it has some error just fix it and give it a try. Do post the kind of output it gives. So that we can see if we can work with this solution.

  • Tunning the sql

    Below is the table structure & index's for the table and Total record count for the table is: 40450708
    here, I am selecting distinct of ELIM_NBR from table using below query and it contains only: 1641 records
    SELECT DISTINCT ELIM_NBR FROM ELIM_RULE_REF_TBL;while we run the above sql, its taking too long (more than 5 min), could you please help me - how to tune the query to get the distinct of ELIM_NBR from the table...
    -- Create table
    create table ELIM_RULE_REF_TBL
      ENT             VARCHAR2(4) not null,
      SUB_ENT         VARCHAR2(3) not null,
      SRC_ACCT        VARCHAR2(9) not null,
      EFF_FROM_DATE   DATE not null,
      EFF_TO_DATE     DATE not null,
      TARG_ENT        VARCHAR2(4) not null,
      TARG_SUB_ENT    VARCHAR2(3) not null,
      ELIM_DESCR      VARCHAR2(115),
      ELIM_NBR        VARCHAR2(5),
      ELIM_RESID_ACCT VARCHAR2(9),
      ENT_LVL         NUMBER
    tablespace INS_DATA
      pctfree 10
      initrans 1
      maxtrans 255
      storage
        initial 128M
        next 50M
        minextents 1
        maxextents unlimited
        pctincrease 0
    -- Create/Recreate indexes
    create index ELIMREF_IDX_01 on ELIM_RULE_REF_TBL (TARG_ENT,TARG_SUB_ENT,SRC_ACCT,ELIM_RESID_ACCT,EFF_FROM_DATE,EFF_TO_DATE)
      tablespace INS_IDX
      pctfree 10
      initrans 2
      maxtrans 255
      storage
        initial 50M
        next 50M
        minextents 1
        maxextents unlimited
        pctincrease 0
    create index ELIMREF_IDX_02 on ELIM_RULE_REF_TBL (TARG_ENT,TARG_SUB_ENT,ELIM_NBR,ENT,SUB_ENT)
      tablespace INS_IDX
      pctfree 10
      initrans 2
      maxtrans 255
      storage
        initial 50M
        next 50M
        minextents 1
        maxextents unlimited
        pctincrease 0
    create unique index ELIMREF_IDX_PK on ELIM_RULE_REF_TBL (ENT,SUB_ENT,SRC_ACCT,EFF_FROM_DATE,EFF_TO_DATE,TARG_ENT,TARG_SUB_ENT)
      tablespace INS_IDX
      pctfree 10
      initrans 2
      maxtrans 255
      storage
        initial 50M
        next 50M
        minextents 1
        maxextents unlimited
        pctincrease 0
      );

    If it is so crucial for you to get this particular query processed as fast as possible there are a couple of things you can evaluate:
    * Follow this thread When your query takes too long ... to generate a proper explain plan output and run tkprof on a sql trace of this statement. Post the results here. This provides information where potentially most of time is spent to process the query.
    * Create an (additional) index that consists just of the ELIM_NBR, provided that you only query this column and restrict only on that column in the WHERE clause. This builds a smaller index than the currently used index ELIMREF_IDX_02 which should speed up the full index scan.
    * Since a sort unique operation is required you could think of increasing your sort memory usage. You haven't mentioned which database version you're running, but if you're already on 9i and have PGA_AGGREGATE_TARGET defined you could switch your session to "WORKAREA_SIZE_POLICY = manual" and setting SORT_AREA_SIZE to a large value, like 100000000 to allow a maximum of 100M for in-memory sorting, depending on your hardware, environment and concurrent usage of the system.
    * If you have the suitable license and hardware you could try to increase the speed of the index fast full scan by using the parallel query option:
    SELECT /*+ PARALLEL_INDEX(ELIM_RULE_REF_TBL, ELIMREF_IDX_02) */ DISTINCT ELIM_NBR
      FROM ELIM_RULE_REF_TBL
    WHERE SUBSTR(ELIM_NBR, 2, 1) != '9';Regards,
    Randolf
    Oracle related stuff blog:
    http://oracle-randolf.blogspot.com/
    SQLTools++ for Oracle:
    http://www.sqltools-plusplus.org:7676/
    http://sourceforge.net/projects/sqlt-pp/

  • Trying to tune a SQL statement

    Please nee your help
    The sql statement below works perfectly and speed fast
    SELECT     0,
         :"gloSpaFolioID",
         SPA_ITEM_ID,
         :"Text Box 2",
         :"gloLogin",
         :"gloLogin"
    FROM     SPA_ITEM
    WHERE     UPC_CODE=:"Text Box 1"
    AND     LOCATION=:"gloLocation"
    But when append with the insert statement below, it slow down.
    Does anyone have an idea on how I could tune this statement?
    INSERT     INTO SPA_FOLIO_ITEM
         SPA_FOLIO_ITEM_ID,
         SPA_FOLIO_ID,
         SPA_ITEM_ID,
         QTY,
         CREATE_STAFF_ID,
         CHANGE_STAFF_ID
    SELECT     0,
         :"gloSpaFolioID",
         SPA_ITEM_ID,
         :"Text Box 2",
         :"gloLogin",
         :"gloLogin"
    FROM     SPA_ITEM
    WHERE     UPC_CODE=:"Text Box 1"
    AND     LOCATION=:"gloLocation"
    Thanks,

    The sql statement below works perfectly and speed fastIf the query starts returning rows quickly (like from sql plus) that doesn't mean the entire query is fast.
    If I have a 300 million row table and do: select * from huge_table, I will start seeing rows in an instant, but the full table scan takes far longer.

  • Tune up sql script

    hi,need somebody to tune up this script,its take about > 1min
    tq
    select
    ood.organization_name
    organization_name,
    ac.check_number
    cheque_no,
    acs.name document_name,
    aip.creation_date date_payment,
    ap_inv.invoice_num invoice_no,
    ap_inv.invoice_date invoice_date,
    poh.segment1 po_no,
    poh.comments description,
    pv.vendor_name supplier_name,
    decode(ap_inv.payment_status_flag,
    'Y', 'Yes',
    'N', 'No',
    'P', 'Partial',
    'Unknown') paid,
    decode(ap_dist.accrual_posted_flag,
    'S', 'Selected',
    'N', 'No',
    'Y', 'Yes',
    'Unknown') ap_inv_accounted,
    decode(aip.posted_flag,
    'N', 'No',
    'Y', 'Yes',
    'Unknown') ap_pay_accounted,
    po_ccid.concatenated_segments po_account,
    po_ccid.SEGMENT2 project_type,
    po_ccid.SEGMENT3 votecode,
    po_ccid.SEGMENT4 financeid,
    sum(nvl(ap_dist.amount,0)) amount
    from po.po_vendors pv
    , ap.ap_invoices_all ap_inv
    , ap.ap_invoice_distributions_all ap_dist
    , po.po_distributions_all po_dist
    , po.po_headers_all poh
    , po.po_lines_all pol
    , apps.gl_code_combinations_kfv ap_ccid
    , apps.gl_code_combinations_kfv po_ccid
    , ap.ap_invoice_payments_all aip
    , apps.ap_checks_all ac
    , ap.ap_check_stocks_all acs
    , apps.org_organization_definitions ood
    where pv.vendor_id = ap_inv.vendor_id
    and po_ccid.SEGMENT2 in ('400','401','402','403','100','404','405','406','407','408')
    and ap_inv.invoice_id = ap_dist.invoice_id
    and ap_dist.po_distribution_id = po_dist.po_distribution_id
    and po_dist.po_header_id = poh.po_header_id
    and po_dist.po_line_id = pol.po_line_id
    and ap_ccid.code_combination_id = ap_dist.dist_code_combination_id
    and po_ccid.code_combination_id = po_dist.code_combination_id
    and ap_inv.invoice_id = aip.invoice_id
    and aip.check_id = ac.check_id
    and ac.check_stock_id = acs.check_stock_id
    and ap_inv.org_id = ood.organization_id
    group by
    ood.organization_name , -- organization_name,
    ac.check_number , -- cheque_no,
    acs.name , -- document_name,
    aip.creation_date , -- date_payment,
    ap_inv.invoice_num , -- invoice_no,
    ap_inv.invoice_date , -- invoice_date,
    poh.segment1 , -- po_no,
    poh.comments , -- description,
    pv.vendor_name , -- supplier_name,
    decode(ap_inv.payment_status_flag,
    'Y', 'Yes',
    'N', 'No',
    'P', 'Partial',
    'Unknown') , -- paid,
    decode(ap_dist.accrual_posted_flag,
    'S', 'Selected',
    'N', 'No',
    'Y', 'Yes',
    'Unknown') , -- ap_inv_accounted,
    decode(aip.posted_flag,
    'N', 'No',
    'Y', 'Yes',
    'Unknown') , -- ap_pay_accounted,
    po_ccid.concatenated_segments ,
    po_ccid.SEGMENT2,
    po_ccid.SEGMENT3, po_ccid.SEGMENT4

    Read this:
    HOW TO: Post a SQL statement tuning request - template posting

  • How to perf tune Oracle SQL/XML query?

    Hi all,
    I am using Oracle 9i and like to run the following Oracle SQL/XML query. It takes about 3+ hour and still not finish. If I get rid all the XML stuffs it only take minutes to run. Does anybody know how to what's the reason of this slow and how to tune it?
    SELECT XMLElement("CUSTOMER",
    XMLForest(C_CUSTKEY "C_CUSTKEY", C_NAME "C_NAME", C_ADDRESS "C_ADDRESS", C_PHONE "C_PHONE", C_MKTSEGMENT "C_MKTSEGMENT", C_COMMENT "C_COMMENT"),
    (SELECT XMLAgg(XMLElement("ORDERS",
    XMLForest(O_ORDERKEY "O_ORDERKEY", O_CUSTKEY "O_CUSTKEY", O_ORDERSTATUS "O_ORDERSTATUS", O_ORDERPRIORITY "O_ORDERPRIORITY", O_CLERK "O_CLERK", O_COMMENT "O_COMMENT"),
    (SELECT XMLAgg(XMLElement("LINEITEM",
    XMLForest(L_ORDERKEY "L_ORDERKEY", L_RETURNFLAG "L_RETURNFLAG", L_LINESTATUS "L_LINESTATUS", L_SHIPINSTRUCT "L_SHIPINSTRUCT", L_SHIPMODE "L_SHIPMODE", L_COMMENT "L_COMMENT")
    FROM LINEITEM
    WHERE LINEITEM.L_ORDERKEY = ORDERS.O_ORDERKEY)
    FROM ORDERS
    WHERE ORDERS.O_CUSTKEY = CUSTOMER.C_CUSTKEY)
    FROM CUSTOMER ;
    Thanks very much in advance for your time,
    Jinghao Liu

    Please post this message at:
    Forums Home » Oracle Technology Network (OTN) » Products » Database » XML DB

  • How to tune this SQL (takes long time to come up with results)

    Dear all,
    I have sum SQL which takes long time ... can any one help me to tune this.... thank You
    SELECT SUM (n_amount)
    FROM (SELECT DECODE (v_payment_type,
    'D', n_amount,
    'C', -n_amount
    ) n_amount, v_vou_no
    FROM vouch_det a, temp_global_temp b
    WHERE a.v_vou_no = TO_CHAR (b.n_column2)
    AND b.n_column1 = :b5
    AND b.v_column1 IN (:b4, :b3)
    AND v_desc IN (SELECT v_trans_source_code
    FROM benefit_trans_source
    WHERE v_income_tax_app = :b6)
    AND v_lob_code = DECODE (:b1, :b2, v_lob_code, :b1)
    UNION ALL
    SELECT DECODE (v_payment_type,
    'D', n_amount,
    'C', -n_amount
    * -1 AS n_amount,
    v_vou_no
    FROM vouch_details a, temp_global_temp b
    WHERE a.v_vou_no = TO_CHAR (b.n_column2)
    AND b.n_column1 = :b5
    AND b.v_column1 IN (:b12, :b11, :b10, :b9, :b8, :b7)
    AND v_desc IN (SELECT v_trans_source_code
    FROM benefit_trans_source
    WHERE income_tax_app = :b6)
    AND v_lob_code = DECODE (:b1, :b2, v_lob_code, :b1));
    Thank You.....

    Thanks a lot,
    i did change the SQL it works fine but slows down my main query.... actually my main query is calling a function which does the sum......
    here is the query.....?
    select A.* from (SELECT a.n_agent_no, a.v_agent_code, a.n_channel_no, v_iden_no, a.n_cust_ref_no, a.v_agent_type, a.v_company_code,
    a.v_company_branch, a.v_it_no, bfn_get_agent_name(a.n_agent_no) agentname,
    PKG_AGE__TAX.GET_TAX_AMT(:P_FROM_DATE,:P_TO_DATE,:P_LOB_CODE,A.N_AGENT_NO)  comm,
    c.v_ird_region
    FROM agent_master a, agent_lob b, agency_region c
    WHERE a.n_agent_no = b.n_agent_no
    AND a.v_agency_region = c.v_agency_region
    AND :p_lob_code = DECODE(:p_lob_code,'ALL', 'ALL',b.v_line_of_business)
    AND :p_channel_no = DECODE(:p_channel_no,1000, 1000,a.n_channel_no)
    AND :p_agency_group = DECODE(:p_agency_group,'ALL', 'ALL',c.v_ird_region)
    group by a.n_agent_no, a.v_agent_code, a.n_channel_no, v_iden_no, a.n_cust_ref_no, a.v_agent_type, a.v_company_code, a.v_company_branch, a.v_it_no, bfn_get_agent_name(a.n_agent_no) ,
    BPG_AGENCY_GEN_ACL_TAX.BFN_GET_TAX_AMOUNT(:P_FROM_DATE,:P_TO_DATE,:P_LOB_CODE,A.N_AGENT_NO),
    c.v_ird_region
    ORDER BY c.v_ird_region, a.v_agent_code DESC)
    A
    WHERE (COMM < :P_VAL_IND OR      COMM >=:P_VAL_IND1);
    Any idea to make this faster....
    Thank You...

  • Tunning PL/SQL  with sql dynamic returning cursor

    Hi
    How can I to tunning a Stored Procedure with Dynamic Cursor that return a Cursor (resultset to VB), is very slow

    1. Run your app (or better yet your PL/SQL procedure) with SQL_TRACE=TRUE and analyze trace file (TKPROF report e.g.)
    2. Tune SQL executed by PL/SQL procedure directly. Copy the SQL from the procedure, run it in SQL*Plus [with SET AUTOTRACE ON] and analyze exec plan, statistics etc.
    3. profile your procedure to figure out which part of it takes what (time, resources etc.)

  • Failover Occured manually or automatically (SQL Server AlwayOn with Availability Groups)

    Hello everyone,
    we had a Failover today on a Windows Server 2012 Failover Cluster with SQL Server Always On and now I'd like to know if the failover was done by the Cluster (or SQL?) or manually from a user...
    In the Cluster Log I can see following entry: INFO  [RCM] rcm::RcmApi::MoveGroup: (CVGID2, 1, 0, MoveType::Manual )
    So I assume that the failover was done by a user... But: I can see only Movetypes of "Manual" in the cluster log of this always on system...
    So my question is: Is it possible that since SQL Server (Always On) is kind of "driving" the cluster, that if it makes an automatic failover the Cluster will log this as a "manual" failover because it was not done by the Cluster service
    itself?
    Thanks for any help!
    Ville

    Hi Ville,
    When we meet this event it indicate it is not an issue since the failover was triggered manually.
    The related third party article:
    dbi services Blog
    http://www.dbi-services.com/index.php/blog/entry/wsfc-manual-or-automatic-failover-that-is-the-question
    I’m glad to be of help to you!
    *** This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these
    sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use
    of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. ***
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Support, contact [email protected]

  • Help me tune the sql query

    Hi,
    TKPROF: Release 8.1.7.0.0 - Production on Wed Sep 23 23:04:43 2009
    (c) Copyright 2000 Oracle Corporation.  All rights reserved.
    Trace file: pindb_ora_9943.trc
    Sort options: prsela  exeela  fchela  fchela 
    count    = number of times OCI procedure was executed
    cpu      = cpu time in seconds executing
    elapsed  = elapsed time in seconds executing
    disk     = number of physical reads of buffers from disk
    query    = number of buffers gotten for consistent read
    current  = number of buffers gotten in current mode (usually for update)
    rows     = number of rows processed by the fetch or execute call
    SELECT f.poid_id0, COUNT (*), SUM (amount), d.subscription_amt, d.free_mb
    FROM event_t a,
    event_bal_impacts_t b,
    (SELECT DISTINCT a.obj_id0 AS account_obj_id0, a.node_location,
    a.plan_obj_id0
    FROM account_products_t a
    WHERE plan_obj_id0 > 0 AND service_obj_id0 > 0
    UNION
    SELECT DISTINCT a.account_obj_id0, a.node_location, a.plan_obj_id0
    FROM audit_account_products_t a
    WHERE plan_obj_id0 > 0
    AND service_obj_id0 > 0
    AND effective_end_t > 0) c,
    iq_prov_tags_t d,
    event_session_dialup_t e,
    plan_t f
    WHERE a.poid_id0 = b.obj_id0
    AND a.poid_id0 = e.obj_id0
    AND b.node_location = c.node_location
    AND a.account_obj_id0 = c.account_obj_id0
    AND c.plan_obj_id0 = d.plan_poid_id0
    AND d.plan_poid_id0 = f.poid_id0
    AND a.end_t >= pin_date_to_seconds (TRUNC (  SYSDATE
    - 1), NULL)
    AND a.end_t < pin_date_to_seconds (TRUNC (SYSDATE), NULL)
    AND b.resource_id IN (1000051)
    GROUP BY f.poid_id0, d.subscription_amt, d.free_mb
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.09       0.08          0          0          0           0
    Execute      2      0.07       0.07          0          0          0           0
    Fetch        2    235.91    2295.25    1509704    1674184        346          11
    total        5    236.07    2295.40    1509704    1674184        346          11
    Misses in library cache during parse: 1
    Optimizer goal: CHOOSE
    Parsing user id: 28 
    Rows     Row Source Operation
         11  SORT GROUP BY
         69   NESTED LOOPS
         70    NESTED LOOPS
         70     NESTED LOOPS
         70      HASH JOIN
         70       HASH JOIN
    300499        TABLE ACCESS BY INDEX ROWID EVENT_T
    300500         INDEX RANGE SCAN (object id 26458)
    816394        TABLE ACCESS BY INDEX ROWID EVENT_BAL_IMPACTS_T
    816395         INDEX RANGE SCAN (object id 26463)
    2385556       VIEW
    2385556        SORT UNIQUE
    3124105         UNION-ALL
    638617          TABLE ACCESS FULL ACCOUNT_PRODUCTS_T
    2485488          TABLE ACCESS FULL AUDIT_ACCOUNT_PRODUCTS_T
        138      TABLE ACCESS BY INDEX ROWID IQ_PROV_TAGS_T
        138       INDEX UNIQUE SCAN (object id 26554)
        138     INDEX UNIQUE SCAN (object id 23148)
         69    INDEX UNIQUE SCAN (object id 27372)
    ALTER SESSION SET SQL_TRACE = TRUE
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        0      0.00       0.00          0          0          0           0
    total        1      0.00       0.00          0          0          0           0
    Misses in library cache during parse: 0
    Misses in library cache during execute: 1
    Optimizer goal: CHOOSE
    Parsing user id: 28 
    OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.09       0.08          0          0          0           0
    Execute      3      0.07       0.07          0          0          0           0
    Fetch        2    235.91    2295.25    1509704    1674184        346          11
    total        6    236.07    2295.40    1509704    1674184        346          11
    Misses in library cache during parse: 1
    Misses in library cache during execute: 1
    OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        2      0.01       0.00          0          0          0           0
    Execute      2      0.00       0.00          0          0          0           0
    Fetch       20      0.00       0.01          0         40          0          18
    total       24      0.01       0.01          0         40          0          18
    Misses in library cache during parse: 1
        2  user  SQL statements in session.
        2  internal SQL statements in session.
        4  SQL statements in session.
    Trace file: pindb_ora_9943.trc
    Trace file compatibility: 8.00.04
    Sort options: prsela  exeela  fchela  fchela 
           1  session in tracefile.
           2  user  SQL statements in trace file.
           2  internal SQL statements in trace file.
           4  SQL statements in trace file.
           3  unique SQL statements in trace file.
         118  lines in trace file.Inputs are appreciated
    Prakash GR

    sorry hash joint for asking this type of question but i do not have any option since i am not query tuner i need inputs so that i can put some effort on this
    Would you like a cup of tea with this my dear ?chilled beer will be offered if any inputs given
    Thanks
    Prakash GR

  • TUNNING A SQL QUERY AND UNSERSTAND EXPLAIN PLAN

    I was trying my handing in tunning sql queries -
    Though I have manged to reduce the cost lil ..creating some index, but have genarated a g8 interesting in tunning - Can some experts ( I know there are lots available :-) ) help me on the approch on "HOW TO TUNE A QUERY"
    moreover I also would like to understand - how to debug a explain plan, - please help ...
    Regards..

    Hi,
    Welcome to this forum...
    I would suggest your to read first the official documentations :
    - The concepts of an Oracle DBMS (this is important to know, because Oracle structures, processes, objects (etc) are the building blocks of your database)
    - SQL reference guide
    - PL/SQL reference guide
    and then at a later stage the Performance and tuning guide:
    You can start there:
    http://download.oracle.com/docs/cd/B14117_01/nav/portal_1.htm
    Once it's done maybe read the Performance and tuning guide:
    http://download.oracle.com/docs/cd/B14117_01/server.101/b10752/toc.htm
    (Chapter 19 explain ... how to interpret the explain plan ..)
    I wish you good chance and be patient: I'm working with Oracle for more that 10 years and I'm still learning! ...
    Rem: The Oracle website is full of interesting articles, and examples, just "search" your special points of interests ..

  • How to tune this SQL Query?

    Hi all expert out there,
    I am using Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production With the Partitioning, Real Application Clusters, OLAP, Data Mining and Real Application Testing options.
    I have this ERP 11i query took 3-4hours to execute. can anyone help to have a look at the Query so make it more faster? I am already out of my idea of solving this. Kindly helpzzz...
    SELECT
    /*+ first_rows(1)*/
    hou.name "OU" ,-- e.g. Seagate Technology USOP1 ,
    mc.description,
    oel.ordered_item "Product Part Number",
    oeh.order_number "Order Num",
    hp.party_name "Bill To Cust Name",
    hcsu.location "Ship To Cust Num",
    oel.line_number || '.' || oel.shipment_number "Order Line Num",
    ft.nls_territory "Ship To Country",
    mc.segment7 "designapplication",
    ccm.formattedcapacity||'GB' "Capacity",
    oel.attribute1 "Order Category",
    oel.Subinventory "Subinventory",
    to_char(oel.actual_shipment_date,'MM/DD/YYYY DY') "Shipment Date",
    NVL(oel.shipped_quantity,0) "Net Units"
    FROM
    oe_order_headers_all oeh
    ,oe_order_lines_all oel
    ,apps.hz_parties hp
    ,apps.hz_cust_accounts hca
    ,apps.hz_cust_site_uses_all hcsu
    ,apps.hz_cust_acct_sites_all hcas
    , apps.seaeng_ccfamilymodelinfo ccm
    ,apps.hz_party_sites hps
    ,apps.fnd_territories ft
    ,apps.hz_locations hl
    ,apps.hr_organization_units hou
    ,apps.mtl_parameters mp
    ,apps.mtl_item_categories mic
    ,apps.mtl_categories_b mc
    ,apps.fnd_lookup_values flv
    WHERE
    oeh.header_id = oel.header_id
    AND oel.flow_status_code = 'CLOSED'
    AND oeh.invoice_to_org_id = hcsu.site_use_id
    AND hl.country = ft.territory_code
    AND hps.location_id = hl.location_id
    AND hcas.party_site_id = hps.party_site_id
    AND hcsu.cust_acct_site_id = hcas.cust_acct_site_id
    AND hcas.cust_account_id = hca.cust_account_id
    AND hca.party_id = hp.party_id
    AND oeh.org_id = hou.organization_id
    AND oel.ship_from_org_id = mp.organization_id
    AND oel.inventory_item_id = mic.inventory_item_id
    AND mic.category_id = mc.category_id
    AND mic.category_set_id = 4
    AND mc.description = ccm.stmodelnumber
    AND flv.lookup_code = hca.sales_channel_code
    AND flv.lookup_type = 'SALES_CHANNEL'
    AND mc.segment7 IN ('PSG','ESG','NSG')
    AND flv.lookup_code NOT IN ('RTL','AD-RTL','EU')
    AND oel.attribute1 IN ('NB','NBEOL','NBSEA')
    AND oel.subinventory IN ('KFGI','AFGI','SFGIF','FGIF')
    AND hou.organization_id = 189
    AND trunc(oel.actual_shipment_date) between TO_DATE('01-Jan-2009','DD-MON-YYYY') and TO_DATE('31-JAN-2009','DD-MON-YYYY')
    regards,
    Lygine
    Edited by: user8989062 on Jun 7, 2010 6:39 PM

    5) The TKPROF output for this statement looks like the following:
    SELECT
    /*+ first_rows(1)*/
    hou.name "OU" ,-- e.g. Seagate Technology USOP1 ,
    mc.description,
    oel.ordered_item "Product Part Number",
    oeh.order_number "Order Num",
      hp.party_name  "Bill To Cust Name",
    hcsu.location "Ship To Cust Num",
    oel.line_number || '.' || oel.shipment_number "Order Line Num",
    ft.nls_territory  "Ship To Country",
    mc.segment7 "designapplication",
    ccm.formattedcapacity||'GB' "Capacity",
    oel.attribute1 "Order Category",
    oel.Subinventory "Subinventory",
    to_char(oel.actual_shipment_date,'MM/DD/YYYY DY') "Shipment Date",
    NVL(oel.shipped_quantity,0) "Net Units"
    FROM
          oe_order_headers_all oeh
         ,oe_order_lines_all   oel
         ,apps.hz_parties  hp
         ,apps.hz_cust_accounts  hca
         ,apps.hz_cust_site_uses_all   hcsu
         ,apps.hz_cust_acct_sites_all  hcas
        , apps.seaeng_ccfamilymodelinfo ccm
         ,apps.hz_party_sites hps
         ,apps.fnd_territories ft
         ,apps.hz_locations hl
         ,apps.hr_organization_units  hou
         ,apps.mtl_parameters  mp
         ,apps.mtl_item_categories mic
         ,apps.mtl_categories_b mc
         ,apps.fnd_lookup_values flv
    WHERE
               oeh.header_id = oel.header_id
    AND   oel.flow_status_code = 'CLOSED'
    AND   oeh.invoice_to_org_id = hcsu.site_use_id
    AND   hl.country = ft.territory_code
    AND   hps.location_id = hl.location_id
    AND   hcas.party_site_id = hps.party_site_id
    AND   hcsu.cust_acct_site_id = hcas.cust_acct_site_id
    AND   hcas.cust_account_id = hca.cust_account_id
    AND   hca.party_id   = hp.party_id
    AND   oeh.org_id = hou.organization_id
    AND   oel.ship_from_org_id = mp.organization_id
    AND   oel.inventory_item_id = mic.inventory_item_id
    AND   mic.category_id = mc.category_id
    AND   mic.category_set_id = 4
    AND   mc.description = ccm.stmodelnumber
    AND   flv.lookup_code = hca.sales_channel_code
    AND   flv.lookup_type = 'SALES_CHANNEL'
    AND mc.segment7 IN ('PSG','ESG','NSG')
    AND flv.lookup_code NOT IN ('RTL','AD-RTL','EU')
    AND oel.attribute1 IN ('NB','NBEOL','NBSEA')
    AND oel.subinventory IN ('KFGI','AFGI','SFGIF','FGIF')
    AND hou.organization_id = 189
    AND trunc(oel.actual_shipment_date) between TO_DATE('01-Jan-2009','DD-MON-YYYY') and TO_DATE('31-JAN-2009','DD-MON-YYYY')
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.79       0.79          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        7     22.23     240.85      19781     536082          0         592
    total        9     23.02     241.64      19781     536082          0         592
    Misses in library cache during parse: 1
    Optimizer mode: FIRST_ROWS
    Parsing user id: 173 
    Rows     Row Source Operation
        592  NESTED LOOPS  (cr=536082 pr=19781 pw=0 time=240822989 us)
        592   NESTED LOOPS  (cr=534885 pr=19776 pw=0 time=239960969 us)
        592    NESTED LOOPS  (cr=533688 pr=19776 pw=0 time=239902332 us)
        594     NESTED LOOPS  (cr=532493 pr=19776 pw=0 time=239811721 us)
        594      NESTED LOOPS  (cr=531892 pr=19776 pw=0 time=239794703 us)
        594       NESTED LOOPS  (cr=530693 pr=19772 pw=0 time=239325284 us)
        594        NESTED LOOPS  (cr=529498 pr=19770 pw=0 time=239152940 us)
        594         NESTED LOOPS  (cr=527709 pr=19766 pw=0 time=236850676 us)
        594          NESTED LOOPS  (cr=525920 pr=19764 pw=0 time=235640068 us)
       2990           NESTED LOOPS  (cr=516943 pr=19688 pw=0 time=195144282 us)
       2990            NESTED LOOPS  (cr=516936 pr=19688 pw=0 time=195120297 us)
       2990             NESTED LOOPS  (cr=510806 pr=19682 pw=0 time=194678671 us)
       3000              HASH JOIN  (cr=504799 pr=19669 pw=0 time=193829763 us)
       3000               NESTED LOOPS  (cr=472280 pr=360 pw=0 time=49218087 us)
          1                NESTED LOOPS  (cr=1029 pr=34 pw=0 time=1745829 us)
          1                 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS_TL (cr=424 pr=25 pw=0 time=1361442 us)
          1                  INDEX UNIQUE SCAN HR_ALL_ORGANIZATION_UNTS_TL_PK (cr=423 pr=25 pw=0 time=1361397 us)(object id 44637)
          1                 TABLE ACCESS BY INDEX ROWID HR_ALL_ORGANIZATION_UNITS (cr=605 pr=9 pw=0 time=384370 us)
          1                  INDEX UNIQUE SCAN HR_ORGANIZATION_UNITS_PK (cr=1 pr=0 pw=0 time=27 us)(object id 43498)
       3000                TABLE ACCESS BY INDEX ROWID OE_ORDER_LINES_ALL (cr=471251 pr=326 pw=0 time=47466249 us)
    156922                 INDEX RANGE SCAN OE_ORDER_LINES_N20 (cr=420 pr=7 pw=0 time=787919 us)(object id 37005064)
    263469               TABLE ACCESS FULL MTL_ITEM_CATEGORIES (cr=32519 pr=19309 pw=0 time=144242743 us)
       2990              TABLE ACCESS BY INDEX ROWID MTL_CATEGORIES_B (cr=6007 pr=13 pw=0 time=843569 us)
       3000               INDEX UNIQUE SCAN MTL_CATEGORIES_B_U1 (cr=3007 pr=2 pw=0 time=162636 us)(object id 37199)
       2990             TABLE ACCESS BY INDEX ROWID SEAENG_CCFAMILYMODELINFO (cr=6130 pr=6 pw=0 time=437784 us)
       2990              INDEX UNIQUE SCAN SEAENG_CCFAMILYMODELINFO_U1 (cr=2997 pr=0 pw=0 time=52872 us)(object id 35838918)
       2990            INDEX UNIQUE SCAN MTL_PARAMETERS_U1 (cr=7 pr=0 pw=0 time=16332 us)(object id 37657)
        594           TABLE ACCESS BY INDEX ROWID OE_ORDER_HEADERS_ALL (cr=8977 pr=76 pw=0 time=40491633 us)
       2990            INDEX UNIQUE SCAN OE_ORDER_HEADERS_U1 (cr=5987 pr=45 pw=0 time=2504442 us)(object id 41952)
        594          TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=1789 pr=2 pw=0 time=1208576 us)
        594           INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=1195 pr=1 pw=0 time=105934 us)(object id 25124976)
        594         TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCT_SITES_ALL (cr=1789 pr=4 pw=0 time=2300224 us)
        594          INDEX UNIQUE SCAN HZ_CUST_ACCT_SITES_U1 (cr=1195 pr=2 pw=0 time=152238 us)(object id 25124996)
        594        TABLE ACCESS BY INDEX ROWID HZ_PARTY_SITES (cr=1195 pr=2 pw=0 time=170875 us)
        594         INDEX UNIQUE SCAN HZ_PARTY_SITES_U1 (cr=601 pr=1 pw=0 time=104550 us)(object id 25124993)
        594       TABLE ACCESS BY INDEX ROWID HZ_LOCATIONS (cr=1199 pr=4 pw=0 time=467329 us)
        594        INDEX UNIQUE SCAN HZ_LOCATIONS_U1 (cr=601 pr=0 pw=0 time=57749 us)(object id 25124992)
        594      TABLE ACCESS BY INDEX ROWID FND_TERRITORIES (cr=601 pr=0 pw=0 time=15562 us)
        594       INDEX UNIQUE SCAN FND_TERRITORIES_U1 (cr=7 pr=0 pw=0 time=6492 us)(object id 33083)
        592     TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=1195 pr=0 pw=0 time=88925 us)
        594      INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=601 pr=0 pw=0 time=27607 us)(object id 81600)
        592    INDEX RANGE SCAN FND_LOOKUP_VALUES_U1 (cr=1197 pr=0 pw=0 time=56714 us)(object id 32878)
        592   TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=1197 pr=5 pw=0 time=860140 us)
        592    INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=599 pr=0 pw=0 time=51136 us)(object id 25126074)
    Elapsed times include waiting on following events:
      Event waited on                             Times   Max. Wait  Total Waited
      ----------------------------------------   Waited  ----------  ------------
      SQL*Net message to client                       7        0.00          0.00
      row cache lock                                  1        0.00          0.00
      gc current block 2-way                      24112        0.00         19.28
      gc cr grant 2-way                             406        0.00          0.19
      db file sequential read                       441        0.09         14.11
      gc cr block 2-way                             284        0.00          0.23
      gc cr block busy                              147        0.19          9.59
      gc current block congested                      2        0.00          0.00
      gc cr multi block request                   13079        0.00          3.98
      SQL*Net message from client                     7        0.27          1.87
      gc cr failure                                  39        0.00          0.02
      cr request retry                               39        0.98         38.27
      SQL*Net more data to client                    12        0.00          0.00
      db file scattered read                       2446        0.13        132.58
      gc cr disk read                                 5        0.00          0.00
    ********************************************************************************6) The DBMS_XPLAN.DISPLAY_CURSOR output:
    SQL> select * from table(dbms_xplan.display_cursor(null, null, 'ALLSTATS LAST'));
    PLAN_TABLE_OUTPUT
    SQL_ID  b8pfhfxscqn1m, child number 0
    SELECT  /*+ gather_plan_statistics */  hou.name "OU" ,-- e.g. Seagate Technology USOP1 ,  mc.descript
    oeh.order_number "Order Num",   hp.party_name  "Bill To Cust Name",  hcsu.location "Ship To Cust Num",
    Num",  ft.nls_territory  "Ship To Country",  mc.segment7 "designapplication",  ccm.formattedcapacity
    oel.Subinventory "Subinventory",  to_char(oel.actual_shipment_date,'MM/DD/YYYY DY') "Shipment Date",
    oe_order_headers_all oeh      ,oe_order_lines_all   oel      ,apps.hz_parties  hp      ,apps.hz_cust
    ,apps.hz_cust_acct_sites_all  hcas     , apps.seaeng_ccfamilymodelinfo ccm      ,apps.hz_party_sites hps
        ,apps.hr_organization_units  hou      ,apps.mtl_paramete
    Plan hash value: 414863479
    | Id  | Operation                                  | Name                           | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |  O
    |   1 |  NESTED LOOPS                              |                                |      1 |      1 |    592 |00:02:25.13 |     535K|  57267 |       |
    |   2 |   NESTED LOOPS                             |                                |      1 |      1 |    592 |00:02:24.22 |     533K|  57185 |      
    |   3 |    NESTED LOOPS                            |                                |      1 |      1 |    592 |00:02:24.15 |     532K|  57183 |     
    |   4 |     NESTED LOOPS                           |                                |      1 |      1 |    594 |00:02:24.00 |     531K|  57171 |     
    |   5 |      NESTED LOOPS                          |                                |      1 |      1 |    594 |00:02:23.98 |     530K|  57170 |    
    |   6 |       NESTED LOOPS                         |                                |      1 |      1 |    594 |00:02:23.49 |     529K|  57129 |   
    |   7 |        NESTED LOOPS                        |                                |      1 |      1 |    594 |00:02:23.08 |     528K|  57096 |  
    |   8 |         NESTED LOOPS                       |                                |      1 |      1 |    594 |00:02:22.51 |     526K|  57031 |       |
    |   9 |          NESTED LOOPS                      |                                |      1 |      1 |    594 |00:02:21.79 |     524K|  56968 |       |
    |  10 |           NESTED LOOPS                     |                                |      1 |      1 |   2990 |00:02:09.65 |     515K|  55703 |      
    |  11 |            NESTED LOOPS                    |                                |      1 |      1 |   2990 |00:02:09.62 |     515K|  55703 |     
    |  12 |             NESTED LOOPS                   |                                |      1 |      1 |   2990 |00:02:08.47 |     509K|  55599 |     
    |* 13 |              HASH JOIN                     |                                |      1 |      2 |   3000 |00:02:07.88 |     503K|  55516 |   974K
    |  14 |               NESTED LOOPS                 |                                |      1 |      2 |   3000 |00:01:39.07 |     471K|  23025 |   
    |  15 |                NESTED LOOPS                |                                |      1 |      1 |      1 |00:00:00.01 |       4 |      0 |  
    |  16 |                 TABLE ACCESS BY INDEX ROWID| HR_ALL_ORGANIZATION_UNITS_TL   |      1 |      1 |      1 |00:00
    |* 17 |                  INDEX UNIQUE SCAN         | HR_ALL_ORGANIZATION_UNTS_TL_PK |      1 |      1 |      1 |00:00:00.0
    |* 18 |                 TABLE ACCESS BY INDEX ROWID| HR_ALL_ORGANIZATION_UNITS      |      1 |      1 |      1 |00:00:
    |* 19 |                  INDEX UNIQUE SCAN         | HR_ORGANIZATION_UNITS_PK       |      1 |      1 |      1 |00:00:00.01
    |* 20 |                TABLE ACCESS BY INDEX ROWID | OE_ORDER_LINES_ALL             |      1 |      2 |   3000 |00:01
    |* 21 |                 INDEX RANGE SCAN           | OE_ORDER_LINES_N20             |      1 |  47575 |    156K|00:00:01.76 |     420
    |* 22 |               TABLE ACCESS FULL            | MTL_ITEM_CATEGORIES            |      1 |  96977 |    263K|00:00:28.49
    |* 23 |              TABLE ACCESS BY INDEX ROWID   | MTL_CATEGORIES_B               |   3000 |      1 |   2990 |00:00:0
    |* 24 |               INDEX UNIQUE SCAN            | MTL_CATEGORIES_B_U1            |   3000 |      1 |   3000 |00:00:00.21
    |  25 |             TABLE ACCESS BY INDEX ROWID    | SEAENG_CCFAMILYMODELINFO       |   2990 |      1 |   2990 |
    |* 26 |              INDEX UNIQUE SCAN             | SEAENG_CCFAMILYMODELINFO_U1    |   2990 |      1 |   2990 |00:00:
    |* 27 |            INDEX UNIQUE SCAN               | MTL_PARAMETERS_U1              |   2990 |      1 |   2990 |00:00:00.02 |    
    |* 28 |           TABLE ACCESS BY INDEX ROWID      | OE_ORDER_HEADERS_ALL           |   2990 |      1 |    594 |00:00:
    |* 29 |            INDEX UNIQUE SCAN               | OE_ORDER_HEADERS_U1            |   2990 |      1 |   2990 |00:00:04.29 |   
    |  30 |          TABLE ACCESS BY INDEX ROWID       | HZ_CUST_SITE_USES_ALL          |    594 |      1 |    594 |00:00:
    |* 31 |           INDEX UNIQUE SCAN                | HZ_CUST_SITE_USES_U1           |    594 |      1 |    594 |00:00:00.31 |   
    |  32 |         TABLE ACCESS BY INDEX ROWID        | HZ_CUST_ACCT_SITES_ALL         |    594 |      1 |    594 |00:00:
    |* 33 |          INDEX UNIQUE SCAN                 | HZ_CUST_ACCT_SITES_U1          |    594 |      1 |    594 |00:00:00.23 |   
    |  34 |        TABLE ACCESS BY INDEX ROWID         | HZ_PARTY_SITES                 |    594 |      1 |    594 |00:00:0
    |* 35 |         INDEX UNIQUE SCAN                  | HZ_PARTY_SITES_U1              |    594 |      1 |    594 |00:00:00.17 |     601
    |  36 |       TABLE ACCESS BY INDEX ROWID          | HZ_LOCATIONS                   |    594 |      1 |    594 |00:00:00.4
    |* 37 |        INDEX UNIQUE SCAN                   | HZ_LOCATIONS_U1                |    594 |      1 |    594 |00:00:00.17 |   
    |  38 |      TABLE ACCESS BY INDEX ROWID           | FND_TERRITORIES                |    594 |      1 |    594 |00:00:00
    |* 39 |       INDEX UNIQUE SCAN                    | FND_TERRITORIES_U1             |    594 |      1 |    594 |00:00:00.02
    |* 40 |     TABLE ACCESS BY INDEX ROWID            | HZ_CUST_ACCOUNTS               |    594 |      1 |    592 |00:00:0
    |* 41 |      INDEX UNIQUE SCAN                     | HZ_CUST_ACCOUNTS_U1            |    594 |      1 |    594 |00:00:00.04 |
    |* 42 |    INDEX RANGE SCAN                        | FND_LOOKUP_VALUES_U1           |    592 |      1 |    592 |00:00:00.06 |   
    |  43 |   TABLE ACCESS BY INDEX ROWID              | HZ_PARTIES                     |    592 |      1 |    592 |00:00:00.91 |  
    |* 44 |    INDEX UNIQUE SCAN                       | HZ_PARTIES_U1                  |    592 |      1 |    592 |00:00:00.40 |     599
    Predicate Information (identified by operation id):
      13 - access("OEL"."INVENTORY_ITEM_ID"="MIC"."INVENTORY_ITEM_ID")
      17 - access("HAOTL"."ORGANIZATION_ID"=189 AND "HAOTL"."LANGUAGE"=USERENV('LANG'))
           filter(DECODE("HR_SECURITY"."VIEW_ALL"(),'Y','TRUE',"HR_SECURITY"."SHOW_RECORD"('HR_ALL_ORGAN
      18 - filter("HAO"."BUSINESS_GROUP_ID"=DECODE("HR_GENERAL"."GET_XBG_PROFILE"(),'Y',"HAO"."BUSINESS_
      19 - access("HAO"."ORGANIZATION_ID"=189)
      20 - filter(("OEL"."FLOW_STATUS_CODE"='CLOSED' AND INTERNAL_FUNCTION("OEL"."ATTRIBUTE1") AND INTER
      21 - access("OEL"."SYS_NC00342$">=TO_DATE(' 2009-01-01 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "O
                  'syyyy-mm-dd hh24:mi:ss'))
      22 - filter("MIC"."CATEGORY_SET_ID"=4)
      23 - filter(("MC"."DESCRIPTION" IS NOT NULL AND INTERNAL_FUNCTION("MC"."SEGMENT7")))
      24 - access("MIC"."CATEGORY_ID"="MC"."CATEGORY_ID")
      26 - access("MC"."DESCRIPTION"="CCM"."STMODELNUMBER")
      27 - access("OEL"."SHIP_FROM_ORG_ID"="MP"."ORGANIZATION_ID")
      28 - filter("OEH"."ORG_ID"=189)
      29 - access("OEH"."HEADER_ID"="OEL"."HEADER_ID")
      31 - access("OEH"."INVOICE_TO_ORG_ID"="HCSU"."SITE_USE_ID")
      33 - access("HCSU"."CUST_ACCT_SITE_ID"="HCAS"."CUST_ACCT_SITE_ID")
      35 - access("HCAS"."PARTY_SITE_ID"="HPS"."PARTY_SITE_ID")
      37 - access("HPS"."LOCATION_ID"="HL"."LOCATION_ID")
      39 - access("HL"."COUNTRY"="FT"."TERRITORY_CODE")
      40 - filter(("HCA"."SALES_CHANNEL_CODE"<>'RTL' AND "HCA"."SALES_CHANNEL_CODE"<>'AD-RTL' AND "HCA".
      41 - access("HCAS"."CUST_ACCOUNT_ID"="HCA"."CUST_ACCOUNT_ID")
      42 - access("FLV"."LOOKUP_TYPE"='SALES_CHANNEL' AND "FLV"."LOOKUP_CODE"="HCA"."SALES_CHANNEL_CODE"
           filter(("FLV"."LOOKUP_CODE"<>'RTL' AND "FLV"."LOOKUP_CODE"<>'AD-RTL' AND "FLV"."LOOKUP_CODE"<
      44 - access("HCA"."PARTY_ID"="HP"."PARTY_ID")
    90 rows selected.
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              &nbs

  • How to tune past SQL query??

    Hi Team,
    Straight to issue --> I am seeing an query running for long time. When i begun to trace that particular query it got over by the time and now how to trace that specific SID and QUERY..
    I am working on 10.2.0.4 version DB..
    How to trace an sql query after its execution?? pls provide steps how to begin with..
    regards
    dkoracle

    dkoracle wrote:
    Hi Team,
    Straight to issue --> I am seeing an query running for long time. When i begun to trace that particular query it got over by the time and now how to trace that specific SID and QUERY..
    I am working on 10.2.0.4 version DB..
    How to trace an sql query after its execution?? pls provide steps how to begin with..Can not be done.
    ALTER SESSION SET SQL_TRACE=TRUE;
    -- run query again

  • Help with Performance tunning PL/SQL

    Hi All,
    I have a PL/SQL procedure, it works fine. No errors, and no bugs. However its taking forever to finish. I am using the concatenation operator (||), and I know its expensive. How can I improve performance to the procedure ?
    Here is the code
    create or replace
    PROCEDURE POST_ADDRESS_CLEANSE AS
    CURSOR C1 IS
    SELECT Z.ROW_ID,
            Z.NAME
    FROM  STGDATA.ACCOUNT_SOURCE Z;
    CURSOR  C2 IS
    SELECT  DISTINCT CLEANSED_NAME || CLEANSED_STREET_ADDRESS ||
            CLEANSED_STREET_ADDRESS_2 || CLEANSED_CITY || CLEANSED_STATE ||
            CLEANSED_POSTAL_CODE AS FULLRECORD
    FROM    STGDATA.ACCOUNT_SOURCE_CLEANSED;
    V_ROWID Number := 1;
    V_FLAG VARCHAR2(30);
    TEMP_ROW_ID VARCHAR2(10) := NULL;
    BEGIN
      -- This loop will update CLEANSED_NAME column in ACCOUNT_SOURCE_CLEANSED table.
      FOR X IN C1 LOOP
        TEMP_ROW_ID := TO_CHAR(X.ROW_ID);
      UPDATE STGDATA.ACCOUNT_SOURCE_CLEANSED A
      SET  A.CLEANSED_NAME = X.NAME
      WHERE A.ROW_ID = TEMP_ROW_ID;
        COMMIT;
      END LOOP;
      -- This loop will update columns EM_PRIMARY_FLAG, EM_GROUP_ID in ACCOUNT_SOURCE_CLEANSED table
      FOR Y IN C2 LOOP
        UPDATE  STGDATA.ACCOUNT_SOURCE_CLEANSED
        SET     EM_GROUP_ID = V_ROWID
        WHERE   CLEANSED_NAME || CLEANSED_STREET_ADDRESS || CLEANSED_STREET_ADDRESS_2 ||
                CLEANSED_CITY || CLEANSED_STATE || CLEANSED_POSTAL_CODE = Y.FULLRECORD;
        UPDATE  STGDATA.ACCOUNT_SOURCE_CLEANSED
        SET     EM_PRIMARY_FLAG = 'Y'
        WHERE   CLEANSED_NAME || CLEANSED_STREET_ADDRESS || CLEANSED_STREET_ADDRESS_2 ||
                CLEANSED_CITY || CLEANSED_STATE || CLEANSED_POSTAL_CODE = Y.FULLRECORD
        AND     ROWNUM = 1;
        V_ROWID := V_ROWID + 1;
        COMMIT;
      END LOOP;
      UPDATE  STGDATA.ACCOUNT_SOURCE_CLEANSED
      SET     EM_PRIMARY_FLAG = 'N'
      WHERE   EM_PRIMARY_FLAG IS NULL;
      COMMIT;
      --dbms_output.put_line('V_ROW:'||V_ROWID);
      --dbms_output.put_line('CLEANSED_NAME:'||Y.FULLRECORD); 
    END POST_ADDRESS_CLEANSE;
    Thanks in advance.
    Message was edited by: Rooney -- added code using syntax highlight

    I was able to modify my code a bit, however I don't see a way of not using loops.
    In the loop, I am updating 2 columns. EM_PRIMARY_FLAG, and EM_GROUP_ID. The data I am working with has duplicate records, and that is why I am using a distinct in the cursor. The requirements are is to make one record a primary record, and the rest are reference records. What makes my record primary is updating column EM_PRIMARY_FLAG with a 'Y', and updating EM_GROUP_ID with a number combines all duplicate records into a group.
    In the procedure, I am getting the distinct records, looping through each one, and then doing 2 updates:
    1 - Update EM_PRIMARY_FLAG to 'Y' where rownum = 1, this will set one record to be primary
    2 - Update EM_GROUP_ID to a number (V_ROWID := V_ROWID + 1) where V_ROWID starts from 1, to group all records into a set.
    Here is my latest code after modifying it:
    create or replace
    PROCEDURE POST_ADDRESS_CLEANSE AS
    CURSOR  C1 IS
    SELECT      DISTINCT NVL(CLEANSED_NAME, '') AS NAME_CLEANSED,
                      NVL(CLEANSED_STREET_ADDRESS, '') AS ADDRESS_CLEANSED,
                      NVL(CLEANSED_STREET_ADDRESS_2, '') AS ADDRESS2_CLEANSED,
                      NVL(CLEANSED_CITY, '') AS CITY_CLEANSED,
                      NVL(CLEANSED_STATE, '') AS STATE_CLEANSED,
                      NVL(CLEANSED_POSTAL_CODE, '') AS POSTAL_CODE_CLEANSED
    FROM        STGDATA.ACCOUNT_SOURCE_CLEANSED;
    V_ROWID Number := 1;
    V_FLAG VARCHAR2(30);
    TEMP_ROW_ID VARCHAR2(10) := NULL;
    BEGIN
        UPDATE STGDATA.ACCOUNT_SOURCE_CLEANSED A
        SET  A.CLEANSED_NAME = (SELECT   Z.NAME
                                                       FROM     STGDATA.ACCOUNT_SOURCE Z
                                                       WHERE    Z.ROW_ID = (SELECT  TO_NUMBER(B.ROW_ID)
                                                                                           FROM    STGDATA.ACCOUNT_SOURCE_CLEANSED B
                                                                                           WHERE   B.ROW_ID = A.ROW_ID));
        COMMIT;
      -- This loop will update columns EM_PRIMARY_FLAG, EM_GROUP_ID in ACCOUNT_SOURCE_CLEANSED table
      FOR Y IN C1 LOOP
        UPDATE   STGDATA.ACCOUNT_SOURCE_CLEANSED
        SET          EM_GROUP_ID = V_ROWID
        WHERE    CLEANSED_NAME = Y.NAME_CLEANSED
        AND          CLEANSED_STREET_ADDRESS = Y.ADDRESS_CLEANSED
        AND          CLEANSED_STREET_ADDRESS_2 = Y.ADDRESS2_CLEANSED
        AND          CLEANSED_CITY = Y.CITY_CLEANSED
        AND          CLEANSED_STATE = Y.STATE_CLEANSED
        AND          CLEANSED_POSTAL_CODE = Y.POSTAL_CODE_CLEANSED;
        UPDATE  STGDATA.ACCOUNT_SOURCE_CLEANSED
        SET     EM_PRIMARY_FLAG = 'Y'
        WHERE   CLEANSED_NAME = Y.NAME_CLEANSED
        AND     CLEANSED_STREET_ADDRESS = Y.ADDRESS_CLEANSED
        AND     CLEANSED_STREET_ADDRESS_2 = Y.ADDRESS2_CLEANSED
        AND     CLEANSED_CITY = Y.CITY_CLEANSED
        AND     CLEANSED_STATE = Y.STATE_CLEANSED
        AND     CLEANSED_POSTAL_CODE = Y.POSTAL_CODE_CLEANSED
        AND     ROWNUM = 1;
        V_ROWID := V_ROWID + 1;
      END LOOP;
      COMMIT;
      UPDATE  STGDATA.ACCOUNT_SOURCE_CLEANSED
      SET     EM_PRIMARY_FLAG = 'N'
      WHERE   EM_PRIMARY_FLAG IS NULL;
      COMMIT;
    END POST_ADDRESS_CLEANSE;
    Thanks
    Message was edited by: Rooney - Just added the code in SQL block using syntax highlight.

Maybe you are looking for

  • Delivery cost with multiple account assignment

    Dear SAP gurus, I found a problem in here. In my company we are using multiple account assignment in PO with multiple asset entered in one line. This PO actually will have customs and delivery cost inside of it, however it is not know at the time of

  • Need help in designing an  ATM simulation in java

    Hi, I've got to design a simulation of an ATM in java(client/server) and if anyone can help me with some coding, it would be nice :) I've got to design the interface on the client side and connect it to a database on a remote server. Any codings of a

  • How to find the backend  SQL query of the JSP page in OIC

    Does anybody how the best way to find the backend SQL QUERY of OIV JSP page?

  • Page item sequence numbers

    Maybe this one is for a future improvement... After reordering page items using the drag and drop feature the items all have a sequence number one greater than it's predecessor. This makes it hard to put a new item in between existing ones. Generatin

  • Standard Business Content & Data Targets for Case Management & CRM Survey

    Dear Friends, We are implementing CRM2007 with BW3.5 for reports for CRM Analytics. We require to Identify the Standard Data Targets & Standard Business Content (Reports) available for the following: 1. Case Management 2. CRM Survey (We have already