Optimizing MINUS query

Hi All,
Please help me in optimizing my MINUS query, I had created a web portal using pl/sql to view the difference between two data loads into a table each data load has its own version number.
for example:
consider table xx
Ver id opt Wcc code st_dt end_dt rate
253 2 xx NBK 0 8/1/2002 7/31/2005 0.02
253 2 xx NBK 0 8/1/2005 3/31/2006 0.01
258 2 yy BKN 0 4/1/2006 12/31/209 0.01
258 2 yy BKN 1 8/1/2002 7/31/2005 0.02
If the table is containing data in thousands my web portal is taking at least 5min to display the difference of the data present for version 253 and 258.
the MINUS query i have written in my code is
select
tab1.id, tab1.opt, tab1.Wcc, tab1.code,tab1.st_dt, tab1.end_dt, tab1.rate
from xx tab1,
(SELECT id,opt,Wcc,code,st_dt,rate FROM xx WHERE ver = 253
minus SELECT id,opt,Wcc,code,st_dt,rate FROM xx WHERE ver = 258 ) tab2
where tab1.id||tab1.opt||tab1.wcc||tab1.code||tab1.st_dt||tab1.rate=tab2.id||tab2.opt||tab2.Wcc||tab2.code||tab2.st_dt||tab2.rate
and tab1.ver in (258,253);
how can i optimize my query so that i can view the difference in min time through the web portal??

SQL> create table xx
  2  as
  3  select 253 Ver, 2 id, 'xx' opt, 'NBK' Wcc, 0 code, date '2002-08-01' st_dt, date '2005-07-31' end_dt, 0.02 rate from dual union all
  4  select 253, 2, 'xx', 'NBK', 0, date '2005-08-01', date '2006-03-31', 0.01 from dual union all
  5  select 253, 5, 'zz', 'KBN', 1, date '2005-08-01', date '2006-03-31', 0.1 from dual union all
  6  select 258, 2, 'xx', 'NBK', 0, date '2002-08-01', date '2005-07-31', 0.02 from dual union all
  7  select 258, 2, 'xx', 'NBK', 0, date '2005-08-01', date '2006-03-31', 0.5 from dual union all
  8  select 258, 2, 'yy', 'BKN', 1, date '2005-08-01', date '2005-03-31', 0.1 from dual
  9  /
Tabel is aangemaakt.
SQL> select ver
  2       , id
  3       , opt
  4       , wcc
  5       , code
  6       , st_dt
  7       , end_dt
  8       , rate
  9       , decode(ver,253,'Deleted',258,'New/Modified') remark
10    from xx
11   where ver in (253,258)
12     and not exists
13         ( select 'dummy'
14             from xx xx2
15            where xx2.id = xx.id
16              and xx2.opt = xx.opt
17              and xx2.wcc = xx.wcc
18              and xx2.code = xx.code
19              and xx2.st_dt = xx.st_dt
20              and xx2.end_dt = xx.end_dt
21              and xx2.rate = xx.rate
22              and xx2.ver != xx.ver
23         )
24  /
  VER    ID OP WCC  CODE ST_DT               END_DT               RATE REMARK
  253     2 xx NBK     0 01-08-2005 00:00:00 31-03-2006 00:00:00   ,01 Deleted
  253     5 zz KBN     1 01-08-2005 00:00:00 31-03-2006 00:00:00    ,1 Deleted
  258     2 xx NBK     0 01-08-2005 00:00:00 31-03-2006 00:00:00    ,5 New/Modified
  258     2 yy BKN     1 01-08-2005 00:00:00 31-03-2005 00:00:00    ,1 New/Modified
4 rijen zijn geselecteerd.Regards,
Rob.

