How to tune self join query in Oracle 11g

Oracle & SQL new to me,and I'm still in learning phase.
Could you please help me to tune below oracle query?? This table contains ~95 lac records and it takes 1 hour to retrieve data using this query.
Your suggestions/comments/help will be appreciated.
Thanks in advance.
SELECT A.CNO AS CNO, A.FNO AS FNO, A.CID AS CID, A.IID AS IID
FROM CAC_LKP A, (SELECT C_DATE, CNO, FNO
FROM (SELECT MAX(CAC_LKP.C_DATE) AS C_DATE, CAC_LKP.CNO AS CNO, CAC_LKP.FNO AS FNO
FROM CAC_LKP
WHERE ACTIVE = 'Y' GROUP BY CNO, FNO)) B
WHERE A.C_DATE = B.C_DATE
AND A.CNO = B.CNO
AND A.FNO = B.FNO
AND A.ACTIVE = 'Y'
Primary key is defied over combination of c_date,iid,active.
Edited by: 1009236 on Jun 1, 2013 12:52 AM

>
SELECT  A.CNO AS CNO,
        A.FNO AS FNO,
        A.CID AS CID,
        A.IID AS IID
FROM    CAC_LKP A,
                        SELECT  C_DATE,
                                CNO,
                                FNO
                        FROM
                            SELECT  MAX(CAC_LKP.C_DATE) AS C_DATE,
                                    CAC_LKP.CNO AS CNO,
                                    CAC_LKP.FNO AS FNO
                            FROM CAC_LKP
                            WHERE ACTIVE = 'Y'
                            GROUP BY CNO, FNO
                    ) B
WHERE   A.C_DATE = B.C_DATE     AND
        A.CNO = B.CNO           AND
        A.FNO = B.FNO           AND
        A.ACTIVE = 'Y';Hi,
Before even starting to see why there is a performance problem, I think you should consider the fact that there is logical problem in your WHERE clause. According to what you mentioned the primary key is composed of *(c_date, iid, active)* yet iid is absent in both the global query's WHERE clause and also that of the subquery.
Consequently the (aggregate) rows in the subquery will not be linked based on a correct logic to the external query.
Regards,
Dariyoosh

