SQL Query tuning WITH in, not in, like

is there any alternative can i user for in, not in, like to optimize this sql statement.
SELECT TKTNUM||’~’||
CUSNAME||’~’||
DECODE(PRTY,0,’PRIORITY 00’, 1,’PRIORITY 01’ ,2,’PRIORITY 02’, 03,’PRIORITY 03’, 04,’PRIORITY04’,’OTHERS’) ||’~’||
TO_CHAR(NEW_TIME(CREATEDTTM,’GMT’,’&2’),’MM-DD-YYYY HH24:MI:SS’) ||’~’||
CURSTA||’~’||
CURSTS||’~’||
OTGTM||’~’||
TOPGRPNAME||’~’||
TO_CHAR(NEW_TIME(LASTUPDDTTM,’GMT’,’&2’),’MM-DD-YYYY HH24:MI:SS’) ||’~’||
RIM(REPLACE(REPLACE(PROBSUMMARY,’’,’’),CHR(10),’’||’~’
FROM T3TKTHEADER
WHERE
CURSTA NOT IN(‘RESOLVED’,’CLOSED’)
AND TOPGRPNAME IN(‘CASJC.OPS’,EMEA.WTO’,’INCHN.GSD-DBA’)
AND PRIT IN (‘0’,’1’,’2’)
AND CUSNAME NOT LIKE ‘% VANGENT%’
ORDER BY PRTY, CREATEDTTM DESC

Hi,
Welcome to the forum!
998537 wrote:
is there any alternative can i user for in, not in, like to optimize this sql statement.That depends on what you mean by "optimize".
There are lots of other ways to get the same results. For example
AND        prit          IN ('0', '1', '2')is equivalent to
AND       (    prit     = '0'
       OR   prit     = '1'
       OR   prit     = '2'
       )and
AND        cusname      NOT LIKE '% VANGENT%'is equivalent to
AND       INSTR (cusname, ' VANGENT')     = 0Some people might have a personal preference for these (or other) alternate forms. I don't; I find what you posted easier to understand and maintain. I would suggest you format your code, however.
Do you want it to run faster? See {message:id=9360003}
If you have some very particular requirements and data, then function-based indexes might help.
For example, if you often look for those same 3 values of prit, and, taken together, they account for less than 5% of the rows in the table, then a function-based index might make things faster. There's nothing in what you posted that looks espcially likely, however.

Similar Messages

  • SQL Query Tuning with  UNIONs

    HI:
    Can some please give some hints for the better performane of the following query:
    Select a.Hedged_Trade_ID, min(a.Trade_Date) Trade_Date, a.DealStartDate, a.MaturityDate
    FROM
    ( Select      distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From          CMS_FUTURES_TRANS a, CMS_PAS_ACCT_TRADE b
    Where     trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           b.org_trade_id = to_number(decode(substr(a.Hedged_Trade_Id,1,1), 'U', 0, a.Hedged_Trade_Id))
    AND           b.fgic_company = paramCompany
    UNION
    Select     distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From          CMS_SWAP_ALLOC a, CMS_PAS_ACCT_TRADE b
    Where           trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           b.org_trade_id = to_number(decode(substr(a.Hedged_Trade_Id,1,1), 'U', 0, a.Hedged_Trade_Id))
    AND           b.fgic_company = paramCompany
    UNION
    Select      distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   to_date('01/01/2001', 'mm/dd/yyyy') DealStartDate,
                   to_date('01/01/9999', 'mm/dd/yyyy') MaturityDate
    From           CMS_FUTURES_TRANS a
    Where      trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.hedged_trade_id IN (     Select     unassigned_id
              From     cms_fas_company
              Where      company_id = paramCompany)
    UNION
    Select      distinct a.Hedged_Trade_ID,
                   a.Trade_Date,
                   to_date('01/01/2001', 'mm/dd/yyyy') DealStartDate,
                   to_date('01/01/9999', 'mm/dd/yyyy') MaturityDate
    From          CMS_SWAP_ALLOC a
    Where           trunc(a.LAST_UPDATE) = to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.Trade_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           a.hedged_trade_id IN (     Select      unassigned_id
         From      cms_fas_company
         Where      company_id = paramCompany)
    UNION
    Select      distinct to_char(a.Org_Trade_id) Hedged_Trade_ID,
                   a.History_Date Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From           CMS_PAS_ACCT_TRADE_HIST a, CMS_PAS_ACCT_TRADE b
    Where           trunc(a.LAST_UPDATE) = to_date(paramRunDate + 1, 'mm/dd/yyyy')
    AND           a.History_Date < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           b.org_trade_id = a.org_trade_id
    AND           b.fgic_company = paramCompany
    UNION
    Select          distinct c.Hedged_Trade_id,
                   DECODE(ABS(to_date(a.History_Date,'mm/dd/yyyy') - to_date(c.Trade_Date,'mm/dd/yyyy')),                     to_date(a.History_Date,'mm/dd/yyyy') - to_date(c.Trade_Date,'mm/dd/yyyy'),                     to_date(a.History_Date,'mm/dd/yyyy'), to_date(c.Trade_Date,'mm/dd/yyyy')) Trade_Date,
                   b.Trade_Date DealStartDate,
                   b.Maturity_Date MaturityDate
    From           CMS_PAS_ACCT_TRADE_HIST a, CMS_PAS_ACCT_TRADE b, CMS_SWAP_ALLOC c
    Where           c.swap_trade_id = a.org_trade_id
    AND           b.org_trade_id = to_number(decode(substr(c.hedged_trade_id,1,1), 'U', 0, c.hedged_trade_id))
    AND           b.fgic_company = paramCompany
    AND           trunc(a.LAST_UPDATE) = to_date(paramRunDate + 1, 'mm/dd/yyyy')
    AND           trunc(a.History_Date) < to_date(paramRunDate, 'mm/dd/yyyy')
    AND           c.trade_date =      (     Select     MAX(trade_date)
              from     CMS_SWAP_ALLOC d
              Where     d.swap_trade_id = c.swap_trade_id
              AND     d.trade_date <= to_date(paramRunDate, 'mm/dd/yyyy')) ) a
    Group By a.Hedged_Trade_ID, a.DealStartDate, a.MaturityDate;

    Overall I suggest taking each one of your selects and running an explain plan to confirm they are behaving well in your database.
    How many rows does this select return? Big IN lists can be bad
        a.hedged_trade_id in(select unassigned_id
                                        from   cms_fas_company
                                        where  company_id = paramcompany)This can be rewritten
       and exists (select 1
                        from   cms_fas_company
                        where  company_id = paramcompany
                        and unassigned_id  =  a.hedged_trade_id in
    )

  • How to numberformat when using sql:query alogn with c:forEach JSTL tags

    Is there anyway to format the numeric values returned from the database when using <sql:query> alogn with <c:forEach> tags
    Here is my jsp code
    <sql:query..../>
    <c:forEach var="row" items="${queryResults.rows}">
    <tr>
    <td><c:out value="${row.COL1}" /></td>
    <td><c:out value="${row.COL2}" /></td>
    </tr>
    </c:forEach>
    Col1 values are numeric without any formats Eg: 1000, 10000, 1000000 etc.
    how can i format them to 1,000 , 10,1000 , 100,000 etc

    It is polite to mention what your answer was. These posts are not just here for you to ask questions, but to be used as a resource for other people to find answers. Saying "I solved it" with no details helps noone.
    I presume you discovered the JSTL <fmt:formatNumber> tag?

  • Ebooks on SQL Query tuning

    HI.
    can somebody post me any links for ebooks on SQL Query tuning techniques for either 10g or 11g.
    Tuning SQL Queries is something I would like to build my expertise on..
    There may be a lot of advisors like SQL Query Advisor, Access Advisor and so on...but nothing like tuning your queries manually to maximum optimization.
    Kindly help me in this !!

    Check Oracle docs http://download.oracle.com/docs/cd/B19306_01/server.102/b14211/toc.htm
    But you should really also be checking:
    http://www.asktom.oracle.com
    http://tkyte.blogspot.com
    AND all other blogs he referres to ( like Jonathan Lewis' site and blog ) you'll find many interesting articles over there, from 'the real world', regarding performance and tuning.
    Edited by: hoek on Mar 24, 2009 10:40 AM

  • Sql query tuning

    How can i do SQl QUERY TUNING in Oracle 9i Database

    Hello,
    How can i do SQl QUERY TUNING in Oracle 9i Database You can start through reviewing the Performance Tuning Guide;
    http://download.oracle.com/docs/cd/B10501_01/server.920/a96533/toc.htm
    Good Luck!
    Adith

  • SQL Performance tuning with wildcard Like condition

    Hi,
    I have performance issue with SQL query.
    When I am using "where emp_name like '%im%' " query is taking longer time than when I use "where emp_name like 'im%' " .
    With former condition query takes 40 sec , with later it takes around 1.5 sec.
    Both returns almost same no. of rows. We have function based index created on emp_name column.
    With wildcard at both ends query goes for full table scan.
    Can any one please suggest way so that query responce time can be reduced.?
    I even tried using hints but still it is going for full table scan instead of using index.

    >
    Hi Mandark,
    <I've rearranged your post>
    When I am using "where emp_name like '%im%' " query is taking longer time than when I use "where emp_name like 'im%' " .
    With wildcard at both ends query goes for full table scan.
    I even tried using hints but still it is going for full table scan instead of using index.
    With former condition query takes 40 sec , with later it takes around 1.5 sec.
    Both returns almost same no. of rows. We have function based index created on emp_name column.You are never going to be able to speed things up with a double wild card - or even 1 wild card at the beginning
    (unless you have some weird index reversing the string).
    With the double wild-card, the system has to search through the string character by character to see
    if there are any letter "i's" and then see if that letter is followed by an "m".
    That's using your standard B-tree index (see below).
    Can any one please suggest way so that query responce time can be reduced.?Yes, I think so - there is full-text indexing - see here:
    http://www.dba-oracle.com/oracle_tips_like_sql_index.htm and
    http://www.oracle-base.com/articles/9i/full-text-indexing-using-oracle-text-9i.php
    AFAIK, it's an extra-cost option - but you can have fun finding out all that for yourself ;)
    HTH,
    Paul...

  • Complex SQL query Tuning

    Hi Fellas,
    I am new to query tuning. I have a big query with a TKPROF details. It is just fetching 34 records and taking approx 30 mins.
    The TKPROF detail will follow after the query.
    SELECT /*+ NO_INDEX(A, RA_CUSTOMER_TRX_N11 ) USE_NL(TYPES,A) */ a.customer_trx_id CUSTOMER_TRX_ID ,
    a.trx_number TRX_NUMBER ,
    A . INTERFACE_HEADER_ATTRIBUTE5 BILLING_STAGE ,
    A . INTERFACE_HEADER_CONTEXT PBRR_TYPE ,
    A . INTERFACE_HEADER_ATTRIBUTE11 BILLING_TERM ,
    NVL ( TL . SEQUENCE_NUM , 1 ) TERM_SEQUENCE_NUMBER ,
    types.type TRX_TYPE ,
    l_types.meaning TRX_TYPE_NAME ,
    TYPES . ACCOUNTING_AFFECT_FLAG OPEN_RECEIVABLE_FLAG ,
    a.trx_date TRX_DATE , SHIP_TO_CUSTOMER_ID SHIP_TO_CUSTOMER_ID ,
    SHIP_TO_CONTACT_ID SHIP_TO_CONTACT_ID ,
    REMIT_TO_ADDRESS_ID REMIT_TO_ADDRESS_ID ,
    A . PRIMARY_SALESREP_ID PRIMARY_SALESREP_ID ,
    B . ACCOUNT_NUMBER CUSTOMER_NUMBER ,
    A . INTERNAL_NOTES INTERNAL_NOTES ,
    A . COMMENTS TRX_COMMENTS ,
    PREVIOUS_CUSTOMER_TRX_ID PREVIOUS_CUSTOMER_TRX_ID ,
    SHIP_TO_SITE_USE_ID SHIP_TO_SITE_USE_ID ,
    NVL ( PRINTING_COUNT , 0 ) PRINTING_COUNT ,
    PRINTING_ORIGINAL_DATE PRINTING_ORIGINAL_DATE ,
    PRINTING_LAST_PRINTED PRINTING_LAST_PRINTED ,
    PRINTING_PENDING PRINTING_PENDING ,
    LAST_PRINTED_SEQUENCE_NUM LAST_PRINTED_SEQUENCE_NUMBER ,
    START_DATE_COMMITMENT START_DATE_COMMITMENT ,
    END_DATE_COMMITMENT END_DATE_COMMITMENT ,
    INITIAL_CUSTOMER_TRX_ID INITIAL_CUSTOMER_TRX_ID ,
    A . INVOICE_CURRENCY_CODE INVOICE_CURRENCY_CODE ,
    A . REASON_CODE CREDIT_HEADER_REASON_CODE_CSTM ,
    A . TERM_ID TERM_ID ,
    A . SHIP_DATE_ACTUAL SHIP_DATE_ACTUAL ,
    A . SHIP_VIA SHIP_VIA ,
    A . WAYBILL_NUMBER WAYBILL_NUMBER ,
    A . PURCHASE_ORDER PURCHASE_ORDER_NUMBER ,
    A . PURCHASE_ORDER_REVISION PURCHASE_ORDER_REVISION ,
    A . PURCHASE_ORDER_DATE PURCHASE_ORDER_DATE ,
    A . CREATED_BY ORDER_CREATED_BY_CSTM ,
    P . DUE_DATE TERM_DUE_DATE_FROM_PS ,
    NVL ( TL . RELATIVE_AMOUNT , 100 ) * ( 100 / NVL ( T . BASE_AMOUNT , 100 ) ) TERM_RELATIVE_AMOUNT ,
    T . NAME TERM_NAME ,
    A . BILL_TO_CUSTOMER_ID BILL_TO_CUSTOMER_ID ,
    A . BILL_TO_CONTACT_ID BILL_TO_CONTACT_ID ,
    A . BILL_TO_SITE_USE_ID BILL_TO_SITE_USE_ID ,
    U_BILL . LOCATION BILL_TO_LOCATION ,
    SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) BILL_CUST_NAME ,
    RTRIM ( RPAD ( LOC . ADDRESS1 , 40 ) ) BILL_ADDRESS1 ,
    RTRIM ( RPAD ( LOC . ADDRESS2 , 40 ) ) BILL_ADDRESS2 ,
    RTRIM ( RPAD ( LOC . ADDRESS3 , 40 ) ) BILL_ADDRESS3 ,
    RTRIM ( RPAD ( LOC . ADDRESS4 , 40 ) ) BILL_ADDRESS4 ,
    LOC . CITY BILL_CITY , NVL ( LOC . STATE ,
    LOC . PROVINCE ) BILL_STATE ,
    LOC . POSTAL_CODE BILL_POSTAL_CODE ,
    LOC . COUNTRY BILL_COUNTRY ,
    U_BILL . TAX_REFERENCE BILL_SITE_TAX_REFERENCE ,
    PARTY . TAX_REFERENCE BILL_CUST_TAX_REFERENCE ,
    nvl(amount_line_items_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)), to_number(null)) TRX_LINE_AMOUNT ,
    nvl(tax_original, to_number(null)) TRX_TAX_AMOUNT ,
    nvl(freight_original, to_number(null)) TRX_FREIGHT_AMOUNT ,
    p.amount_due_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)) TRX_ALL_AMOUNT ,
    DECODE ( : P_ORDER_BY , 'TRX_NUMBER' , A . TRX_NUMBER , 'ADJUSTMENT_NUMBER' , A.TRX_NUMBER , 'CUSTOMER' , SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) , 'POSTAL_CODE' , LOC . POSTAL_CODE , A . TRX_NUMBER ) ORDER_BY ,
    LOC . ADDRESS1 BILL_TO_ADDRESS1 ,
    LOC . ADDRESS2 BILL_TO_ADDRESS2 ,
    LOC . ADDRESS3 BILL_TO_ADDRESS3 ,
    LOC . ADDRESS4 BILL_TO_ADDRESS4 ,
    LOC . STATE BILL_TO_STATE ,
    LOC . PROVINCE BILL_TO_PROVINCE ,
    T . description Term_Description ,
    a . org_id ,
    a . created_from
    FROM
    AR_ADJUSTMENTS COM_ADJ,
    AR_PAYMENT_SCHEDULES P,
    RA_CUST_TRX_LINE_GL_DIST REC,
    RA_CUSTOMER_TRX A,
    HZ_CUST_ACCOUNTS B,
    RA_TERMS T,
    RA_TERMS_LINES TL,
    RA_CUST_TRX_TYPES TYPES,
    AR_LOOKUPS L_TYPES,
    HZ_PARTIES      PARTY,
    HZ_CUST_ACCT_SITES A_BILL,
    HZ_PARTY_SITES PARTY_SITE,
    HZ_LOCATIONS LOC,
    HZ_CUST_SITE_USES U_BILL
    WHERE
    A.BILL_TO_CUSTOMER_ID = B.CUST_ACCOUNT_ID AND
    REC.CUSTOMER_TRX_ID = A.CUSTOMER_TRX_ID AND
    REC.LATEST_REC_FLAG = 'Y' AND
    REC.ACCOUNT_CLASS = 'REC' AND
    P.PAYMENT_SCHEDULE_ID + DECODE ( P.CLASS , 'INV' , 0 , '' ) = COM_ADJ.PAYMENT_SCHEDULE_ID (+) AND
    COM_ADJ.SUBSEQUENT_TRX_ID IS NULL AND 'C' = COM_ADJ.ADJUSTMENT_TYPE (+) AND
    A.COMPLETE_FLAG = 'Y' AND
    A.CUST_TRX_TYPE_ID = TYPES.CUST_TRX_TYPE_ID AND
    L_TYPES.LOOKUP_TYPE = 'INV/CM/ADJ' AND
    A.PRINTING_OPTION IN ( 'PRI' , 'REP' ) AND
    L_TYPES.LOOKUP_CODE = DECODE ( TYPES.TYPE , 'DEP' , 'INV' , TYPES.TYPE ) AND
    NVL ( P.TERMS_SEQUENCE_NUMBER , nvl ( TL.SEQUENCE_NUM , 0 ) ) = nvl ( TL.SEQUENCE_NUM , nvl ( p.terms_sequence_number , 0 ) ) AND
    DECODE ( P.PAYMENT_SCHEDULE_ID , '' , 0 , NVL ( T.PRINTING_LEAD_DAYS , 0 ) ) = 0 AND
    A.BILL_TO_SITE_USE_ID = U_BILL.SITE_USE_ID AND
    U_BILL.CUST_ACCT_SITE_ID = A_BILL.CUST_ACCT_SITE_ID AND
    A_BILL.party_site_id = party_site.party_site_id AND
    B.PARTY_ID = PARTY.PARTY_ID AND
    loc.location_id = party_site.location_id AND
    NVL ( P.AMOUNT_DUE_REMAINING , 0 ) <> 0 AND
    A.CUST_TRX_TYPE_ID = 2526 AND
    TYPES.TYPE = 'INV' AND
    A.TERM_ID = TL.TERM_ID (+) AND
    A.TERM_ID = T.TERM_ID (+) AND
    A.CUSTOMER_TRX_ID = P.CUSTOMER_TRX_ID (+) AND
    A.PRINTING_PENDING = 'Y' AND
    NVL ( TL.SEQUENCE_NUM , 1 ) > NVL ( A.LAST_PRINTED_SEQUENCE_NUM , 0 ) and 1 = 1 AND
    NOT EXISTS ( SELECT 'X' from ECE_TP_DETAILS ETD ,
                        ECE_TP_HEADERS ETH
    WHERE
    ETH.TP_HEADER_ID = A_BILL.TP_HEADER_ID AND
    ETD.TP_HEADER_ID = ETH.TP_HEADER_ID AND
    ETD.EDI_FLAG = 'Y' AND
    ETD.DOCUMENT_ID = 'INO' AND
    ETD.DOCUMENT_TYPE = DECODE ( TYPES.TYPE , 'CM' , DECODE ( A.PREVIOUS_CUSTOMER_TRX_ID , NULL , 'OACM' , 'CM' ) , TYPES.TYPE ) ) AND
    A.PRINTING_OPTION IN ( 'PRI' , 'REP' ) AND
    T.NAME not like 'PB%' AND
    1 = 1 AND
    MIT_AR.GE_AR_COAKLEY_ELIGIBLE ( 'NEW' , P.CUSTOMER_TRX_ID , A.ORG_ID ) = 0 AND
    A.org_id = TYPES.org_id UNION SELECT /*+ NO_INDEX(A, RA_CUSTOMER_TRX_N11 ) USE_NL(TYPES,A) */
    a.customer_trx_id , a.trx_number ,
    A . INTERFACE_HEADER_ATTRIBUTE5 ,
    A . INTERFACE_HEADER_CONTEXT ,
    A . INTERFACE_HEADER_ATTRIBUTE11 ,
    NVL ( P . TERMS_SEQUENCE_NUMBER , 1 ) ,
    types.type , l_types.meaning ,
    TYPES . ACCOUNTING_AFFECT_FLAG ,
    a.trx_date ,
    A . SHIP_TO_CUSTOMER_ID ,
    A . SHIP_TO_CONTACT_ID ,
    A . REMIT_TO_ADDRESS_ID ,
    A . PRIMARY_SALESREP_ID ,
    B . ACCOUNT_NUMBER ,
    A . INTERNAL_NOTES ,
    A . COMMENTS ,
    PREVIOUS_CUSTOMER_TRX_ID ,
    SHIP_TO_SITE_USE_ID ,
    NVL ( PRINTING_COUNT , 0 ) ,
    PRINTING_ORIGINAL_DATE ,
    PRINTING_LAST_PRINTED ,
    PRINTING_PENDING ,
    LAST_PRINTED_SEQUENCE_NUM ,
    START_DATE_COMMITMENT ,
    END_DATE_COMMITMENT ,
    INITIAL_CUSTOMER_TRX_ID ,
    A . INVOICE_CURRENCY_CODE ,
    A . REASON_CODE ,
    A . TERM_ID ,
    A . SHIP_DATE_ACTUAL ,
    A . SHIP_VIA ,
    A . WAYBILL_NUMBER ,
    A . PURCHASE_ORDER ,
    A . PURCHASE_ORDER_REVISION ,
    A . PURCHASE_ORDER_DATE ,
    A . CREATED_BY ,
    P . DUE_DATE ,
    NVL ( TL . RELATIVE_AMOUNT , 100 ) * ( 100 / NVL ( T . BASE_AMOUNT , 100 ) ) ,
    T . NAME , A . BILL_TO_CUSTOMER_ID ,
    A . BILL_TO_CONTACT_ID ,
    A . BILL_TO_SITE_USE_ID ,
    U_BILL . LOCATION BILL_TO_LOCATION ,
    SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) BILL_CUST_NAME ,
    RTRIM ( RPAD ( LOC . ADDRESS1 , 40 ) ) ,
    RTRIM ( RPAD ( LOC . ADDRESS2 , 40 ) ) ,
    RTRIM ( RPAD ( LOC . ADDRESS3 , 40 ) ) ,
    RTRIM ( RPAD ( LOC . ADDRESS4 , 40 ) ) ,
    LOC . CITY ,
    NVL ( LOC . STATE , LOC . PROVINCE ) ,
    LOC . POSTAL_CODE ,
    LOC . COUNTRY ,
    U_BILL . TAX_REFERENCE ,
    PARTY . TAX_REFERENCE ,
    nvl(amount_line_items_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)), to_number(null)) ,
    nvl(tax_original, to_number(null)) ,
    nvl(freight_original, to_number(null)) ,
    p.amount_due_original + decode(a.initial_customer_trx_id, '', 0, nvl(com_adj.amount,0)) ,
    DECODE ( : P_ORDER_BY , 'TRX_NUMBER' ,
    A . TRX_NUMBER , 'ADJUSTMENT_NUMBER' ,
    A.TRX_NUMBER , 'CUSTOMER' ,
    SUBSTRB ( PARTY . PARTY_NAME , 1 , 50 ) , 'POSTAL_CODE' ,
    LOC . POSTAL_CODE , A . TRX_NUMBER ) ORDER_BY ,
    LOC . ADDRESS1 ,
    LOC . ADDRESS2 ,
    LOC . ADDRESS3 ,
    LOC . ADDRESS4 ,
    LOC . STATE ,
    LOC . PROVINCE ,
    T . description ,
    a . org_id ,
    a . created_from
    FROM
    RA_TERMS_LINES TL,
    RA_CUST_TRX_TYPES TYPES,
    AR_LOOKUPS L_TYPES,
         HZ_CUST_ACCOUNTS B,
    HZ_PARTIES      PARTY,
    HZ_CUST_SITE_USES U_BILL,
    HZ_CUST_ACCT_SITES A_BILL,
    HZ_PARTY_SITES PARTY_SITE,
    HZ_LOCATIONS LOC,
    AR_ADJUSTMENTS COM_ADJ,
    RA_CUSTOMER_TRX A,
    AR_PAYMENT_SCHEDULES P,
    RA_TERMS T
    WHERE
    A.BILL_TO_CUSTOMER_ID = B.CUST_ACCOUNT_ID
    AND P.PAYMENT_SCHEDULE_ID + DECODE ( P.CLASS , 'INV' , 0 , '' ) = COM_ADJ.PAYMENT_SCHEDULE_ID (+) AND
    COM_ADJ.SUBSEQUENT_TRX_ID IS NULL AND
    'C' = COM_ADJ.ADJUSTMENT_TYPE (+) AND
    A.COMPLETE_FLAG = 'Y' AND
    A.CUSTOMER_TRX_ID = P.CUSTOMER_TRX_ID AND
    A.CUST_TRX_TYPE_ID = TYPES.CUST_TRX_TYPE_ID AND
    L_TYPES.LOOKUP_TYPE = 'INV/CM/ADJ' AND
    A.PRINTING_OPTION IN ( 'PRI' , 'REP' ) AND
    L_TYPES.LOOKUP_CODE = DECODE ( TYPES.TYPE , 'DEP' , 'INV' , TYPES.TYPE ) AND
    NVL ( T.PRINTING_LEAD_DAYS , 0 ) > 0 AND
    A.BILL_TO_SITE_USE_ID = U_BILL.SITE_USE_ID AND
    U_BILL.CUST_ACCT_SITE_ID = A_BILL.CUST_ACCT_SITE_ID AND
    A_BILL.PARTY_SITE_ID = PARTY_SITE.PARTY_SITE_ID AND
    B.PARTY_ID = PARTY.PARTY_ID AND
    LOC.LOCATION_ID = PARTY_SITE.LOCATION_ID AND
    NVL ( P.TERMS_SEQUENCE_NUMBER , TL.SEQUENCE_NUM ) = TL.SEQUENCE_NUM AND
    NVL ( P.AMOUNT_DUE_REMAINING , 0 ) <> 0 AND
    A.CUST_TRX_TYPE_ID = 2526 AND
    TYPES.TYPE = 'INV' AND
    T.TERM_ID = P.TERM_ID AND
    TL.TERM_ID (+) = T.TERM_ID AND
    A.PRINTING_PENDING = 'Y' AND
    P.TERMS_SEQUENCE_NUMBER > NVL ( A.LAST_PRINTED_SEQUENCE_NUM , 0 ) and 1 = 1 AND
    NOT EXISTS ( SELECT 'X' from
    ECE_TP_DETAILS ETD ,
    ECE_TP_HEADERS ETH
    WHERE
    ETH.TP_HEADER_ID = A_BILL.TP_HEADER_ID AND
    ETD.TP_HEADER_ID = ETH.TP_HEADER_ID AND
    ETD.EDI_FLAG = 'Y' AND
    ETD.DOCUMENT_ID = 'INO' AND
    ETD.DOCUMENT_TYPE = DECODE ( TYPES.TYPE , 'CM' , DECODE ( A.PREVIOUS_CUSTOMER_TRX_ID , NULL , 'OACM' , 'CM' ) , TYPES.TYPE ) ) AND
    NVL ( A.PRINTING_OPTION , 'REP' ) IN ( 'PRI' , 'REP' ) AND
    T.NAME not like 'PB%' AND 1 = 1 AND
    MIT_AR.GE_AR_COAKLEY_ELIGIBLE ( 'NEW' , P.CUSTOMER_TRX_ID , A.ORG_ID ) = 0 AND
    A.org_id = TYPES.org_id
    ORDER BY 60 ASC,68 ASC,2 ASC,3 ASC,4 ASC,5 ASC,44 ASC
    call count cpu elapsed disk query current rows
    Parse 1 0.06 0.08 0 364 0 0
    Execute 1 5.11 4.98 0 0 0 0
    Fetch 18 81.35 1375.73 203636 357435 0 34
    total 20 86.52 1380.80 203636 357799 0 34
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 25
    Number of plan statistics captured: 1
    Rows (1st) Rows (avg) Rows (max) Row Source Operation
    34 34 34 SORT UNIQUE (cr=569992 pr=227554 pw=0 time=1658548772 us)
    34 34 34 UNION-ALL (cr=569992 pr=227554 pw=0 time=773822949 us)
    34 34 34 FILTER (cr=228022 pr=107808 pw=0 time=773820837 us)
    34 34 34 FILTER (cr=228022 pr=107808 pw=0 time=773820394 us)
    34 34 34 NESTED LOOPS OUTER (cr=228022 pr=107808 pw=0 time=771070499 us)
    34 34 34 NESTED LOOPS (cr=227986 pr=107806 pw=0 time=773799735 us)
    34 34 34 NESTED LOOPS (cr=227882 pr=107806 pw=0 time=773742298 us)
    34 34 34 NESTED LOOPS (cr=227812 pr=107806 pw=0 time=773688021 us)
    34 34 34 NESTED LOOPS (cr=227708 pr=107806 pw=0 time=773636699 us)
    34 34 34 FILTER (cr=227638 pr=107806 pw=0 time=773566978 us)
    4616 4616 4616 NESTED LOOPS OUTER (cr=220679 pr=106836 pw=0 time=823009777 us)
    4610 4610 4610 NESTED LOOPS (cr=206836 pr=102712 pw=0 time=623651066 us)
    4610 4610 4610 NESTED LOOPS (cr=188367 pr=96450 pw=0 time=531982352 us)
    4610 4610 4610 NESTED LOOPS (cr=179145 pr=96449 pw=0 time=530980660 us)
    4610 4610 4610 FILTER (cr=165290 pr=96448 pw=0 time=529149297 us)
    4610 4610 4610 NESTED LOOPS OUTER (cr=165290 pr=96448 pw=0 time=530066499 us)
    4607 4607 4607 NESTED LOOPS (cr=156074 pr=96442 pw=0 time=762671095 us)
    4615 4615 4615 NESTED LOOPS (cr=146842 pr=96441 pw=0 time=751255741 us)
    4615 4615 4615 NESTED LOOPS (cr=137610 pr=96435 pw=0 time=751144978 us)
    4615 4615 4615 NESTED LOOPS (cr=123763 pr=96435 pw=0 time=750949543 us)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUSTOMER_TRX_ALL (cr=114531 pr=96435 pw=0 time=750675597 us)
    *329687 329687 329687 INDEX RANGE SCAN RA_CUSTOMER_TRX_N17 (cr=6232 pr=6046 pw=0 time=31982795 us)(object id 4788)* 4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_TYPES_ALL (cr=9232 pr=0 pw=0 time=239450 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_CUST_TRX_TYPES_U1 (cr=4617 pr=0 pw=0 time=160139 us)(object id 55731)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID FND_LOOKUP_VALUES (cr=13847 pr=0 pw=0 time=180337 us)
    4615 4615 4615 INDEX UNIQUE SCAN FND_LOOKUP_VALUES_U1 (cr=9232 pr=0 pw=0 time=135717 us)(object id 491822)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_TERMS_B (cr=9232 pr=6 pw=0 time=155074 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_TERMS_B_U1 (cr=4617 pr=0 pw=0 time=60566 us)(object id 55734)
    4607 4607 4607 TABLE ACCESS BY INDEX ROWID RA_TERMS_TL (cr=9232 pr=1 pw=0 time=181115 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_TERMS_TL_U1 (cr=4617 pr=1 pw=0 time=82153 us)(object id 55755)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID RA_TERMS_LINES (cr=9216 pr=6 pw=0 time=259864 us)
    4610 4610 4610 INDEX RANGE SCAN RA_TERMS_LINES_U1 (cr=4609 pr=1 pw=0 time=104110 us)(object id 6703)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=13855 pr=1 pw=0 time=1350357 us)
    4610 4610 4610 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=9222 pr=1 pw=0 time=434079 us)(object id 44870)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCT_SITES_ALL (cr=9222 pr=1 pw=0 time=1024466 us)
    4610 4610 4610 INDEX UNIQUE SCAN HZ_CUST_ACCT_SITES_U1 (cr=4612 pr=1 pw=0 time=337477 us)(object id 46883)
    4610 4610 4610 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_LINE_GL_DIST_ALL (cr=18469 pr=6262 pw=0 time=86754686 us)
    4610 4610 4610 INDEX RANGE SCAN RA_CUST_TRX_LINE_GL_DIST_N6 (cr=13836 pr=4061 pw=0 time=55844483 us)(object id 1510967)
    4616 4616 4616 TABLE ACCESS BY INDEX ROWID AR_PAYMENT_SCHEDULES_ALL (cr=13843 pr=4124 pw=0 time=50689789 us)
    4616 4616 4616 INDEX RANGE SCAN AR_PAYMENT_SCHEDULES_N2 (cr=9233 pr=1494 pw=0 time=15169412 us)(object id 4877)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=70 pr=0 pw=0 time=24450 us)
    34 34 34 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=36 pr=0 pw=0 time=5962 us)(object id 85278)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_PARTY_SITES (cr=104 pr=0 pw=0 time=58609 us)
    34 34 34 INDEX UNIQUE SCAN HZ_PARTY_SITES_U1 (cr=70 pr=0 pw=0 time=31189 us)(object id 45527)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_LOCATIONS (cr=70 pr=0 pw=0 time=52502 us)
    34 34 34 INDEX UNIQUE SCAN HZ_LOCATIONS_U1 (cr=36 pr=0 pw=0 time=23883 us)(object id 45692)
    34 34 34 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=104 pr=0 pw=0 time=49042 us)
    34 34 34 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=70 pr=0 pw=0 time=22106 us)(object id 757879)
    0 0 0 TABLE ACCESS BY INDEX ROWID AR_ADJUSTMENTS_ALL (cr=36 pr=2 pw=0 time=19638 us)
    0 0 0 INDEX RANGE SCAN AR_ADJUSTMENTS_N3 (cr=36 pr=2 pw=0 time=19271 us)(object id 5473)
    0 0 0 NESTED LOOPS (cr=0 pr=0 pw=0 time=9 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_HEADERS_U1 (cr=0 pr=0 pw=0 time=7 us)(object id 46119)
    0 0 0 TABLE ACCESS BY INDEX ROWID ECE_TP_DETAILS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_DETAILS_U2 (cr=0 pr=0 pw=0 time=0 us)(object id 6271)
    0 0 0 FILTER (cr=341970 pr=119746 pw=0 time=883424760 us)
    0 0 0 FILTER (cr=341970 pr=119746 pw=0 time=883424758 us)
    0 0 0 NESTED LOOPS OUTER (cr=341970 pr=119746 pw=0 time=883424757 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424752 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424749 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424742 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424740 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424735 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424731 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424726 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424724 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424721 us)
    0 0 0 NESTED LOOPS (cr=341970 pr=119746 pw=0 time=883424715 us)
    34 34 34 NESTED LOOPS (cr=341900 pr=119746 pw=0 time=882400482 us)
    4615 4615 4615 NESTED LOOPS (cr=123763 pr=93484 pw=0 time=674293279 us)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUSTOMER_TRX_ALL (cr=114531 pr=93484 pw=0 time=674030164 us)
    329687 329687 329687 INDEX RANGE SCAN RA_CUSTOMER_TRX_N17 (cr=6232 pr=6130 pw=0 time=36621510 us)(object id 4788)
    4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_TYPES_ALL (cr=9232 pr=0 pw=0 time=249893 us)
    4615 4615 4615 INDEX UNIQUE SCAN RA_CUST_TRX_TYPES_U1 (cr=4617 pr=0 pw=0 time=169485 us)(object id 55731)
    34 34 34 TABLE ACCESS BY INDEX ROWID AR_PAYMENT_SCHEDULES_ALL (cr=218137 pr=26262 pw=0 time=310068873 us)
    3304 3304 3304 INDEX RANGE SCAN AR_PAYMENT_SCHEDULES_N2 (cr=214841 pr=24355 pw=0 time=283359418 us)(object id 4877)
    0 0 0 TABLE ACCESS BY INDEX ROWID RA_TERMS_B (cr=70 pr=0 pw=0 time=829 us)
    34 34 34 INDEX UNIQUE SCAN RA_TERMS_B_U1 (cr=36 pr=0 pw=0 time=527 us)(object id 55734)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_CUST_SITE_USES_ALL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_CUST_SITE_USES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 44870)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCT_SITES_ALL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_CUST_ACCT_SITES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 46883)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_CUST_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_CUST_ACCOUNTS_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 85278)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_PARTIES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_PARTIES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 757879)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_PARTY_SITES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_PARTY_SITES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 45527)
    0 0 0 TABLE ACCESS BY INDEX ROWID HZ_LOCATIONS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN HZ_LOCATIONS_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 45692)
    0 0 0 TABLE ACCESS BY INDEX ROWID RA_TERMS_TL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN RA_TERMS_TL_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 55755)
    0 0 0 TABLE ACCESS BY INDEX ROWID RA_TERMS_LINES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX RANGE SCAN RA_TERMS_LINES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 6703)
    0 0 0 TABLE ACCESS BY INDEX ROWID FND_LOOKUP_VALUES (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN FND_LOOKUP_VALUES_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 491822)
    0 0 0 TABLE ACCESS BY INDEX ROWID AR_ADJUSTMENTS_ALL (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX RANGE SCAN AR_ADJUSTMENTS_N3 (cr=0 pr=0 pw=0 time=0 us)(object id 5473)
    0 0 0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_HEADERS_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 46119)
    0 0 0 TABLE ACCESS BY INDEX ROWID ECE_TP_DETAILS (cr=0 pr=0 pw=0 time=0 us)
    0 0 0 INDEX UNIQUE SCAN ECE_TP_DETAILS_U2 (cr=0 pr=0 pw=0 time=0 us)(object id 6271)
    Elapsed times include waiting on following events:
    Event waited on Times Max. Wait Total Waited
    ---------------------------------------- Waited ---------- ------------
    row cache lock 6 0.00 0.00
    SQL*Net message to client 18 0.00 0.00
    SQL*Net more data to client 2 0.00 0.00
    gc current block 2-way 5375 0.02 3.76
    gc cr grant 2-way 49175 0.05 24.09
    db file sequential read 203636 0.51 1289.70
    gcs drm freeze in enter server mode 7 0.28 1.30
    latch: KCL gc element parent latch 4 0.00 0.00
    gc remaster 2 0.07 0.12
    latch: gcs resource hash 26 0.00 0.00
    latch free 1 0.00 0.00
    gc cr block 2-way 9 0.00 0.00
    latch: cache buffers chains 1 0.00 0.00
    SQL*Net message from client 18 0.00 0.00
    Thanks in Advance..

    Hi,
    it looks like your problem stems from the wrong choice of the driving table:
    4615 4615 4615            NESTED LOOPS (cr=123763 pr=96435 pw=0 time=750949543 us)
    4615 4615 4615              TABLE ACCESS BY INDEX ROWID RA_CUSTOMER_TRX_ALL (cr=114531 pr=96435 pw=0 time=750675597 us)
    329687 329687 329687       INDEX RANGE SCAN RA_CUSTOMER_TRX_N17 (cr=6232 pr=6046 pw=0 time=31982795 us)(object id 4788) 4615 4615 4615 TABLE ACCESS BY INDEX ROWID RA_CUST_TRX_TYPES_ALL (cr=9232 pr=0 pw=0 time=239450 us)
    4615 4615 4615              INDEX UNIQUE SCAN RA_CUST_TRX_TYPES_U1 (cr=4617 pr=0 pw=0 time=160139 us)(object id 55731)
    4615 4615 4615              TABLE ACCESS BY INDEX ROWID FND_LOOKUP_VALUES (cr=13847 pr=0 pw=0 time=180337 us)Here, you are spending 750 seconds retriveing 329k rows, 98% of which are rejected on the next step. And the reason this is happening is because the optimizer underestimates the cardinatlity of this step by a factor of x50 (6.2k estimated, 329k actual value). Find out why this is happening or post relevant diagnostic information here (predicates, columns stats etc.), and fix this -- this should make your query twice as fast.
    The second half is coming from the SORT UNIQUE step. This looks weird: why on Earth it would take over 800 seconds to sort 34 rows?! Maybe there is something in the plan that I'm missing -- it's really hard to read because it's not formatted. Please obtain a plan with rowsource stats using dbms_xplan.display_cursor and post it here (google dbms_xplan.display_cursor allstats last if not sure how to do that).
    Best regards,
    Nikolay

  • SQL query slow with call to function

    I have a SQL query that will return in less than a second or two with a function in-line selected in the "from" clause of the statement. As soon as I select that returned value in the SQL statement, the statement takes from anywhere from 2 to 5 minutes to return. Here is a simplified sample from the statement:
    This statement returns in a second or 2.
    select A.pk_id
    from stu_schedule A, stu_school B, stu_year C, school_year D,
    (select calc_ytd_class_abs2(Z.PK_ID,'U') ytd_unx
    from stu_schedule Z) II
    where B.pk_id = A.fk_stu_school
    and C.pk_id = B.fk_stu_year
    and D.pk_id = C.year
    and D.school_year = '2011';
    if I add this function call in, the statement runs extremely poor.
    select A.pk_id,
    II.ytd_unx
    from stu_schedule A, stu_school B, stu_year C, school_year D,
    (select calc_ytd_class_abs2(Z.PK_ID,'U') ytd_unx
    from stu_schedule Z) II
    where B.pk_id = A.fk_stu_school
    and C.pk_id = B.fk_stu_year
    and D.pk_id = C.year
    and D.school_year = '2011';
    Here is the function that is called:
    create or replace FUNCTION calc_ytd_class_abs2 (p_fk_stu_schedule in varchar2,
    p_legality in varchar2) return number IS
    l_days_absent number := 0;
    CURSOR get_class_abs IS
    select (select nvl(max(D.days_absent),'0')
    from cut_code D
    where D.pk_id = C.fk_cut_code
    and (D.legality = p_legality
    or p_legality = '%')) days_absent
    from stu_schedule_detail B, stu_class_attendance C
    where B.fk_stu_schedule = p_fk_stu_schedule
    and C.fk_stu_schedule_detail = B.pk_id;
    BEGIN
    FOR x in get_class_abs LOOP
    l_days_absent := l_days_absent + x.days_absent;
    END LOOP;
    return (l_days_absent);
    END calc_ytd_class_abs2;

    Query returns anywhere from 6000 to 32000 rows. For each of those rows a parameter is passed in to 4 different functions to get ytd totals. When I call the functions in the in-line view but do not select from them in the main SQL, the report (oh, this is Application Express 4.0 interactive reports, just an FYI) runs fast. The report comes back in a few seconds. But when I select from the in-line view to display those ytd totals, the report runs extremely slow. I know there are the articles about context switching and how mixing SQL with PL/SQL performs poorly. So I tried a pipeline table function where the function for the ytd totals populate the columns of the pipeline table and I select from the pipeline table in the SQL query in the interactive report. That seemed to perform a little worse from what I can tell.
    Thanks for any help you can offer.

  • SQL Query Result with Random Sorting

    Hi Experts,
    My Oracle Version : Oracle9i
    I have three tables which are given below,
    Table Name:     check_team
    team_id      team_code
    100          A
    101          B
    102          C
    103          D
    Table Name:     check_product
    product_id     product_code
    1          XXX
    2          XYZ
    Table Name:     check_team_products
    tprod_id     tprod_team_id     tprod_product_id
    1          100          1
    2          100          2
    3          101          1
    4          101          2
    5          102          1
    6          102          2
    7          103          1
    8          103          2
    Required Output First Time:
    team_id   team_code   product_id   product_code
    100       A           1             XXX
    101       B           2             XYZ
    102       A           1             XXX
    103       B           2             XYZ
    Required Output Second Time:
    team_id   team_code   product_id   product_code
    100       B           2             XYZ
    101       A           1             XXX
    102       B           2             XYZ
    103       A           1             XXXI need the result as Required Output specified above and also the result has to be random too.. Can someone help me in writing a SQL Query to get results as that?
    Added Oracle Version

    So, is it something like this you want?
    SQL> ed
    Wrote file afiedt.buf
      1  with check_team as (select 100 as team_id, 'A' as team_code from dual union all
      2                      select 101, 'B' from dual union all
      3                      select 102, 'C' from dual union all
      4                      select 103, 'D' from dual)
      5      ,check_product as (select 1 as product_id, 'XXX' as product_code from dual union all
      6                         select 2, 'XYZ' from dual)
      7      ,check_team_products as (select 1 as tprod_id, 100 as tprod_team_id, 1 as tprod_product_id from dual union all
      8                               select 2, 100, 2 from dual union all
      9                               select 3, 101, 1 from dual union all
    10                               select 4, 101, 2 from dual union all
    11                               select 5, 102, 1 from dual union all
    12                               select 6, 102, 2 from dual union all
    13                               select 7, 103, 1 from dual union all
    14                               select 8, 103, 2 from dual)
    15  --
    16  -- end of test data
    17  --
    18  select team_id, team_code, product_id, product_code
    19  from (
    20        select t.team_id, t.team_code, p.product_id, p.product_code
    21              ,row_number() over (partition by team_id order by dbms_random.random()) as rn
    22        from check_team t join check_team_products tp on (tp.tprod_team_id = t.team_id)
    23                          join check_product p on (p.product_id = tp.tprod_product_id)
    24       )
    25* where rn = 1
    SQL> /
       TEAM_ID T PRODUCT_ID PRO
           100 A          2 XYZ
           101 B          1 XXX
           102 C          2 XYZ
           103 D          1 XXX
    SQL> /
       TEAM_ID T PRODUCT_ID PRO
           100 A          2 XYZ
           101 B          1 XXX
           102 C          2 XYZ
           103 D          1 XXX
    SQL> /
       TEAM_ID T PRODUCT_ID PRO
           100 A          1 XXX
           101 B          2 XYZ
           102 C          1 XXX
           103 D          1 XXX

  • Query panel with table not working in taskflow region

    I've created an adf query panel with table(using all queriable attributes criteria) in a page fragment and at design time it's showing properly. But when inside a region in a jspx page, the search, save and reset buttons of the query panel are not rendering when i'm running the page. Please help.

    Its hard to give any solution without any information like your JDev version, what happens if you drop the query directly in the jspx page, what exception you get in console etc.
    -Arun

  • SQL query result with HTML Data in output

    Hello,
    I have a SQL table , in one column I store HTML data. I need to query the table and get the HTML data in the columns that have 'HREF'. The output shows as grid on the sql management studio, however when I export it to excel, the HTML data does not get copied
    correctly, since there are HTML tags etc.
    How can I export the report correctly from SQL ?

    Hello,
    The HTML data is stored in a column with datatype as nvarchar(max). Sample data in the column is shown below. It is with formatting etc and is rendered as is on the web page. the business wants to generate a quick report so that they can see the pages that
    have links displayed. I can do that by querying the columns that have a 'HREF' in the text.
    Can I get the exact HREF values using just sql query? There can be more than one links on a page.
    Also, If I just want to copy the whole column and paste it on excel, how can I do that? If I copy the data below and paste, it does not get copied in one cell.. it spreads across multiple cells, so the report does not make any sense.
    <br />
    <table border="0" cellpadding="0" cellspacing="0" style="width: 431pt; border-collapse: collapse;" width="574">
    <tbody>
    <tr height="19" style="height: 14.25pt; ">
    <td height="19" style="border: 0px blue; width: 431pt; height: 14.25pt; background-color: transparent;" width="574"><a href="https:"><u><font color="#0066cc" face="Calibri">ax </font></u></a></td>
    </tr>
    </tbody>
    <colgroup>
    <col style="width: 431pt; " width="574" />
    </colgroup>
    </table>

  • SQL Query execution with LISTAGG

    Hello,
    I am trying to get some data using LISTAGG functionality.
    I have below query with works fine with the table, but not with the rowsource which is based on same table.
    SELECT COL1,LISTAGG(COL3, ',') WITHIN GROUP (ORDER BY COL2) AS fieldName FROM IOP_Sample_RS GROUP BY COL1it gives parsing error
    line 1:22: expecting "FROM", found '('col 22 is '(' of LISTAGG function.
    One more Question
    Does UNION, MINUS works with RSQL in IOP?
    Thanks,
    Sumant Chhunchha.
    Edited by: Sumant on Jun 15, 2011 5:04 AM

    LISTAGG, UNION, MINUS is not supported with RSQL, instead you can massage your SQL query to load data into IOP and then use a simple RSQL query.

  • Sql query tuning required

    Dear Experts,
    I have a sql query which taking more than 2 hour of time ot execute.
    the explain plan is :
    PLAN_TABLE_OUTPUT
    Plan hash value: 2694368390
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 379 | 44561 (1)| 00:08:55 |
    | 1 | INLIST ITERATOR | | | | | |
    | 2 | TABLE ACCESS BY INDEX ROWID | OPS_CITY_MAST | 2 | 30 | 5 (0)| 00:00:01 |
    |* 3 | INDEX UNIQUE SCAN | OPS_CITY_MAST_IDX_01 | 2 | | 3 (0)| 00:00:01 |
    | 4 | TABLE ACCESS BY INDEX ROWID | OPS_BR_MAST | 1 | 16 | 2 (0)| 00:00:01 |
    |* 5 | INDEX UNIQUE SCAN | OPS_BR_MAST_IDX_01 | 1 | | 1 (0)| 00:00:01 |
    | 6 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
    |* 7 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
    | 8 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
    |* 9 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
    | 10 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
    |* 11 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
    | 12 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
    |* 13 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
    | 14 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
    |* 15 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
    | 16 | TABLE ACCESS BY INDEX ROWID | OPS_CHG_GROUP_AMT | 1 | 15 | 4 (0)| 00:00:01 |
    |* 17 | INDEX UNIQUE SCAN | OPS_CHG_GROUP_AMT_IDX_02 | 1 | | 3 (0)| 00:00:01 |
    | 18 | SORT GROUP BY NOSORT | | 1 | 31 | 10 (0)| 00:00:01 |
    | 19 | NESTED LOOPS | | | | | |
    | 20 | NESTED LOOPS | | 2 | 62 | 10 (0)| 00:00:01 |
    | 21 | TABLE ACCESS BY INDEX ROWID | OPS_UULT_WB_DTLS | 2 | 24 | 6 (0)| 00:00:01 |
    |* 22 | INDEX RANGE SCAN | OPS_UULT_WB_DTLS_IDX_03 | 2 | | 3 (0)| 00:00:01 |
    |* 23 | INDEX UNIQUE SCAN | OPS_UPD_ULT_IDX_01 | 1 | | 1 (0)| 00:00:01 |
    |* 24 | TABLE ACCESS BY INDEX ROWID | OPS_UPD_ULT | 1 | 19 | 2 (0)| 00:00:01 |
    | 25 | NESTED LOOPS | | | | | |
    | 26 | NESTED LOOPS | | 1 | 379 | 44561 (1)| 00:08:55 |
    | 27 | NESTED LOOPS | | 1 | 360 | 44559 (1)| 00:08:55 |
    | 28 | NESTED LOOPS | | 1 | 333 | 44558 (1)| 00:08:55 |
    | 29 | NESTED LOOPS | | 1 | 312 | 44557 (1)| 00:08:55 |
    | 30 | NESTED LOOPS | | 1 | 302 | 44555 (1)| 00:08:55 |
    | 31 | NESTED LOOPS | | 1 | 281 | 44553 (1)| 00:08:55 |
    |* 32 | HASH JOIN | | 4383 | 1112K| 35779 (2)| 00:07:10 |
    |* 33 | HASH JOIN RIGHT OUTER | | 4383 | 1070K| 34631 (2)| 00:06:56 |
    | 34 | TABLE ACCESS FULL | OPS_CUST_CNTR | 7270 | 94510 | 68 (0)| 00:00:01 |
    |* 35 | HASH JOIN | | 4383 | 1014K| 34562 (2)| 00:06:55 |
    | 36 | NESTED LOOPS OUTER | | 4414 | 875K| 33135 (2)| 00:06:38 |
    |* 37 | HASH JOIN | | 4414 | 827K| 32963 (2)| 00:06:36 |
    | 38 | TABLE ACCESS FULL | OPS_ST_UN_MAST | 36 | 504 | 3 (0)| 00:00:01 |
    |* 39 | HASH JOIN | | 4414 | 767K| 32959 (2)| 00:06:36 |
    |* 40 | HASH JOIN | | 4414 | 543K| 28417 (2)| 00:05:41 |
    | 41 | NESTED LOOPS | | | | | |
    | 42 | NESTED LOOPS | | 4414 | 495K| 26483 (2)| 00:05:18 |
    |* 43 | HASH JOIN | | 4949 | 483K| 16641 (2)| 00:03:20 |
    |* 44 | TABLE ACCESS BY INDEX ROWID| OPS_WAYBL | 4423 | 367K| 2292 (1)| 00:00:28 |
    |* 45 | INDEX RANGE SCAN | OPS_WAYBL_IDX_11 | 5050 | | 16 (0)| 00:00:01 |
    | 46 | TABLE ACCESS FULL | OPS_PULTD_WB_DTLS | 4474K| 64M| 14298 (2)| 00:02:52 |
    |* 47 | INDEX UNIQUE SCAN | OPS_TS_RECONSILE_IDX_02 | 1 | | 1 (0)| 00:00:01 |
    |* 48 | TABLE ACCESS BY INDEX ROWID | OPS_TS_RECONSILE | 1 | 15 | 2 (0)| 00:00:01 |
    | 49 | TABLE ACCESS FULL | OPS_CC_CORCEE_ADDR | 998K| 10M| 1922 (2)| 00:00:24 |
    | 50 | TABLE ACCESS FULL | OPS_ADDR_MAST | 1006K| 49M| 4531 (1)| 00:00:55 |
    | 51 | TABLE ACCESS BY INDEX ROWID | OPS_WB_DOD_DTLS | 1 | 11 | 1 (0)| 00:00:01 |
    |* 52 | INDEX UNIQUE SCAN | OPS_WB_DOD_DTLS_IDX_02 | 1 | | 0 (0)| 00:00:01 |
    | 53 | TABLE ACCESS FULL | OPS_TRIP_SHT | 423K| 13M| 1422 (2)| 00:00:18 |
    | 54 | TABLE ACCESS FULL | OPS_PROV_LT_DLVRY | 446K| 4361K| 1142 (2)| 00:00:14 |
    | 55 | TABLE ACCESS BY INDEX ROWID | OPS_PLTD_WB_DTLS | 1 | 21 | 2 (0)| 00:00:01 |
    |* 56 | INDEX UNIQUE SCAN | OPS_PLTD_WB_DTLS_IDX_04 | 1 | | 1 (0)| 00:00:01 |
    | 57 | TABLE ACCESS BY INDEX ROWID | OPS_ULTD_WB_DTLS | 1 | 21 | 2 (0)| 00:00:01 |
    |* 58 | INDEX UNIQUE SCAN | OPS_ULTD_WB_DTLS_IDX_02 | 1 | | 1 (0)| 00:00:01 |
    |* 59 | TABLE ACCESS BY INDEX ROWID | OPS_UPD_LT_DLVRY | 1 | 10 | 2 (0)| 00:00:01 |
    |* 60 | INDEX UNIQUE SCAN | OPS_UPD_LT_DLVRY_IDX_01 | 1 | | 1 (0)| 00:00:01 |
    | 61 | TABLE ACCESS BY INDEX ROWID | OPS_TRPT_VHLS | 1 | 21 | 1 (0)| 00:00:01 |
    |* 62 | INDEX UNIQUE SCAN | OPS_TRPT_VHLS_IDX_01 | 1 | | 0 (0)| 00:00:01 |
    | 63 | TABLE ACCESS BY INDEX ROWID | PO_VENDORS | 1 | 27 | 1 (0)| 00:00:01 |
    |* 64 | INDEX UNIQUE SCAN | VENDOR_UNIQUE | 1 | | 0 (0)| 00:00:01 |
    |* 65 | INDEX UNIQUE SCAN | OPS_GATE_PASS_IDX_01 | 1 | | 1 (0)| 00:00:01 |
    |* 66 | TABLE ACCESS BY INDEX ROWID | OPS_GATE_PASS | 1 | 19 | 2 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    3 - access("OCM"."ID"=:B1 OR "OCM"."ID"=:B2)
    5 - access("OBM"."ID"=:B1)
    7 - access("CGA"."DOC_TYPE"='WB' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=6)
    9 - access("CGA"."DOC_TYPE"='WB' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=7)
    11 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=15)
    13 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=16)
    15 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=17)
    17 - access("CGA"."DOC_TYPE"='GP' AND "CGA"."DOC_ID"=:B1 AND "CGA"."CHG_GROUP_ID"=23)
    22 - access("DT"."WAYBL_ID"=:B1)
    23 - access("ULT"."ID"="DT"."UPD_ULT_ID")
    24 - filter("ULT"."FROM_BR_MAST_ID"=:B1)
    32 - access("OPL"."ID"="TS"."PROV_LT_DLVRY_ID")
    33 - access("CNTR"."ID"(+)="SYS_ALIAS_10"."CUST_CNTR_ID")
    35 - access("TSREC"."TRIP_SHT_ID"="TS"."ID")
    37 - access("ST"."ID"="AD"."ST_UN_MAST_ID")
    39 - access("AD"."ID"="CCADD"."ADDR_MAST_ID")
    40 - access("CCADD"."ID"="SYS_ALIAS_10"."CC_CEE_ADDR_ID")
    43 - access("SYS_ALIAS_10"."ID"="DTL"."WAYBL_ID")
    44 - filter("SYS_ALIAS_10"."GL_TRFD" IS NULL OR "SYS_ALIAS_10"."GL_TRFD"='Y')
    45 - access(TRUNC(INTERNAL_FUNCTION("FIRST_DLVRY_DT"))=TO_DATE(' 2011-08-15 00:00:00', 'syyyy-mm-dd
    hh24:mi:ss'))
    47 - access("DTL"."ID"="TSREC"."PULTD_WB_DTLS_ID")
    48 - filter("TSREC"."STATUS_LID"=157)
    52 - access("SYS_ALIAS_10"."ID"="OWD"."WAYBL_ID"(+))
    56 - access("LTDTL"."PROV_LT_DLVRY_ID"="OPL"."ID" AND "LTDTL"."WAYBL_ID"="DTL"."WAYBL_ID")
    58 - access("ULTDTL"."PLTD_WB_DTLS_ID"="LTDTL"."ID")
    59 - filter("UPLT"."PROV_LT_DLVRY_ID"="OPL"."ID")
    60 - access("ULTDTL"."UPD_LT_DLVRY_ID"="UPLT"."ID")
    62 - access("OTV"."ID"="OPL"."TRPT_VHLS_ID")
    64 - access("PO"."VENDOR_ID"="OTV"."VENDOR_ID")
    65 - access("SYS_ALIAS_9"."ID"="ULTDTL"."GATE_PASS_ID")
    66 - filter("SYS_ALIAS_9"."GL_TRFD" IS NULL OR "SYS_ALIAS_9"."GL_TRFD"='Y')
    so, please help me.
    Regards,
    Viveka Nand
    Edited by: 891502 on Oct 14, 2011 4:39 AM

    891502 wrote:
    now am putted in the correct format,In the link it tells you to provide the query. Which you have not done.
    It tells you how to format the plan. Which you have not done.
    It tells you to provide database version and optimizer parameters. Which you have not done.
    It tells you to provide autotrace statistics and trace output. Which you have not done.
    So in what way is this format correct ?
    so, please give me the way to resolved it.With the information you have provided we can say with certainty that if you write your query this way it will be fast.
    select null from dual;

  • ORA-06502 on  'SQL query' report with sort in 'Report Attributes'

    Hi All,
    We get the next error if we set sorting on a column in a 'Report' based on a 'SQL query'. Removing the sort, error disappeares:
    failed to parse SQL query:
    ORA-06502: PL/SQL: numeric or value error: NULL index table key value
    Any suggestions?
    Erik

    Erik,
    Thanks, this explains it. By specifying the request as part of your URL you run into the recently uncovered issue. The request you're setting is REMFROMLIST and ADD2LIST. You probably either have links that include those requests or you have branches where you specify them. Either way, in order to get reports sorting to work, you'll have to make sure that the request strings are not part of your URL. This is a work-around and the upcoming HTML DB patch release will solve this issue.
    One way of avoiding this is to have computations on the previous pages that set a napplication level or page level item to the REMFROMLIST and ADD2LIST values and then you can use those items for your conditions that are currently evaluating those strings.
    Hope this helps and sorry for the inconvenience,
    Marc

  • Problem in executeQuery(SQL query sentence with a ' );

    for example:
    String strName="";
    //here strName is searching key value.
    String strQueryString="SELECT * FROM Authors WHERE name='" strName "'";
    ResultSet rs=st.executeQuery(strQueryString);
    if strName value is "yijun_lee",that will return all information which the name columns value is "yijun_lee" with SQL Query sentence SELECT * FROM Authors WHERE name='yijun_lee'
    but if strName value is "yijun ' lee",that value contains a '.that will error!
    how to do?
    thanks very much!

    You could parse <strName > and insert another ' for each '
    A concrete example would be SELECT * FROM Authors WHERE name='yijun '' lee'HTH

Maybe you are looking for

  • Sales orders in /SAPAPO/RRP3

    Colleagues, I have a question regarding the display of sales orders/deliveries within the transaction /SAPAPO/RRP3.  We are currently utilising SCM7.0 and my question realtes to the following process. 1. Sales orders are created in ECC and transferre

  • ITunes Match displays all my music but won't download or play

    I have just upgraded to Yosemite, doing a clean install.  There are 2 user profiles - mine and my partner's.  My partner's music appears to be playing fine.  I have iTunes Match.  I created my user account yesterday, logging in with my one & only App

  • Problem in xml header syntax!!

    Greetings, I'm testing hibernate framework, I'm writing this mapping xml file. Regardless the fact it is a hibernate example, this is a normal xml file problem. When I write that xml to the JBuilder editor, I get an error msg. I don't know why! and t

  • If I buy a new hard drive reinstall my OS and Adobe apps, will this count as installing on 2nd comp?

    If I buy a new hard drive, or format my current one and reinstall my OS and Adobe apps, will this count as installing on a 'second computer'? If so, how then do I go about ACTUALLY installing the apps on a second computer if I'm unable to 'de-registe

  • Labview-Oc​tave/GNUPl​ot Inegration

    Hello, forum! I'm working on a little project to take images I've acquired using some LabVIEW code and automatically import them into GNU Octave (similar to MATLAB) and turn them into a 3D image "stack" an added bonus would bethe ability to return th