Similar Messages

  • Need a procedure to make minus query as output for 2 tables

    Can anybody help me to create a procedure so that I could get minus query of 2 table as a result.
    Requirement:
    I have two table 1- src_table_list ,2- tgt_table_list both tables have 2 columns : serial_no,table_name and 100 records each. and details mentioned in column "table_name" are actually tables name which present in my testing database.
    so I need one procedure which will pick one table_name from src_table_list and one table_name from tgt_table_name each time recursively and provide minus query as a result. as below.
    select c1,c2,c3,c4 from table1 --(fetched from src_table_list)
    minus
    select b1,b2,b3,b4 from table2 --(fetched from tgt_table_list)
    Can any body give or help me to create the procedure..as I have to prepare minus query for more than 200 tables and then I need to test them for integration testing..
    Edited by: 974253 on Nov 30, 2012 5:39 AM

    select 'select '||chr(39)||src_table_list.tblname||chr(39)||','||chr(39)||trg_table_list.tblname||chr(39)||',count(*) from '||' ( select * from '||src_table_list.tblname||'minus select * from '||trg_table_list.tblname||');'
    from src_table_list, trg_table_list
    WHERE src_table_list.serial_no = tgt_table_list.serial_no
    The above statement should give output similar to below code and will list down all the table names in the 2 tables(i.e. above src and trg) -
    select 'src_table_list.tbl1','trg_table_list.tbl1',count(*) from
    (select col1, col2 from src_table_list.tbl1 minus select col1, col2 from trg_table_list.tbl1 );
    select 'src_table_list.tbl2','trg_table_list.tbl2',count(*) from
    (select col1, col2 from src_table_list.tbl2 minus select col1, col2 from trg_table_list.tbl2 );
    Now atleast you can run these statements as script and get to know what all tables are having count differences.
    I might have missed out on some syntax part in above code but hope to have helped you in some way as you will be specific with number of tables to check for differences.

  • Plz help in optimizing the query

    I have a query which is written to cater to the scenario that the entered start and end date do not overlap with the start and end date already present in the database records. Can someone help in optimizing this query. all inclusion and exclusion scenario's have to be taken care of.
    the query is as follows:
    SELECT COUNT(*) FROM CLAS WHERE TRIM(UPPER(CLAS_CDE)) =UPPER('timecheck') AND TRIM(UPPER(CLAS_TYPE_CDE))=UPPER('TEST_3')
    AND TRIM(UPPER(LANG_CDE))=UPPER('en')
    AND (
    (END_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
    OR
    (START_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
    OR (
    (START_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
    AND
    (END_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
    OR(('09/13/2007' BETWEEN TO_CHAR(START_DT,'MM/DD/YYYY') AND TO_CHAR(END_DT,'MM/DD/YYYY'))
    AND ('09/15/2007' BETWEEN TO_CHAR(START_DT,'MM/DD/YYYY') AND TO_CHAR(END_DT,'MM/DD/YYYY'))
    );

    I format your code in different way:
    SELECT COUNT(*)
    FROM CLAS
    WHERE TRIM(UPPER(CLAS_CDE)) = UPPER('timecheck')
    AND TRIM(UPPER(CLAS_TYPE_CDE))=UPPER('TEST_3')
      AND TRIM(UPPER(LANG_CDE))=UPPER('en')
       AND (
         (END_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
            OR
         (START_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
            OR (
              (START_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
              AND
              (END_DT BETWEEN TO_DATE('09/13/2007','MM/DD/YYYY') AND TO_DATE('09/15/2007','MM/DD/YYYY'))
         OR (
              ('09/13/2007' BETWEEN TO_CHAR(START_DT,'MM/DD/YYYY') AND TO_CHAR(END_DT,'MM/DD/YYYY'))
              AND
              ('09/15/2007' BETWEEN TO_CHAR(START_DT,'MM/DD/YYYY') AND TO_CHAR(END_DT,'MM/DD/YYYY'))
          );First thought that cross my mind it to check if changing this:
    UPPER('timecheck') to simple:
    'TIMECHECK' and do the same with other expressions with UPPER function.
    Peter D.

  • WebI:Creating a minus query from data providers not using the same universe

    Hi there. A client of ours wishes to use Combined Query on multiple universes in the same WebI report. So far, I have heard it is not feasible in WebI. I know it is possible in DeskI. Even the sub-query option is limited to just one universe in WebI. So, I am stuck.
    Is there a workaround to creating a minus query from data providers not using the same universe in WebI?

    Hello Amit,
    what are the Datasources ? Usually when you create a Sub Query you can choose a seperate Universe.
    Maybe if you only see one Universe you only have the rights for this Universe ?!.
    Regards
    -Seb.

  • Minus query returning row

    Hi
    One of my sql minus query is returning row even though both the statements are having same value.
    My query is following :
    SELECT UNIQUE EMP_AGG_DW_PAYROLL_TOT_FTE_CNT,EMP_AGG_PAY_DEM_EMP_SKEY
    FROM DW.AGG_EMP_ACCT_COMPENSATIONS
    WHERE EMP_AGG_DW_EFF_START_DT='1-jan-2006'
    AND EMP_AGG_PAY_DEM_EMP_SKEY IN (133607,141955)
    MINUS
    select * from (
    SELECT SUM(CS),PAY_DEM_EMP_SKEY FROM (
    SELECT SUM(CP)/24 CS,PAY_DEM_EMP_SKEY FROM (
    SELECT COUNT(PAY_DEM_DW_PAYROLL_FTE_COUNT) CP,PAY_DEM_EMP_SKEY FROM
    DW.PAYROLL_DEMOGRAPHICS A,MRADMIN_PROFPROCDT MP
    WHERE PAY_DEM_DW_PAYROLL_FTE_COUNT=1
    AND PAY_DEM_DW_PAYROLL_ADJUST_IND = 0
    AND PAY_DEM_EMP_SKEY IN (133607,141955)
    AND (TRUNC(A.PAY_DEM_PAY_DT) BETWEEN TRUNC(MP.D_BEGIN_DATE)
    AND TRUNC(MP.D_END_DATE))
    GROUP BY PAY_DEM_EMP_SKEY)GROUP BY PAY_DEM_EMP_SKEY
    UNION ALL
    SELECT SUM(CP)/48 CS,PAY_DEM_EMP_SKEY FROM (
    SELECT COUNT(PAY_DEM_DW_PAYROLL_FTE_COUNT) CP,PAY_DEM_EMP_SKEY FROM DW.PAYROLL_DEMOGRAPHICS
    WHERE PAY_DEM_DW_PAYROLL_FTE_COUNT=.5
    AND PAY_DEM_DW_PAYROLL_ADJUST_IND = 0
    AND PAY_DEM_EMP_SKEY IN (133607,141955)
    GROUP BY PAY_DEM_EMP_SKEY)GROUP BY PAY_DEM_EMP_SKEY )
    GROUP BY PAY_DEM_EMP_SKEY
    This is returning two rows.
    When I run queries seperatly then i am getting following result :
    EMP_AGG_DW_PAYROLL_TOT_FTE_CNT     EMP_AGG_PAY_DEM_EMP_SKEY
    0.0416666666666667                133607
    0.0416666666666667                    141955
    SUM(CS)          PAY_DEM_EMP_SKEY
    0.0416666666666667 133607
    0.0416666666666667     141955
    I am unable to get the exact reason behind me.
    Any help is higly appreciated.

    Probably some differences that are not shown due to rounding as in example below?
    SQL> set numw 5
    SQL> select 1.666666666666 from dual;
    1.666666666666
             1,667
    Elapsed: 00:00:00.00
    SQL> select 1.666666 from dual;
    1.666666
       1,667
    Elapsed: 00:00:00.00
    SQL> select 1.666666666666 from dual
      2  minus
      3  select 1.666666 from dual;
    1.666666666666
             1,667
    Elapsed: 00:00:00.00
    SQL> Gints Plivna
    http://www.gplivna.eu

  • Minus query in ODI 3.4

    hi everyone ,
    can some please explain me how to do minus query in Odi . i am not allowed to create views so please such suggestion.
    in short some method where i can compare two tables and extract only the data that is present in one table and if both have the same data ignore.
    eg .
    table 1
    id desc flag
    100 sdfsd 3
    101 sdfsd 3
    102 sdfds 3
    table 2
    id desc flag
    100 sdfd 8
    103 sdfd 8
    105 dfsdf 8
    result should be
    id desc flag
    100 sdfsd 3
    101 sdfsd 3
    102 sdfds 3
    103 sdfd 8
    105 dfsdf 8
    i .e 8 should not update 3 flag data . in IKM update its lost and if i put update on both id and flag i have two copies . so i just want to compare and load all the date of table 1 completely and then table 2 which doesnt mached with id .
    Please suggest .

    Hi Dev,
    For the situation as you described do something like:
    1) load the firts table with IKM Control Append
    2) Load the second table with the IKM Incremental Update but change the "UPDATE" option to "NO" and use the ID as KEY.
    That will give you exaclty what you wanted because will only load the new records from second and ignore those that already exists in the target.
    Cezar Santos

  • Checkbox in a minus query

    Hello,
    I am trying to use the checkbox function in the following query to produce a report with a checkbox but it is not working correctly -- the query is fine, but does not work when I place the checkbox function within it:
    Select HTMLDB_ITEM.CHECKBOX(1,loginid) Grant,
    lastname || ', ' || firstname fullname
    from arxusers A
    where a.rec_status = 'A'
    minus
    Select a.loginid,
    lastname || ', ' || firstname fullname
    from arxusers a , arxaccess b
    where a.loginid = b.loginid
    and b.rpt_id = :id
    and a.rec_status = 'A'
    Says from keyword not found where expected -- Can you not use the checkbox with a 'minus' SQL query

    Can anyone please help me with this -- I even changed my query to a 'not exists' and I still get the same, 'From keyword not found where expected' error. Is the checkbox function in a report limited to just basic SQL report queries? That does not make sense. Here is my new query getting the same error:
    SELECT HTMLDB_ITEM.CHECKBOX(1,a.loginid) Grant,
    a.lastname || ', ' || a.firstname
    from arxusers A
    where a.rec_status = 'A'
    and not exists (
    select 'Y'
    from arxaccess b
    where a.loginid = b.loginid
    and b.rpt_id = :id)
    Any help would be great.
    Thanks,
    Jeff

  • Issue with Minus Query

    Hello All,
    I am using oracle database 10.2.0.4 in windows server.
    I am doing data comparision for 2 set of query using the minus operator with same condition.
    My 1st query is giving 23 rows and 2nd query is giving 20 rows. The output should be 3 rows, but I am not getting the same. It is returning null.
    select WORK,WORK_UNIT,WORK_STR from WORK_GROUP
    where WORK_UNIT=003
    and WORK_STR=10
    and WORK is null
    MINUS
    select WORK,WORK_UNIT,WORK_STR from WORK_GROUP@DBL_SOURCE
    where WORK_UNIT=003
    and WORK_STR=10
    and WORK is null;
    Any updates on the above query?
    Thank You,
    RKP

    RKP wrote:
    Hello All,
    I am using oracle database 10.2.0.4 in windows server.
    I am doing data comparision for 2 set of query using the minus operator with same condition.
    My 1st query is giving 23 rows and 2nd query is giving 20 rows. The output should be 3 rows, but I am not getting the same. It is returning null.
    select WORK,WORK_UNIT,WORK_STR from WORK_GROUP
    where WORK_UNIT=003
    and WORK_STR=10
    and WORK is null
    MINUS
    select WORK,WORK_UNIT,WORK_STR from WORK_GROUP@DBL_SOURCE
    where WORK_UNIT=003
    and WORK_STR=10
    and WORK is null;
    Any updates on the above query?
    Thank You,
    RKPHave a look at your query and the where conditions. 3 column are being selected and they all are in where criteria.
    No matter how many rows are returned, column value returned by both the queries would always be :
    WORK     WORK_UNIT     WORK_STR
    NULL     003          10All rows returned by both the queries would be EXACTLY same!!!
    There is no way to differentiate any rows returned by query 1 with those returned by query 2.
    Since all rows returned by first query do match with those returned by second query, MINUS operator results in no rows selected.

  • Inserts on Sub (minus) Query

    Ok. This is what I need to complete. I need to Insert from a minus subquery.
    Here is my query where I need to insert the results.
    select id_user, system_id, nm_database, cd_altname from accounts_temp
    minus
    select id_user, system_id, nm_database, cd_altname from accounts
    From the results, I need to insert all fields into 'Accounts'. Note, that the four columns listed above in the subquery, are only 4 of the 15 columns in the tables; I need to insert *. I cannot use a the merge on b/c sometimes, the values of these columns are null.
    Any thoughts?

    or more simple
    insert into accounts
    select * from accounts_temp
    where (id_user, system_id, nm_database, cd_altname) not in
             (select (id_user, system_id, nm_database, cd_altname) from accounts)Although usually "not in" is not very fast. It could be replaced by "no exists" or compared for performance with the previous version.
    Edited by: Sven W. on Oct 21, 2008 8:55 AM

  • Table Sizes Optimized in Query

    Hi Experts,
         When I was run the query, I am not getting the data in the query but when I checked the data in the cube, the data is available there. I went RSRT there I checked the technical information in that <b>Table Sizes Optimized is showing RED</b>. What is this error How to solve this problem?
    Thanks in Advance
    Ravi.

    Hi Ravi,
    What was the fix for this issue ?
    Appreciate your help.
    Thanks

  • MINUS Query problem

    Hey, I am having a problem with one of the minus queries I wrote.
    When I execute my source query and Target query seperately they return back in a less than a minute.
    Where as if I use Source minus Target It is taking more than 5 hours. Can any one explain me the reason.
    Thanks & Regards

    ss_study wrote:
    Hey, I am having a problem with one of the minus queries I wrote.
    When I execute my source query and Target query seperately they return back in a less than a minute.How are you timing the queries?
    Re: Materialized View creation very slow

  • Please help with optimizing aggregate query

    Good Morning.
    I hope this will be a simple question, so I won't give a lot of detail about the db unless you ask for clarification.
    I have four tables I need to join. CLC can have many CLS, and a CLS can have many CUSTD, and a CUSTD can have many CUSTDR.
    I need to add up all the rows in CUSTD for one CLC, and subtract the sum of all the rows in CUSTDR for that CLC.
    I first tried this
    SELECT SUM(custd.amount_owed) - SUM(cusdr.amount_refunded)but that doesn't work because the amount owed is returned for every amount refunded.
    Then I tried using a subquery
       SELECT
          TO_CHAR(owed.amount - NVL(SUM(custdr.refund_amount),0), 'L999G999G999G999D00')
        INTO
          g$_value
        FROM
          claim_settlements          cls
          ,customer_debts            custd
          ,customer_debt_recoveries  custdr
             SELECT
                cls1.clc_id                 id
                ,SUM(custd1.owed_amount),0  amount
              FROM
                claim_settlements    cls1
                ,customer_debts      custd1
              WHERE
                    custd1.st_table_short_name = 'CLS'
                AND custd1.key_value = cls1.id
                AND custd1.status != 'WO'
              GROUP BY
                cls1.clc_id  
          )owed
        WHERE
              custd.st_table_short_name = 'CLS'
          AND custd.key_value = cls.id
          AND custd.id = custdr.custd_id(+)
          AND cls.clc_id = p$_key_value
          AND owed.id = cls.clc_id
        GROUP BY
          owed.amount;   I would like to know if this is possible using an analytic sum. This query works, but I read that analytics are better than sub queries, and I am still not sure on all the uses of analytic functions.
    Thanks
    Message was edited by:
    dmill
    Updated my question.
    Message was edited by:
    dmill

    Thanks, Nic
    That is the kind of query I was imaging, so I hope we can get it to work.
    Here is information about the tables and the data:
    DESC customer_debts
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER(28)                                                                                                                                                                                   
    CUSTA_ID                       NOT NULL NUMBER(28)                                                                                                                                                                                   
    GLTT_ID                        NOT NULL NUMBER(28)                                                                                                                                                                                   
    ST_TABLE_SHORT_NAME            NOT NULL VARCHAR2(10)                                                                                                                                                                                 
    KEY_VALUE                      NOT NULL NUMBER(28)                                                                                                                                                                                   
    OWED_AMOUNT                    NOT NULL NUMBER(11,2)                                                                                                                                                                                 
    OWED_BY_CUSTOMER               NOT NULL VARCHAR2(1)                                                                                                                                                                                  
    STATUS                         NOT NULL VARCHAR2(2)                                                                                                                                                                                  
    NOTES                                   VARCHAR2(4000)                                                                                                                                                                               
    DATE_CREATED                   NOT NULL DATE                                                                                                                                                                                         
    CREATED_BY                     NOT NULL VARCHAR2(30)                                                                                                                                                                                 
    DATE_MODIFIED                           DATE                                                                                                                                                                                         
    MODIFIED_BY                             VARCHAR2(30)                                                                                                                                                                                 
    13 rows selected
    DESC customer_debt_recoveries
    Name                           Null     Type                                                                                                                                                                                         
    ID                             NOT NULL NUMBER(28)                                                                                                                                                                                   
    CUSTD_ID                       NOT NULL NUMBER(28)                                                                                                                                                                                   
    ST_TABLE_FOR_PAID_BY           NOT NULL VARCHAR2(10)                                                                                                                                                                                 
    KEY_VALUE_FOR_PAID_BY          NOT NULL NUMBER(28)                                                                                                                                                                                   
    REFUND_AMOUNT                  NOT NULL NUMBER(11,2)                                                                                                                                                                                 
    REFUND_CHECK_NUMBER                     NUMBER(9)                                                                                                                                                                                    
    DATA_SOURCE                    NOT NULL VARCHAR2(1)                                                                                                                                                                                  
    NOTES                                   VARCHAR2(4000)                                                                                                                                                                               
    DATE_CREATED                   NOT NULL DATE                                                                                                                                                                                         
    CREATED_BY                     NOT NULL VARCHAR2(30)                                                                                                                                                                                 
    DATE_MODIFIED                           DATE                                                                                                                                                                                         
    MODIFIED_BY                             VARCHAR2(30)                                                                                                                                                                                 
    12 rows selected
    customer_debts custd
    ID                     CSL_ID                 OWED_AMOUNT           
    1                      4143802                20                    
    2                      4143802                10                    
    3                      4143802                10                    
    5                      4143796                10                    
    6                      4143806                10                    
    7                      999999999              20                    
    8                      999999999              10                    
    9                      999999999              10                    
    11                     4143802                100                   
    9 rows selected
    customer_debt_recoveries custdr
    ID                     CUSTD_ID               REFUND_AMOUNT         
    1                      5                      10                    
    2                      1                      27                    
    3                      1                      5                     
    3 rows selected
    claim_charges clc and claim_settlements cls
    CLC_ID                 CLS_ID                
    537842                 4143802               
    537842                 999999999             
    538057                 4143796               
    538209                 4143806               
    4 rows selectedThe clc is the object that we want the information for. For example, clc 537842 should return the amount of 148, which is the sum of all the debts minus the sum of all the recoveries.
    clc 4143796 would return 0
    and 4143806 would return 10
    When I run your query for clc 537842, which looks like this with joins
    select distinct
           clc.id clc_id
           ,sum(custd.owed_amount) over (partition by clc.id, cls.id, custd.id)
           sum(custdr.refund_amount) over (partition by clc.id, cls.id, custd.id, custdr.id) amount_owed
    from  
      claim_charges             clc
      ,claim_settlements        cls
      ,customer_debts           custd
      ,customer_debt_recoveries custdr
    where 
          cls.clc_id = clc.id
      AND custd.key_value = cls.id
      AND custd.st_table_short_name = 'CLS'
      AND custd.id = custdr.custd_id (+)
      AND custd.status != 'WO'
      AND clc.id = 537842I get this result
    CLC_ID                 AMOUNT_OWED           
    537842                                       
    537842                 35                    
    537842                 13                    
    3 rows selectedThanks again, and let me know if this is not enough info.

  • Need help in optimizing the query with joins and group by clause

    I am having problem in executing the query below.. it is taking lot of time. To simplify, I have added the two tables FILE_STATUS = stores the file load details and COMM table that is actual business commission table showing records successfully processed and which records were transmitted to other system. Records with status = T is trasnmitted to other system and traansactions with P is pending.
    CREATE TABLE FILE_STATUS
    (FILE_ID VARCHAR2(14),
    FILE_NAME VARCHAR2(20),
    CARR_CD VARCHAR2(5),
    TOT_REC NUMBER,
    TOT_SUCC NUMBER);
    CREATE TABLE COMM
    (SRC_FILE_ID VARCHAR2(14),
    REC_ID NUMBER,
    STATUS CHAR(1));
    INSERT INTO FILE_STATUS VALUES ('12345678', 'CM_LIBM.TXT', 'LIBM', 5, 4);
    INSERT INTO FILE_STATUS VALUES ('12345679', 'CM_HIPNT.TXT', 'HIPNT', 4, 0);
    INSERT INTO COMM VALUES ('12345678', 1, 'T');
    INSERT INTO COMM VALUES ('12345678', 3, 'T');
    INSERT INTO COMM VALUES ('12345678', 4, 'P');
    INSERT INTO COMM VALUES ('12345678', 5, 'P');
    COMMIT;Here is the query that I wrote to give me the details of the file that has been loaded into the system. It reads the file status and commission table to show file name, total records loaded, total records successfully loaded to the commission table and number of records that has been finally transmitted (status=T) to other systems.
    SELECT
        FS.CARR_CD
        ,FS.FILE_NAME
        ,FS.FILE_ID
        ,FS.TOT_REC
        ,FS.TOT_SUCC
        ,NVL(C.TOT_TRANS, 0) TOT_TRANS
    FROM FILE_STATUS FS
    LEFT JOIN
        SELECT SRC_FILE_ID, COUNT(*) TOT_TRANS
        FROM COMM
        WHERE STATUS = 'T'
        GROUP BY SRC_FILE_ID
    ) C ON C.SRC_FILE_ID = FS.FILE_ID
    WHERE FILE_ID = '12345678';In production this query has more joins and is taking lot of time to process.. the main culprit for me is the join on COMM table to get the count of number of transactions transmitted. Please can you give me tips to optimize this query to get results faster? Do I need to remove group and use partition or something else. Please help!

    I get 2 rows if I use my query with your new criteria. Did you commit the record if you are using a second connection to query? Did you remove the criteria for file_id?
    select carr_cd, file_name, file_id, tot_rec, tot_succ, tot_trans
      from (select fs.carr_cd,
                   fs.file_name,
                   fs.file_id,
                   fs.tot_rec,
                   fs.tot_succ,
                   count(case
                            when c.status = 'T' then
                             1
                            else
                             null
                          end) over(partition by c.src_file_id) tot_trans,
                   row_number() over(partition by c.src_file_id order by null) rn
              from file_status fs
              left join comm c
                on c.src_file_id = fs.file_id
             where carr_cd = 'LIBM')
    where rn = 1;
    CARR_CD FILE_NAME            FILE_ID           TOT_REC   TOT_SUCC  TOT_TRANS
    LIBM    CM_LIBM.TXT          12345678                5          4          2
    LIBM    CM_LIBM.TXT          12345677               10          0          0Using RANK can potentially produce multiple rows to be returned though your data may prevent this. ROW_NUMBER will always prevent duplicates. The ordering of the analytical function is irrelevant in your query if you use ROW_NUMBER. You can remove the outermost query and inspect the data returned by the inner query;
    select fs.carr_cd,
           fs.file_name,
           fs.file_id,
           fs.tot_rec,
           fs.tot_succ,
           count(case
                    when c.status = 'T' then
                     1
                    else
                     null
                  end) over(partition by c.src_file_id) tot_trans,
           row_number() over(partition by c.src_file_id order by null) rn
    from file_status fs
    left join comm c
    on c.src_file_id = fs.file_id
    where carr_cd = 'LIBM';
    CARR_CD FILE_NAME            FILE_ID           TOT_REC   TOT_SUCC  TOT_TRANS         RN
    LIBM    CM_LIBM.TXT          12345678                5          4          2          1
    LIBM    CM_LIBM.TXT          12345678                5          4          2          2
    LIBM    CM_LIBM.TXT          12345678                5          4          2          3
    LIBM    CM_LIBM.TXT          12345678                5          4          2          4
    LIBM    CM_LIBM.TXT          12345677               10          0          0          1

  • Help required in optimizing the query response time

    Hi,
    I am working on a application which uses a jdbc thin client. My requirement is to select all the table rows in one table and use the column values to select data in another table in another database.
    The first table can have maximum of 6 million rows but the second table rows will be around 9000.
    My first query is returning within 30-40 milliseconds when the table is having 200000 rows. But when I am iterating the result set and query the second table the query is taking around 4 millisecond for each query.
    the second query selection criteria is to find the value in the range .
    for example my_table ( varchar2 column1, varchar2 start_range, varchar2 end_range);
    My first query returns a result which then will be used to select using the following query
    select column1 from my_table where start_range < my_value and end_range> my_value;
    I have created an index on start_range and end_range. this query is taking around 4 millisseconds which I think is too much.
    I am using a preparedStatement for the second query loop.
    Can some one suggest me how I can improve the query response time?
    Regards,
    Shyam

    Try the code below.
    Pre-requistee: you should know how to pass ARRAY objects to oracle and receive resultsets from java. There are 1000s of samples available on net.
    I have written a sample db code for the same interraction.
    Procedure get_list takes a array input from java and returns the record set back to java. You can change the tablenames and the creteria.
    Good luck.
    DROP TYPE idlist;
    CREATE OR REPLACE TYPE idlist AS TABLE OF NUMBER;
    CREATE OR REPLACE PACKAGE mypkg1
    AS
       PROCEDURE get_list (myval_list idlist, orefcur OUT sys_refcursor);
    END mypkg1;
    CREATE OR REPLACE PACKAGE BODY mypkg1
    AS
       PROCEDURE get_list (myval_list idlist, orefcur OUT sys_refcursor)
       AS
          ctr   NUMBER;
       BEGIN
          DBMS_OUTPUT.put_line (myval_list.COUNT);
          FOR x IN (SELECT object_name, object_id, myvalue
                      FROM user_objects a,
                           (SELECT myval_list (ROWNUM + 1) myvalue
                              FROM TABLE (myval_list)) b
                     WHERE a.object_id < b.myvalue)
          LOOP
             DBMS_OUTPUT.put_line (   x.object_name
                                   || ' - '
                                   || x.object_id
                                   || ' - '
                                   || x.myvalue
          END LOOP;
       END;
    END mypkg1;
    [pre]
    Testing the code above. Make sure dbms output is ON.
    [pre]
    DECLARE
       a      idlist;
       refc   sys_refcursor;
       c number;
    BEGIN
       SELECT x.nu
       BULK COLLECT INTO a
         FROM (SELECT 5000 nu
                 FROM DUAL) x;
       mypkg1.get_list (a, refc);
    END;
    [pre]
    Vishal V.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Partitioning For Optimal Parallel Query Execution

    Hi All,
    We are trying to design an architecture that benefits from partitioning and parallel query to obtain the best query response times for our system.
    Let me start by describing the main table which has five columns:
    Columns:
    1) DocId ------- Numeric Primary Key Constraint (Unique)
    2) Text ------- CLOB of XML Content
    3) SCode ------- Varchar 12 ( service code Can be one of 200 values)
    4) A_Date ------- Oracle Date ( The arrival date )
    5) A_Month ------- Numeric partition key, the month number (1-12)
    We insert 100,000 records daily so a month of data will contain 3,000,000 rows. The Text varies from 4k to 200k bytes and on average is around 30k bytes per document. A_Date is obtained when the C++ application receives a client connection. After document transmission is complete the DocId is obtained from an Oracle sequence. It is true that A_Date and DocId increase together. However because of varying document sizes and transmission rates, there is no guarantee that consistent order between the two columns exists.
    For Example: If time (t) increases and the connection times are: t1, t2, t3, t4 and the document at t2 took long to transmit, we can have:
    A_Date -------- DocId (From Oracle Sequence)
    t2 -------------- 1004
    t4 -------------- 1003
    t3 -------------- 1002
    t1 -------------- 1001
    A_Month is simply the month number (1-12) extracted from the transmission entry timestamp. It is also our Partition Key (see below). When the year wraps around from 2006 to 2007, data for January will simpy fall into the 1st partition bucket, and so on..
    QUERY NEEDS: Our queries are centered around a DateTime interval Where the left endpoint is current day/time. They can query the current day, current to 1 month back, current to 2 months back, .. current to 15 months back. We MUST RETURN a list of DocId's sorted in DESCENDING ORDER for screen display purposes.
    In General we need to return sub-second for the 1st month. As we query further back in time longer response times between 1 and 4 seconds are acceptable.
    PARTITIONING AND INDEXES:
    The table is partitioned by A_Month as shown below:
    Create Table IndexTable
    PARTITION BY RANGE (A_Month)
    ( PARTITION p1 VALUES LESS THAN 1.1 ...
    PARTITION p2 VALUES LESS THAN 2.1 ...
    PARTITION p3 vALUES LESS THAN 3.1 ...
    There are GLOBAL INDEXES on DocId, Text(Domain Index), and SCode.
    A_Date is a LOCAL INDEX.
    QUERY STRUCTURE:
    My Query structure looks like this (for a 2 month query):
    SELECT DocId from IndexTable WHERE
    Contains (Text, 'BUSH and EARNINGS') > 0 AND
    SCode in ('S1', 'S2', 'S3', 'S4') AND
    A_Date Between '2006-01-15 11:00:00' AND '2006-03-15 11:00:00'
    Order By DocId DESC;
    QUESTIONS (THERE ARE THREE)
    #### QUESTION 1 ####
    As I examine various explain plans, the PSTART and PSTOP do not reflect consistency with my A_Date range. It seems to always display:
    PStart PStop
    RowId RowId
    no matter what my A_Date range is.. I don't see it pruning my partitions. I cannot find documentation as to what RowId means or how it affects the optimizer's plan.
    #### QUESTION 2 ####
    I have tried parallelization hints on the table and indexes such as
    /*+ PARALLEL( IndexTable, 4) */ and on the A_Date index
    /*+ PARALLEL_INDEX( IndexTable, A_Date_Index, 4) */.
    I can't really tell if the parallel hints make a difference.
    However, the FIRST_ROWS hint makes a big difference if I nest the query inside a rownum clause as:
    SELECT * from
    ( Select /+* first_rows */ ... WHERE CONTAINS... > 0 AND... )
    Where ROWNUM <=20;
    #### QUESTION 3 ####
    I'm running close to the latest RedHat Linux and Oracle 10g2 and I have read about Parallel Slave Processes in Tom Kyte's Expert Oracle Architecture book in which he says you should see processes like:
    ora...p000..
    ora...p001..
    ora...pnnn..
    Which are the parallel slave processes. I have NEVER seen any oracle processes numbered as such running on my system when I run test queries. I have seen some q_ processes and others, but not the pnnn processes..
    Can I benefit from parallel query without ever seeing these processes??
    I Greatly Appreciate Any Advice To Any Of These Questions..
    Joe

    Well I can tell you this much. You will never get partition pruning if you don't have the partition key in the where clause.
    I'm not sure about the parallel query. There was a time that this wasn't supported with Text indexes, but not sure if that still applies today.
    In theory there are two levels of partitioning that can be defined that you may want to test out.
    1st, range partition the base table and create a partitioned text index based on that. This is what you are currently doing.
    2nd, in the storage preference for the $I table specify a range or hash partition (I've never tried this, but in theory it should work) on the token_text column. Try this out and see if it works.

Maybe you are looking for

  • I need help - Adobe Photoshop CC Crashing and Burning when trying to utilize my favorite plugin! :(

    That's right... this flagship of prime and lovable software goes crashing and burning every time I try to use my favorite plugin. It works just fine in CS5! I've tried contacting the developers and everyone to know avail. Can someone at the lovable A

  • Adobe Reader 9.0 Won't Open

    Running windows vista home premium 64 bit  I was attemping to open a pdf file from a web page that I created approx 1 month ago.  I didn't see that and update had been completed, however I was running reader 9.0.  I for what ever reason was not able

  • Is there a way to insert AFDs into a file upon export?

    I don't *think* this is possible, but I wanted to get confirmation.  Can Premiere insert an AFD flag to a file upon export?

  • Material PR transfer from ECC to SRM without Account Assignment

    Hello, When a material PR without account assignment is transferred to SRM. The scenario is determined as direct procurement and system will behave in extended classic scenario. I would need the PO to be posted in backend ie Classic scenario. We are

  • Running Apache 1.3.19 on a HP-UX11.11

    I am running Apache 1.3.19 on a HP-UX11.11 with Weblogic 6.1. I got this warning when starting apache: [Fri Jan 25 12:52:10 2002] [warn] Loaded DSO libexec/mod_wl.so uses plain Apache 1.3 API, this module might crash under EAPI! (please recompile it