Similar Messages

  • Tune this "contains" query in Oracle 11g EE 11.2.0.3.0

    I have a query like below. It runs for ever. The size of this table is not very big, it has around 30 millions rows. The owner column is very selective, and I've added "owner' column in the "filter by" clause of the domain index against description. Do you have any recommendation how I can tune this query? Any help would be greatly appreciated.
    select count(*) from items s where contains(s.description, '%111%' ) > 0 and s.owner = 1234;
    Thanks.

    You need to use SDATA to access the owner column through the index. You may also get better performance on your wildcard searches by using a wordlist with attributes as shown below. Please see the demonstration below.
    SCOTT@orcl_11gR2> create table items as
      2  select object_id as owner, object_name as description
      3  from   all_objects
      4  /
    Table created.
    SCOTT@orcl_11gR2> insert into items values (1234, 'A111B')
      2  /
    1 row created.
    SCOTT@orcl_11gR2> select count(*) from items
      2  /
      COUNT(*)
         75048
    1 row selected.
    SCOTT@orcl_11gR2> begin
      2    ctx_ddl.create_preference('mywordlist', 'BASIC_WORDLIST');
      3    ctx_ddl.set_attribute('mywordlist','PREFIX_INDEX','TRUE');
      4    ctx_ddl.set_attribute('mywordlist','PREFIX_MIN_LENGTH', 1);
      5    ctx_ddl.set_attribute('mywordlist','SUBSTRING_INDEX', 'YES');
      6    ctx_ddl.set_attribute('mywordlist', 'wildcard_maxterms', 50000) ;
      7  end;
      8  /
    PL/SQL procedure successfully completed.
    SCOTT@orcl_11gR2> create index items_idx
      2  on items (description)
      3  indextype is ctxsys.context
      4  filter by owner
      5  parameters ('wordlist  mywordlist')
      6  /
    Index created.
    SCOTT@orcl_11gR2> set autotrace on explain
    SCOTT@orcl_11gR2> select count(*) from items
      2  where  contains (description, '%111% and (sdata (owner = 1234))') > 0
      3  /
      COUNT(*)
             1
    1 row selected.
    Execution Plan
    Plan hash value: 1238254566
    | Id  | Operation        | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT |           |     1 |    29 |    12   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE  |           |     1 |    29 |            |          |
    |*  2 |   DOMAIN INDEX   | ITEMS_IDX |    39 |  1131 |     4   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - access("CTXSYS"."CONTAINS"("DESCRIPTION",'%111% and (sdata
                  (owner = 1234))')>0)
    Note
       - dynamic sampling used for this statement (level=2)
    SCOTT@orcl_11gR2>

  • How to build table join query in Jdeveloper

    Hi,
    Can someone tell me how to build table join query in Jdeveloper's Expression Builder UI?

    [Is it possible to create a table of contents in Crystal Reports?|http://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/oss_notes_boj/sdn_oss_boj_erq/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/scn_bosap/notes%7B6163636573733d36393736354636443646363436353344333933393338323636393736354637333631373036453646373436353733354636453735364436323635373233443330333033303331333233313335333133303330%7D.do]

  • Self-join query to Analytical function query

    Hi All,
    I have converted a self-join query to Analytical function query due to the performance reasons.
    Query which is using Analytical function is giving the correct count as I compared it with query using Self-Join. Can you please tell what is wrong in the query.
    ==========================
    Query using Self-Join
    select count(1)
    From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
    from ohr_pers_curr t1 ) pers
         , (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
         from OHR_JOB_CURR t2) job
    where pers.empl_id = job.empl_id
    and pers.dw_creation_dt=pers.pers_max_date
    and job.dw_creation_dt=job.job_max_date
    and job.dummy_row_flag = 'N'
    and pers.dw_creation_rsn_cd in ('N', 'U')
    and job.dw_creation_rsn_cd in ('N', 'U')
    ================================================
    Query Using Analytical function
    select count(1)
    From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
    from ohr_pers_curr t1 ) pers
         , (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
         from OHR_JOB_CURR t2) job
    where pers.empl_id = job.empl_id
    and pers.dw_creation_dt=pers.pers_max_date
    and job.dw_creation_dt=job.job_max_date
    and job.dummy_row_flag = 'N'
    and pers.dw_creation_rsn_cd in ('N', 'U')
    and job.dw_creation_rsn_cd in ('N', 'U')
    ==================================

    Hi David,
    The base is same but the problem is different.
    As far as implementation concern these queries looks same, but when I see there counts, they do not match. I do not have any reason why this happening.
    Regards
    Gaurav

  • How Can I install Web Service on Oracle 11g r1?

    How Can I install Web Service on Oracle 11g r1?

    Hi,
    Is necesary install XDB?
    Because i have executed;
    select * from dba_registry
    where COMP_ID = 'XDB'
    Oracle XML Database
    Regards

  • How to take a backup manually in oracle 11g express edition?

    how to take a backup manually in oracle 11g express edition? i want to take backup of a database and want to restore later on on a different computer .

    Stop database, and copy all files to another machine.That sounds to me to be the closest answer to the question posted. And perhaps the easiest solution.
    Install XE on the "another machine", shut it down. Shutdown the source database. Copy the datafiles to the new host. Startup your "new" database, it will be an exact clone of the original.
    To find out what files need to be copied, as system (or sysdba) run:
    select name from v$datafile;
    ... file1, file2 ...
    show parameter control
    ... control_files ... file1 ...
    alter database backup controlfile to trace [reset logs];
    show parameter diag;
    ... diagnostic_dest ... <drive:>|/diag_pathTo do a proper database clone its better to recreate the controlfile and reset the archive sequence number- leave out the controlfile copy part. In the diag_path location find the trace file under diag/rdbms/$ORACLE_SID/<db_name>/trace it should be the latest ora*.trc file.
    The trace file has the commands to recreate the controlfile, which includes the datafiles and putting the tempfile back in place. If the datafiles are in a different location, i.e. in Windows going to a different drive, or you want do put datafiles in a different folder location, the create controlfile statement is the easiest time to get the file directories right.
    If you add the "reset logs" qualifier (don't use the brackets!) you'll get just the reset logs version of the create controlfile statement, otherwise you'll get both NORESETLOGS and RESETLOGS versions of a create controlfile statement, run one or the other. Not both. Not the entire trace file, just the create controlfile, alter database open [reset logs], alter tablespace temp add tempfile ... bits. No recovery is needed for a database that is shutdown when datafiles get copied to a new location/host.

  • How to resolve Self Joins in OBIEE

    Hi All
    Could any one help how to resolve the Self Join relations in OBIEE (ex. "SCOTT.EMP" table has "EMPNO" column which is referenced by "MGR" column in the same table.
    Request:
    Need to drill down from the Top manager to his employees and so on
    Another example can be applied for reporting the Organisation Chart

    This is a great article that shows ho to do the Alias:
    Adding an Alias table within the BI physical layer using Oracle BI Administration Tool
    http://www.mandsconsulting.com/adding-an-alias-table-within-oracle-bi-administration-tool
    Thanks
    Subra

  • Need help with self join query

    Hello,
    I have table A with the following data
    oid parent_oid
    10 4
    4 2
    2 2
    12 6
    6 6
    parent_oid is the parent of oid. I'd like a query that shows the final parent of the oid. The result should show the following
    oid final parent
    10 2
    4 2
    2 2
    12 6
    6 6
    I'm using Oracle 10g. I'm familiar with self joins, but that alone will not do the job. Thanks!

    Hi,
    arizona9952 wrote:
    ... I'm familiar with self joins, but that alone will not do the job.You're absolutely right!
    A 2-way self join would work for rows have no parent, or rows that are directly connected to their final ancestor (such as oid=4), but not for anything farther away.
    A 3-way self-join would work for one more level away from the final row, but no more. That would be enough for the small set of sample data that you posted, but it would not work if you added a new row with parent_id=10.
    An N-way self-join would work for up to N+1 levels, but no more.
    You need something that can go any number of levels, such as CONNECT BY:
    SELECT     CONNECT_BY_ROOT oid     AS oid
    ,     parent_oid          AS final_parent
    FROM     a
    WHERE     CONNECT_BY_ISLEAF     = 1
    CONNECT BY     oid     = PRIOR parent_oid
         AND     oid     != parent_oid
    ;Edited by: Frank Kulash on Feb 22, 2010 7:09 PM
    Upon sober reflection, I think that a Top-Down query, like the one below, would be more efficient than a Bottom-Up query, like the one above:
    SELECT     oid
    ,     CONNECT_BY_ROOT     parent_oid     AS final_parent
    FROM     a
    START WITH     parent_oid     = oid
    CONNECT BY     parent_oid     = PRIOR oid
         AND     oid          != PRIOR oid
    ;

  • How to write self join in sql?

    Hi,
    I have table named "table_upload", column "record_type" value "01,03,04....." and start_date,end_date and so on
    And i will have value for  start_date,end_date only for record_type=01,rest of type these two columns will be null.
    now i need to write query with self join, to include above concept. can any one please help me .
    and my query look like follows,

    I just want to re write
    Please provide DDL+DML + expected output!
    and short explanation what is it that we all ask you for:
    DDL = Data Definition Language. In our case that is, CREATE TABLE statements for your tables and other definitions that are needed to understand your tables structure and there for let us to test and reproduce the problem in our server. Without DDL no one
    can execute any query.
    How to get DDL: Right click on the table in Object Explorer and select script table as CREATE. Post these create table scripts here.
    DML = data manipulation language is a family of queries used for manipulating the data it self like: inserting, deleting and updating data. In our case we need some sample data in order to check the query and get result, so we need some indert query for
    sample data.
    If you post a "create table query" for the tables and "insert <table> query" with some sample, then we could help you without Assuming/Guessing. There is a reason that DDL is generally asked for and expected when discussing query problems - it helps
    to identify issues, clarify terminology and prevent incorrect assumptions.  Sample data also provides a common point of reference for the discussion. A script that can be used to illustrate or reproduce the issue you have, will encourage others to help.
    [Personal Site] [Blog] [Facebook]

  • How to Avoid Self Join

    Hi
    Assume I have following table
    Id Code
    1 aa
    1 bb
    1 cc
    2 bb
    2 cc
    The records will be grouped by Id. I want to find out group containing code say 'aa' and 'bb'.
    Is it possible to obtain the desired result in a single query without using Self join?
    Regards
    ~Pravin

    This is a little clumsy....Anyway try it:
    WITH t AS
    (SELECT 1 id,'aa' code FROM dual
    UNION ALL
    SELECT 1,'bb' FROM dual
    UNION ALL
    SELECT 1,'cc' FROM dual
    UNION ALL
    SELECT 2,'bb' FROM dual
    UNION ALL
    SELECT 2,'cc' FROM dual)
    SELECT id FROM t
    WHERE code IN('aa', 'bb')
    GROUP BY id HAVING COUNT(*) = 2;

  • How to tune past SQL query??

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

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

  • How to write REF-CURSOR Query in Oracle Reports

    Hello Guys!!
    I have a form in which you can select regions/divisions/locations etc by the use of check boxes. And the selected values will be inserted into a table, and based on the selected values of the table the report is run.
    The issue I have is with the query inside the Oracle reports(attached to this file).
    The query works fine until the last two EXISTS conditions.
    IF a region exists In the table report_param then it works fine but if there are no divisions in it , then the query returns no values, which is not correct.
    Someone has advised me to use a ref-cursor query inside reports tool, which I am not aware off. So, anykind of suggestions or advises are welcome. Please let me know about it as it is very urgent issue for me. Anykind of help would be greatly
    appreciated.
    Thanks,
    Vishal
    -------------------------------------------------------Query in Oracle Reports---------------------------------------------------------
    select c.key_segment, p.supplier_id,
    decode(:in_col_nm, 'BRAND',nvl(p.product_brand,'<Unknown Brand>'), 'PLN',nvl(p.product_legal_name,'<Unknown Legal Name>')) COL_NM,
    sum(a.ext_price) sales_dols,
    sum(comp_allow_pkg.get_comp_allow_stddiv(a.control_loc_id, a.product_id, a.sold_to_customer_id,
    a.doc_dt, a.ext_price, a.units)) cust_reb_dols,
    sum(a.units) units,
    sum(a.ext_cost) cost_dols
    from sales a, key_segment_plns c, product p, rep_wrtr_dw_cust h
    where a.doc_dt between :in_start_dt and :in_end_dt
    and a.customer_oc = h.control_loc_id
    and a.sold_to_customer_id = h.sold_to_cust_id
    and a.ship_to_customer_id = h.ship_to_cust_id
    and ((:in_dg_cd = 'B' and h.dealer_grower_cd in ('D','G')) or h.dealer_grower_cd = :in_dg_cd)
    and a.product_id = p.product_id
    and p.product_gl_class_cd = 'CHEM'
    and p.product_legal_name = c.product_legal_name
    and c.key_segment in ('GLYPHOSATE','PLANT HEALTH/RUST FUNGICIDES','PYRETHROIDS','STROBI FUNGICIDES')--&IN_KEY_SEGMENTS
    -- and (:in_oc = 'ALL' or (a.control_loc_id in (select control_loc from control_loc_comb_ocs where control_loc_comb = :in_oc)))
    -- SALES DATA FILTERS TO MATCH ACCUM_SALES_DG_MV
    and a.sale_type_cd in ('02','08')
    and a.document_type_cd in ('I','C','D')
    and (substr(a.product_id,-1) in ('0','1') OR nvl(upper(trim(p.product_brand)),'X') = 'TECH FEE')
    and a.units <> 0
    and a.unit_cost <> 0
    and a.unit_price <> 0
    -- NEW FILTERS ADDED 9/11/07: LOCATION(BRANCH), BUSINESS TYPE, RSM/ASM/REP
    and ((:in_loc = 'ALL') or (nvl(a.warehouse_id,'<blank>') in (SELECT param_value
    FROM report_param
    WHERE report_id = :IN_REPORT_ID
    AND session_id= :IN_SESSION_ID
    AND USER_ID = :IN_USER_ID
    AND param_name='LOCATION_TYPE')))
    and ((:in_uhs_ag = 'ALL') or (:in_uhs_ag = 'NA' and p.product_uhs_ag != 'A') or (p.product_uhs_ag = :in_uhs_ag))
    and ((:in_sales_rep = 'ALL') or (nvl(a.territory_id,'<blank>') in (SELECT param_value
    FROM report_param
    WHERE report_id = :IN_REPORT_ID
    AND session_id= :IN_SESSION_ID
    AND USER_ID = :IN_USER_ID
    AND param_name='SALES_REP_TYPE')))
    and EXISTS
    (SELECT '1'
    FROM locations l, report_param rp
    WHERE rp.report_id = :IN_REPORT_ID
    AND rp.session_id= :IN_SESSION_ID
    AND rp.user_id = :IN_USER_ID
    AND rp.param_value = l.region
    AND rp.param_name = 'REGION_TYPE'
    AND a.warehouse_id = L.ARS_LOCATION)
    and EXISTS
    (SELECT '1'
    FROM locations l, report_param rp
    WHERE rp.report_id = :IN_REPORT_ID
    AND rp.session_id= :IN_SESSION_ID
    AND rp.user_id = :IN_USER_ID
    AND rp.param_value = l.region
    AND rp.param_name = 'DIVISION_TYPE'
    AND a.warehouse_id = L.ARS_LOCATION)
    group by c.key_segment, P.supplier_id,
    decode(:in_col_nm, 'BRAND',nvl(p.product_brand,'<Unknown Brand>'), 'PLN',nvl(p.product_legal_name,'<Unknown Legal Name>'))

    Hi,
    I need your help to create a report using Ref-Cursor. please see the below thread
    Report using ref cursor or dynamic Sql

  • Parallel  query in Oracle 11g

    We use Oracle 11g DB on windows2008R2.
    We wrote very long and heavy SELECT SQL query usign several table join and sub-queries and it take very long time to get result.
    We did SQL statement tuning as much as we can do so far. ( I will get the Execution Plan and ADDM, etc access right)
    I today notice about Parallel query function.
    Where could I write parallel hint phrases in very long SELECT SQL query ?
    In each selected tables like below ?
    /sample in Oracle Doc/
    SELECT /*+ PARALLEL(employees 4) PARALLEL(departments 4)
           USE_HASH(employees) ORDERED */ MAX(salary), AVG(salary)
    FROM employees, departments
    WHERE employees.department_id = departments.department_id
    GROUP BY employees.department_id;

    You need to be careful with some of the examples in the docs. The example you quote includes an unnecessary join. Before considering parallel query, it should have been re-written to this:
    SELECT MAX(salary), AVG(salary)
    FROM employees
    WHERE department_id is not null
    GROUP BYdepartment_id;
    Later versions of the CBO may do this re-write for you, but it is important to understand why the SQL is inefficient before throwing parallel servers at it. Are you sure that all your joins are actually necessary? (I know you are just going to say "yes", but you might want to think about it first.)

  • How to Create Manual Standby for a Oracle 11g RAC with ASM to Single Instan

    Hi All,
    I have a task to configure a Single Instance Standby Database with ASM for 2-node Primary RAC+ASM database.
    Version using : 11.1.0.6 Oracle 11g --- *" STANDARD EDITION "* Please note datagaurd will not be supported in standard edition.
    Primary database - 2 -Node RAC using ASM storage (All datafiles, redologs, controfile and archive logs)
    Need to setup a single instance standby database manually and than using scripts to transfer the archive logs from primary server to standby server to do recovery time to time.
    Please let me know if there are any configuration document(s) which can help me to set the manual standby? or need your kind help to give your valuable ideas how to go in above situation.
    Thanks in advance

    Niall Litchfield wrote:
    El DBA wrote:
    Well if the archive logs are stored in ASM on the primary nodes, you almost definitely want to be using RMAN to access them. Then to transfer between primary and standby there are many options, this is essentially the step that Data Guard takes care of - so if you (and I) are running Standard Edition, this is the part to "worry" about.
    I'm not really sure what you mean by this:
    As in standard edition the archive destination will be on ASMI don't think the Edition of Oracle (Standard or Enterprise) dictates where and how you store your archive logs, but it's possible I've misunderstood what you mean...SE dictates that database storage for RAC will be ASM, including archive log files. Anything else is not officially supported. I haven't tested but it may be that you can use LOG_ARCHIVE_DUPLEX_DEST to specify a local filesystem for archive log files as well.
    If that doesn't work you'll have to script rman "backup as copy" jobs regularly and then transport the results of that.
    Niall Litchfield
    http://www.orawin.info/
    By the way, it seems Robert has been pretty helpful, it's polite to give him some points dude. And since this is your thread, not mine, give him a "helpful" from me too :p
    El DBA
    Incidentally if you are forced down the RMAN backup as copy route then this will likely throw your current backup retention strategy somewhat, you'll need to think carefully about when an archivelog can be deleted following backup, how many times it might be backed up and so on. I do understand, and have argued for, the use of SE RAC. I also understand and have argued for and implemented manually managed standby a number of times. However by the time your primary database is a RAC instance and you need a standby for DR etc then you really have to look very seriously at whether Standard Edition is still the right investment or not. You will be spending a lot of DBA time managing and troubleshooting this, and you will find that it is less reliable than the off the shelf solution. I suspect that you are very close to the point where an EE installation becomes a worthwhile investment here.
    Niall Litchfield
    http://www.orawin.info/

  • How to find the size of a Oracle 11g table

    I want to find the size of a table in Oracle 11g.
    I hope, my question is clear.
    Please revert with the reply to my query.
    Regards

    --- List all tables sorted by size
    set lines 100 pages 999
    col     segment_name     format a40
    col      mb           format 999,999,999
    select     segment_name
    ,     ceil(sum(bytes) / 1024 / 1024) "MB"
    from     dba_segments
    where     segment_type = 'TABLE'
    group     by segment_name
    order      by ceil(sum(bytes) / 1024 / 1024) desc
    --- List all tables owned by a user sorted by size
    set lines 100 pages 999
    col     segment_name     format a40
    col      mb           format 999,999,999
    select     segment_name
    ,     ceil(sum(bytes) / 1024 / 1024) "MB"
    from     dba_segments
    where     owner like '&user'
    and     segment_type = 'TABLE'
    group     by segment_name
    order      by ceil(sum(bytes) / 1024 / 1024) desc
    /

Maybe you are looking for

  • Changing user from one company code to another

    Dera all, we are currently using SRM 5.0. presently one user assigned to co.code X, we want to assign this user to co.code Y .he sholud get all the attributes of  co.code Y and should not carry any attributes of co.code X. Kindly help me out. This is

  • TS4147 Ive deleted my contacts, is there a way to restore them from a back up??

    Ive deleted my contacts, is there a way to restore them from a back up??

  • Credit memo referencing

    Hi Gurus, I know that cr memo can be created with reference to a Billing doc or a cr memo request which reference either a billing doc or an order. What i am unclear is the way of describing the referencing. Cases: 1. Cr memo creation with reference

  • Can .air installer some files together with the application?

    I have an installer.air that is created by Adobe Flash. I have another file called settings.cfg which is needed for my application to run. How do let the computer auto install settings.cfg into the installation directory together with the application

  • Should it be like this???

    Hi, When the double[], "loads", is changed in the second line, also the corresponding value in the ArrayList, "loads__", changes! Should it work like this? Seems very strange to me. loads__.add(loads); loads[1] = 345635;Regards, Pingen