Connect by prior subquery - performance problem

Hello,
I have some data which is organized in a folder tree. The requeriement is to be able to search from any subfolder and down.
/Documents
___Folder A
______Doc A
______Doc B
___Folder B
______Doc C
______Doc D
The folder structure is defined in a table called CORNERS where the records(=folders) has a ID/PARENTID relationsship to describe the folder structure.
Another table, called MASTER, contains the main content. Each item has a CORNERID value which defined in which subfolder the document is located.
MASTER
ID CORNERID TITLE INDEX_URL
100 2 Doc A http://xxx/yy.com
101 2 Doc B http://xxz/yy.com
102 3 Doc C http://xyz/yy.com
103 3 Doc D http://xyz/zz.com
CORNERS
ID PARENTID NAME
1 Documents
2 1 Folder A
3 1 Folder B
MASTER table has ~50000 records
CORNERS has ~900 records.
Analyzed nighly and stats are fresh.
Indexes defined:
CORNERS_ID_PARENT_IDX corners(id,parentid)
CORNERS_PARENT_ID_IDX corners(parentid,id)
MASTER_ID_CORNERID_IDX master(id,cornerid)
MASTER_CORNERID_ID_IDX master(cornerid,id)
Oracle Text index (URL based) on MASTER.INDEX_URL
Foreign key defined:
MASTER.CORNERID references CORNERS.ID
If I do a search without involving the hierarchy, then the search runs pretty fast:
SQL> SELECT COUNT(*) FROM (SELECT a.id, a.cornerid FROM MASTER a WHERE (CONTAINS(title,'$ADS AND {S} AND $PARAMETER',2) > 1 OR CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1) );
COUNT(*)
5125
Elapsed: 00:00:00.14
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1354 Card=1 Bytes=15
8)
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MASTER' (Cost=1354 Car
d=758 Bytes=119764)
3 2 BITMAP CONVERSION (TO ROWIDS)
4 3 BITMAP OR
5 4 BITMAP CONVERSION (FROM ROWIDS)
6 5 SORT (ORDER BY)
7 6 DOMAIN INDEX OF 'MASTER_TITLE_IDX' (Cost=470)
8 4 BITMAP CONVERSION (FROM ROWIDS)
9 8 SORT (ORDER BY)
10 9 DOMAIN INDEX OF 'MASTER_IDX' (Cost=650)
Statistics
1462 recursive calls
0 db block gets
5507 consistent gets
347 physical reads
0 redo size
380 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
BUT, if I add a subquery to limit the search to a certain folder tree (which includes ~200 nodes), then the performance is really badly affected. The subquery itself runs fast - around 0.07 seconds, but together with the rest of the query the preformance is really bad:
SQL> SELECT COUNT(*) FROM (SELECT a.id, a.cornerid FROM MASTER a WHERE (CONTAINS(title,'$ADS AND {S} AND $PARAMETER',2) > 1 OR CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1) AND cornerid IN ( SELECT ID FROM corners START WITH id = 2434 CONNECT BY PRIOR id = parentid) );
COUNT(*)
942
Elapsed: 00:00:01.83
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=118 Card=1 Bytes=175
1 0 SORT (AGGREGATE)
2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MASTER' (Cost=19 Card=
1 Bytes=162)
3 2 NESTED LOOPS (Cost=118 Card=8 Bytes=1400)
4 3 VIEW OF 'VW_NSO_1' (Cost=2 Card=6 Bytes=78)
5 4 SORT (UNIQUE)
6 5 CONNECT BY (WITH FILTERING)
7 6 NESTED LOOPS
8 7 INDEX (UNIQUE SCAN) OF 'SYS_C002969' (UNIQUE
) (Cost=1 Card=1 Bytes=4)
9 7 TABLE ACCESS (BY USER ROWID) OF 'CORNERS'
10 6 NESTED LOOPS
11 10 BUFFER (SORT)
12 11 CONNECT BY PUMP
13 10 INDEX (RANGE SCAN) OF 'CORNERS_PARENT_ID_IDX
' (NON-UNIQUE) (Cost=2 Card=6 Bytes=48)
14 3 INDEX (RANGE SCAN) OF 'MASTER_CORNERID_ID_IDX' (NON-
UNIQUE) (Cost=1 Card=38)
Statistics
29267 recursive calls
0 db block gets
55414 consistent gets
140 physical reads
0 redo size
380 bytes sent via SQL*Net to client
503 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
1 rows processed
I've tried an alternative syntax, instead of the IN clause like this:
SELECT COUNT(*) FROM (
WITH folders AS (
SELECT ID
FROM CORNERS
START WITH ID=2434
CONNECT BY PRIOR ID= PARENTID
SELECT a.id
FROM MASTER a, folders b
WHERE a.cornerid = b.id
AND CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1);
It does runfaster, but still takes around 1 second.
Any suggestion on how to make this run faster!?
Thanks in advance!
-Mats

how long does it takes to complete the query?

Similar Messages

  • CONNECT BY PRIOR and performance of Query Plan

    Anyone,
    I have an SQL Statement that is performing rather slow and I am trying to figure out if I could optimize it. Here is the SQL:
       SELECT/*+ index(MAXIMO.EQNDX99) */
            maximo.equipment.eqnum, maximo.equipment.parent, LEVEL
       FROM maximo.equipment@maxi_dblink
       WHERE parent = :b1 CONNECT BY PRIOR eqnum = parent
       ORDER BY eqnum, LEVELAfter some research in this board I followed some advice found to create an index on the table for both the eqnum, parent and the parent, eqnum. EQNDX99 and EQNDX999 respectivley.
    Now the Qery Plan for this query shows the following:
    SELECT STATEMENT (REMOTE)
       SORT (ORDER BY)
          FILTER
             CONNECT BY
                 INDEX (FAST FULL SCAN) EQNDX99 (NON-UNIQUE)
                 TABLE ACESS (BY USER ROWID) EQUIPMENT
                 INDEX (RANGE SCAN) EQNDX999 (NON-UNIQUE)Now it appears to be using both indexes but it is operating through a DBLINK. Is there anything else I can do to increase performance??? It appears to be using the HINT through the link as well.
    Thanks for any help I can get,
    David Miller

    how long does it takes to complete the query?

  • Connect By Prior - poor performance

    Hi,
    SELECT parent_id, parent_Val_id,child_value,chld_chr_id FROM (
    SELECT
    mo.parent_id ,
    mo.parent_Val_id,
    mo.child_value,
    mo.chld_chr_id ,
    leaf_param
    FROM char_Value mo
    ,char_pairing CP
    ,Hierarcy_t hier
    , ref_v reft
                        where CP.chr_pair_id =mo.chr_pair_id and CP.hier_id = hier.hier_id
    and hier.Hierarcy_typ_ref_id = reft.ref_id
    START WITH mo.parent_id = 1888
    AND mo.parent_Val_id = 11738095
    CONNECT BY NOCYCLE PRIOR mo.chld_chr_id = mo.parent_id
    AND PRIOR mo.child_value = mo.parent_Val_id
    ) where leaf_param='Y';
    Rows In the table
    char_Value - 62606506
    char_pairing - 16225
    Hierarcy_t - 106
    ref_v - 746
    Plan hash value: 4222991804
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 49 | 2744 | 67966 (1)| 00:00:02 |
    |* 1 | VIEW | | 49 | 2744 | 67966 (1)| 00:00:02 |
    |* 2 | CONNECT BY WITH FILTERING | | | | | |
    | 3 | NESTED LOOPS | | | | | |
    | 4 | NESTED LOOPS | | 48 | 3840 | 33830 (1)| 00:00:01 |
    |* 5 | HASH JOIN | | 137 | 6987 | 44 (3)| 00:00:01 |
    |* 6 | HASH JOIN | | 1 | 42 | 8 (13)| 00:00:01 |
    | 7 | NESTED LOOPS | | 6 | 204 | 4 (0)| 00:00:01 |
    | 8 | TABLE ACCESS BY INDEX ROWID| REFERENCE_TYP | 1 | 24 | 1 (0)| 00:00:01 |
    |* 9 | INDEX UNIQUE SCAN | REFERENCE_TYP_IND | 1 | | 0 (0)| 00:00:01 |
    |* 10 | TABLE ACCESS BY INDEX ROWID| REFERNCE | 6 | 60 | 3 (0)| 00:00:01 |
    |* 11 | INDEX RANGE SCAN | REF_AK1_IDX | 6 | | 1 (0)| 00:00:01 |
    | 12 | TABLE ACCESS STORAGE FULL | Hierarcy | 106 | 848 | 3 (0)| 00:00:01 |
    | 13 | TABLE ACCESS STORAGE FULL | char_pairing | 16191 | 142K| 36 (0)| 00:00:01 |
    |* 14 | INDEX RANGE SCAN | char_pairing_IND | 4119 | | 9 (0)| 00:00:01 |
    |* 15 | TABLE ACCESS BY INDEX ROWID | char_Value | 1 | 29 | 262 (0)| 00:00:01 |
    |* 16 | HASH JOIN | | 1 | 106 | 34135 (1)| 00:00:01 |
    | 17 | NESTED LOOPS | | 43 | 4171 | 34098 (1)| 00:00:01 |
    |* 18 | HASH JOIN | | 43 | 2924 | 33840 (1)| 00:00:01 |
    | 19 | TABLE ACCESS STORAGE FULL | Hierarcy | 106 | 848 | 3 (0)| 00:00:01 |
    |* 20 | HASH JOIN | | 300 | 18000 | 33836 (1)| 00:00:01 |
    | 21 | NESTED LOOPS | | 48 | 2400 | 33831 (1)| 00:00:01 |
    | 22 | TABLE ACCESS BY INDEX ROWID| REFERENCE_TYP | 1 | 24 | 1 (0)| 00:00:01 |
    |* 23 | INDEX UNIQUE SCAN | REFERENCE_TYP_IND | 1 | | 0 (0)| 00:00:01 |
    | 24 | CONNECT BY PUMP | | | | | |
    |* 25 | TABLE ACCESS STORAGE FULL | REFERNCE | 739 | 7390 | 5 (0)| 00:00:01 |
    |* 26 | TABLE ACCESS BY INDEX ROWID | char_Value | 1 | 29 | 6 (0)| 00:00:01 |
    |* 27 | INDEX RANGE SCAN | char_Value_IND | 4 | | 2 (0)| 00:00:01 |
    | 28 | TABLE ACCESS STORAGE FULL | char_pairing | 16191 | 142K| 36 (0)| 00:00:01 |
    Is there any alternate to modify the query.
    Please provide the solution
    Regards
    Sudhakar P.

    Please describe your tables and indexes.
    In the mean time, I'll take a stab on the information we have so far. I don't know where "leaf_param" comes from.create index cv_connect_indx on char_Value(parent_id, parent_Val_id, chld_chr_id, child_value, chr_pair_id);
    SELECT parent_id, parent_Val_id, child_value, chld_chr_id
      FROM (SELECT mo.parent_id,
                   mo.parent_Val_id,
                   mo.chld_chr_id,
                   mo.child_value,
                   mo.chr_pair_id
              FROM char_Value mo
             START WITH mo.parent_id = 1888
                    AND mo.parent_Val_id = 11738095      
            CONNECT BY NOCYCLE PRIOR mo.chld_chr_id = mo.parent_id
                   AND PRIOR mo.child_value = mo.parent_Val_id       
            ) c,
            char_pairing CP, Hierarcy_t hier, ref_v reft
      where CP.chr_pair_id = c.chr_pair_id
        and CP.hier_id = hier.hier_id
        and hier.Hierarcy_typ_ref_id = reft.ref_id
        and leaf_param = 'Y';Edited by: Adam Martin on Feb 13, 2012 1:53 PM

  • Slow connect by prior ... start with subquery in 9i

    Has anyone come across a performance problem (compared to 8i) when using hierarchical queries where the START WITH list is generated by a subquery? The culprit seems to be an extra visit to the subquery block as part of the CONNECT BY WITH FILTERING operation.
    For example, take a simple tree structure:
    CREATE TABLE tree
    id NUMBER,
    parentid NUMBER
    CONSTRAINT tree_pk PRIMARY KEY (id)
    ...and a subquery - here just a table called sample with a subset of the ids from the tree table:
    CREATE TABLE sample
    id NUMBER,
    CONSTRAINT sample_pk PRIMARY KEY (id)
    ...with which to drive the start points of the treewalk:
    SELECT parentid, id, label
    FROM tree
    CONNECT BY PRIOR parentid = id
    START WITH id IN
    SELECT id FROM SAMPLE
    With the tables populated and analyzed, I get this from 8i:
    Execution Plan
    .0......SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=19)
    .1....0...CONNECT BY
    .2....1.....NESTED LOOPS (Cost=1 Card=1280 Bytes=10240)
    .3....2.......INDEX (FAST FULL SCAN) OF 'ID_PK' (UNIQUE) (Cost=1 Card=1280 Bytes=5120)
    .4....2.......INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE)
    .5....1.....TABLE ACCESS (BY USER ROWID) OF 'TREE'
    .6....1.....TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (Cost=2 Card=1 Bytes=19)
    .7....6.......INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE) (Cost=1 Card=1)
    Statistics
    .....0..recursive calls
    .....4..db block gets
    .15687..consistent gets
    ....59..physical reads
    .....0..redo size
    223313..bytes sent via SQL*Net to client
    .38276..bytes received via SQL*Net from client
    ...343..SQL*Net roundtrips to/from client
    .....3..sorts (memory)
    .....0..sorts (disk)
    ..5120..rows processed
    and this is 9i:
    Execution Plan
    .0......SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=19)
    .1....0...CONNECT BY (WITH FILTERING)
    .2....1.....NESTED LOOPS
    .3....2.......NESTED LOOPS (Cost=2 Card=1280 Bytes=10240)
    .4....3.........INDEX (FAST FULL SCAN) OF 'ID_PK' (UNIQUE) (Cost=2 Card=1280 Bytes=5120)
    .5....3.........INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE)
    .6....2.......TABLE ACCESS (BY USER ROWID) OF 'TREE'
    .7....1.....NESTED LOOPS
    .8....7.......BUFFER (SORT)
    .9....8.........CONNECT BY PUMP
    10....7.......TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (Cost=2 Card=1 Bytes=19)
    11...10.........INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE) (Cost=1 Card=20480)
    12....1.....INDEX (UNIQUE SCAN) OF 'SAMPLE_PK' (UNIQUE) (Cost=1 Card=1 Bytes=4)
    Statistics
    .....1..recursive calls
    .....1..db block gets
    .20525..consistent gets
    ....72..physical reads
    ...120..redo size
    224681..bytes sent via SQL*Net to client
    .38281..bytes received via SQL*Net from client
    ...343..SQL*Net roundtrips to/from client
    .....9..sorts (memory)
    .....0..sorts (disk)
    ..5120..rows processed
    ..so, about another 5000 logical reads, corresponding to the extra access of the sample table at the bottom of the query plan. So instead of just visiting the START WITH subquery once, to kick off the treewalk, I seem to be revisiting it for every row returned. Not too bad if that happens to be a unique index scan as here but that's not always the case.
    I know I've got new options for re-writing this as a join under 9i, I'm just curious about those extra lookups and why they're necessary.
    Cheers - Andrew

    There is undocumented parameter in Oracle 9i "_old_connect_by_enabled"
    which controls the behavoiur of hierarchy queries in 9i and above:
    You can try to return to 8i behaviour using it:
    SQL> SELECT parentid, id
      2  FROM tree
      3  CONNECT BY PRIOR parentid = id
      4  START WITH id IN
      5  (
      6  SELECT id FROM SAMPLE
      7  )
      8  /
    Execution Plan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=26)
       1    0   CONNECT BY (WITH FILTERING)
       2    1     TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE)
       3    2       NESTED LOOPS (Cost=2 Card=1 Bytes=26)
       4    3         TABLE ACCESS (FULL) OF 'SAMPLE' (TABLE) (Cost=2 Card
              =1 Bytes=13)
       5    3         INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (C
              ost=0 Card=1 Bytes=13)
       6    1     NESTED LOOPS
       7    6       BUFFER (SORT)
       8    7         CONNECT BY PUMP
       9    6       TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE) (Cost=
              1 Card=1 Bytes=26)
      10    9         INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (C
              ost=1 Card=1)
      11    1     TABLE ACCESS (FULL) OF 'TREE' (TABLE) (Cost=1 Card=1 Byt
              es=26)
      12    1     INDEX (UNIQUE SCAN) OF 'SAMPLE_PK' (INDEX (UNIQUE)) (Cos
              t=1 Card=1 Bytes=13)
    SQL> alter session set "_old_connect_by_enabled" = TRUE;
    Session altered.
    SQL> SELECT parentid, id
      2  FROM tree
      3  CONNECT BY PRIOR parentid = id
      4  START WITH id IN
      5  (
      6  SELECT id FROM SAMPLE
      7  )
      8  /
    Execution Plan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=26)
       1    0   CONNECT BY
       2    1     NESTED LOOPS (Cost=2 Card=1 Bytes=26)
       3    2       TABLE ACCESS (FULL) OF 'SAMPLE' (TABLE) (Cost=2 Card=1
               Bytes=13)
       4    2       INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (Cos
              t=0 Card=1 Bytes=13)
       5    1     TABLE ACCESS (BY USER ROWID) OF 'TREE' (TABLE)
       6    1     TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE) (Cost=1
              Card=1 Bytes=26)
       7    6       INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (Cos
              t=1 Card=1)
    Rgds.

  • CONNECT BY, Performance problems with hierarchical SQL

    Hello,
    I have a performance problem with the following SQL:
    table name: testtable
    columns: colA, colB, colC, colD, colE, colF
    Following hierarchical SQL:
    SELECT colA||colB||colC AS A1, colD||colE||colF AS B1, colA, colB, colC, level
    FROM testable
    CONNECT BY PRIOR A1 = B1
    START WITH A1 = 'aaa||bbbb||cccc'
    With big tables the performance of this construct is very bad. I can't use functional based indexes to create an Index on "colA||colB||colC" and "colD||colE||colF"
    Has anyone an idea how I can get a real better performance for this hierarchical construct, if I have to combine multiple colums? Or is there any better way (with an PL/SQL or view trigger) solve something like this ??
    Thanks in advance for your investigation :)
    Carmen

    Why not
    CONNECT BY PRIOR colA = colD
    and PRIOR colB = colE
    and ...
    ? It is not the same thing, but I suspect my version is correct:-)

  • Oracle Virtual Machine Connect by Prior Problem

    Hi Friends ,
    In my company we have virtualization. this is new in our organization.
    oracle image have been taken and put in to virtual machine.(vmware).
    after that, procedure which has "connect by prior" start to create wrong output.
    please help us on what to do to correct problem given above.
    (my oracle version 10g)

    JAVAMAN2 wrote:
    simple sample:
    SELECT ROWNUM+2000-1 AS YIL FROM DUAL CONNECT BY ROWNUM <= 2010-2005 ORDER BY ROWNUM+2000-1 DESC;output on virtual machine
    2009,2008,2007,2006,2005
    output on real world
    2010,2009,2008,2007,2006,2005If that is really the output from that query, then both your virtual machine and your "real world" are broken. On all 4 versions of Oracle I have available, I get:
    SQL> SELECT ROWNUM + 2000 - 1 AS YIL
      2  FROM DUAL
      3  CONNECT BY ROWNUM <= 2010 - 2005
      4  ORDER BY ROWNUM + 2000 - 1 DESC;
           YIL
          2004
          2003
          2002
          2001
          2000Which is what I would expect given that 2010 - 2005 = 5.
    Perhaps if you showed an actual cut and paste from a sqlplus session on each machine someone might be able to help, however, I'm inclined to agree with sb92075 that it is a data issue in your real query.
    John

  • Connect by prior performance

    In our application, we have a lot of 'trees' in our data (parent/child relationships within a table). We rely heavily on 'connect by prior' to search through the trees, to find for example values that are defined in one of the parents ('inheritance').
    Lately however, we've been eliminating connect by priors wherever we can lately, because performance is dramatic. No matter what indexes we use, we keep getting full tablescans on large tables because of the connect by priors. Views with 2 connect by priors are deadly for performance.
    What is a good way to optimize connect by priors?
    As an example of why we use connect by prior, consider this:
    table A
    ID   PARENT_ID   NAME    LENGTH
    1    NULL        root    10
    2    1           child   NULLWe consider record 2 'derived' from record 1, inheriting its length. The alternative for connect by prior is to populate the child length whenever the master length changes, and this is what we do in most situations now, but it's hell to keep track of this. If a record changes, you would still have to go down the tree to change all child lengths etc, so we rather use connect by prior.
    Any ideas?

    Ivo, would you please post the table definition (if different than your example), the actual list of indexes, a sample query, and the explain plan for that query?
    I use hierarchical queries extensively on tables with millions of rows. They work very well if all the pieces are in place. Let's see if we can get your queries humming...

  • Connect by prior by speific order problem.

    Oracle: 10.2.0.4
    I have a table containing events backup.
    i want to list the hierarchy within that backup oreder by the time_stamp for all levels.
    create table BCK_EVENTS
      bck_backup_id           NUMBER(9) default 0 not null,
      event_id                   NUMBER(15) default 0 not null,
      event_name              NVARCHAR2(100) default ' ' not null
      time_stamp               NUMBER(9) default 0 not null,
      parent_event            NUMBER(15) default 0 not null,
    add constraint BCK_EVENTS_PK primary key (BCK_EVENT_ID, EVENT_ID); // event_id is not unique can be under one or more backup id'sthis is not a tree with one root, there is more than one event in the root level (level 1).
    example:
    Event Name      Time         level   <- time is numeric but for easier reading.
    *Event A          10:00         1
       *Event C        10:30         2
         *Event B      11:17         3
    *Event H          12:10         1
         *Event J       12:10         2
         *Event M      12:21         2
    *Event Z          15:33         1
       *Event R        16:56          2
        *Event M      16:57          3
       *Event G        20:20         2What i tried was :
    select  lpad( '*', level*2 ) || event_id,event_name,time_stamp,parent_event,level from bck_events
         where bck_event_id=100031
         start with parent_event is null
        connect by prior event_id = parent_eventand there are two problems with it.
    1. it's not ordered even when i added an Index (parent_event,time_stamp) and try to hint it.
    2. it returns loads of multiple rows.
    hope it's clear enough. I thank for any help.
    Edited by: 973065 on Nov 25, 2012 8:04 AM
    Edited by: 973065 on Nov 25, 2012 9:23 AM
    Edited by: 973065 on Nov 25, 2012 9:29 AM
    Edited by: 973065 on Nov 25, 2012 9:31 AM

    Hi,
    973065 wrote:
    Oracle: 10.2
    I have a table containing events backup.
    i want to list the hierarchy within that backup oreder by the time_stamp for all levels.
    create table BCK_EVENTS
    bck_backup_id           NUMBER(9) default 0 not null,
    event_id                   NUMBER(15) default 0 not null,
    event_name              NVARCHAR2(100) default ' ' not null
    time_stamp               NUMBER(9) default 0 not null,
    parent_event            NUMBER(15) default 0 not null,
    Thanks for posting the version number and the CREATE TABLE statement. Don't forget to post INSERT statements for your sample data.
    add constraint BCK_EVENTS_PK primary key (BCK_EVENT_ID, EVENT_ID); // event_id is not unique can be under one or more backup id's
    this is not a tree with one root, there is more than one event in the root level (level 1).Is it a forest, that is, a set of trees?
    example:
    Event Name      Time         level   <- time is numeric but for easier reading.
    *Event A          10:00         1
    *Event C        10:30         2
    *Event B      11:17         3
    *Event H          12:10         1
    *Event J       12:10         2
    *Event M      12:21         2
    *Event Z          15:33         1
    *Event R        16:56          2
    *Event M      16:57          3
    *Event G        20:20         2
    That seems to be a forest, that is, every row has 0 or 1 parent, and no row is its own ancestor.
    What i tried was :
    select lpad( '*', level*2 ) || event_id,event_name,time_stamp,parent_event,level from bck_events
    where bck_event_id=100031
    start with parent_event is null
    connect by prior event_id = parent_event
    and there are two problems with it.
    1. it's not ordered even when i added an Index (parent_event,time_stamp) and try to hint it.Depending on your data, you may just need to add
    ORDER SIBLINGS BY  time_stampat the end, after the CONNECT BY clause.
    If you need rows sorted by time_stamp under their roots, but otherwise without regard to the hierarchy, then use CONNECT_BY_ROOT.
    2. it returns loads of multiple rows.Again, it depends on your data. I'll bet you need something more in the CONNECT BY clause, but I can't tell what without some sample data and an exxplanation of how you gett the results you posted from that data. The fact that bck_event_id is part of the primary key makes me suspect that maybe bck_event_id needs to be somewhere in the CONNECT BY clause, but that's just a wild guess.
    hope it's clear enough. I thank for any help.As mentioned before, see the forum FAQ {message:id=9360002}

  • Problem with connect by prior

    Hi,
    I've table TAB_MGR:
    EMPLOYEE................MANAGER
    ABCD...................ABC
    ABC.....................AB
    AB......................A
    WAXXY..Y...............WAXX
    WAXX...................WA
    WA.....................W
    I tried this query:
    select a.EMPLOYEE, b.MANAGER
    from
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) a,
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) b
    where a.lv=1 and a.rl=b.rl;
    output is:
    EMPLOYEE....................MANAGER
    ABCD........................ABC
    ABCD........................AB
    ABCD........................A
    ABC.........................AB
    ABC.........................A
    AB..........................A
    WAXXYY.....................WAXX
    WAXXYY.....................WA
    WAXXYY.....................W
    WAXX.......................WA
    WAXX.......................W
    WA.........................W
    but I'd like to get also the level 1 of employee = MANAGER
    In my case I'd like to get this output:
    EMPLOYEE........................MANAGER
    ABCD..........................ABCD
    ABCD..........................ABC
    ABCD..........................AB
    ABCD..........................A
    ABC...........................ABC
    ABC...........................AB
    ABC...........................A
    AB............................AB
    AB............................A
    WAXXYY........................WAXXYY
    WAXXYY........................WAXX
    WAXXYY........................WA
    WAXXYY........................W
    WAXX..........................WAXX
    WAXX..........................WA
    WAXX..........................W
    WA............................WA
    WA............................W
    in my query lacks:
    EMPLOYEE........................MANAGER
    ABCD..........................ABCD
    ABC...........................ABC
    AB............................AB
    WAXXYY........................WAXXYY
    WAXX..........................WAXX
    WA............................WA
    How can I get also this record in my query??
    Thanks!

    Hi,
    I had to add two record at the table TAB_MGR, because W and A haven't manager:
    New tab TAB_MGR is:
    EMPLOYEE................MANAGER
    ABCD...................ABC
    ABC.....................AB
    AB......................A
    WAXXYY.................WAXX
    WAXX....................WA
    WA.......................W
    W.........................
    A..........................
    I have written the query:
    select a.EMPLOYEE, b.MANAGER
    from
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) a,
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) b
    where a.lv=1 and a.rl=b.rl
    AND a.MANAGER IS NOT NULL
    AND b.MANAGER IS NOT NULL
    UNION ALL
    select NVL(r.EMPLOYEE,R.MANAGER),NVL(r.EMPLOYEE,R.MANAGER)
    from TAB_MGR r
    connect by prior r.EMPLOYEE = r.MANAGER
    group by r.EMPLOYEE,R.MANAGER;
    output is:
    EMPLOYEE........................MANAGER
    ABCD..........................ABCD
    ABCD..........................ABC
    ABCD..........................AB
    ABCD..........................A
    ABC...........................ABC
    ABC...........................AB
    ABC...........................A
    AB............................AB
    AB............................A
    A.............................A
    WAXXYY........................WAXXYY
    WAXXYY........................WAXX
    WAXXYY........................WA
    WAXXYY........................W
    WAXX..........................WAXX
    WAXX..........................WA
    WAXX..........................W
    WA............................WA
    WA............................W
    W............................W
    It seems run correctly, but now I have another problem:
    In my table TAB_MGR I have add a new column DESCRIPTION:
    EMPLOYEE................MANAGER.......DESCRIPTION
    A.......................................L1
    AB......................A..............L2
    ABC.....................AB.............L3
    ABCD...................ABC.............L4
    W......................................M1
    WA.......................W.............M2
    WAXX....................WA.............M3
    WAXXYY.................WAXX.............M4
    from my query I'd like to get this output:
    EMPLOYEE........................MANAGER.......DESCRIPTION
    ABCD...................................ABCD..............L1-L2-L3-L4
    ABCD...................................ABC...............L1-L2-L3-L4
    ABCD...................................AB................L1-L2-L3-L4
    ABCD...................................A.................L1-L2-L3-L4
    ABC....................................ABC...............L1-L2-L3
    ABC....................................AB................L1-L2-L3
    ABC....................................A.................L1-L2-L3
    AB.....................................AB................L1-L2
    AB.....................................A.................L1-L2
    A......................................A.................L1
    WAXXYY........................WAXXYY............M1-M2-M3-M4
    WAXXYY........................WAXX..............M1-M2-M3-M4
    WAXXYY........................WA................M1-M2-M3-M4
    WAXXYY........................W.................M1-M2-M3-M4
    WAXX..........................WAXX..............M1-M2-M3
    WAXX..........................WA................M1-M2-M3
    WAXX..........................W.................M1-M2-M3
    WA............................WA................M1-M2
    WA............................W.................M1-M2
    W............................W..................M1
    Have someone any idea of like completing my query?
    Thanks in advance!!!!

  • Problem on Connect by prior...

    Dear all,
    I have a problem that duplicated records are retrieved of the followings:
    SELECT GRPID, itemid, parentid from T_MENU_TREE
    where GRPID = 1
    and exists (select 1 from T_MENU_ROLE x
         where x.grpid = GRPID
         and x.itemcode = itemid)               
    connect by prior itemid = parentid
    start with parentid IS NULL;
    The result is:
    GRPID itemid parentid
    1 aaa A
    1 aaa A
    1 bbb A
    1 bbb A
    1 ccc A
    1 ccc A
    BUT, if amend the sql likes:
    SELECT GRPID, itemid, parentid from T_MENU_TREE
    where GRPID = 1
    connect by prior itemid = parentid
    start with parentid IS NULL
    and exists (select 1 from T_MENU_ROLE x
         where x.grpid = GRPID
         and x.itemcode = itemid)     
    There is no problem.           
    The result is:
    GRPID itemid parentid
    1 aaa A
    1 bbb A
    1 ccc A
    Does anyone know why?
    Please help and thanks in advance.

    jennisy wrote:
    SELECT GRPID, itemid, parentid from T_MENU_TREE
    where GRPID = 1
    *&gt; connect by prior itemid = parentid*
    start with parentid IS NULL
    and exists (select 1 from T_MENU_ROLE x
    where x.grpid = GRPID
    and x.itemcode = itemid)     Something isn't right. Your connect by clause states - "prior itemid = parentid".
    GRPID itemid parentid
    1 aaa A
    1 bbb A
    1 ccc AYet your resultset doesn't show that relationship being satisfied.
    Please post your actual sqlplus session and, if possible, the table structures and relevant data in them.
    isotope

  • Connect By Prior - Performance and Missing Parent

    I have to say right off the top that I'm not a SQL expert by any means so please be gentle. :)
    I have a query that runs through a Bill Of Materials that works except for two things:
    1. It is horribly slow - 60-90 seconds to return under 300 rows.
    2. It doesn't show the parent node.
    I'm looking into indexes now, so I'm not sure if there's an issue there or not. As far as #2 goes, there are two tables that are involved: BOM_STRUCTURES_B and BOM_COMPONENTS_B. Every item below the parent node has a BOM_COMPONENTS_B record. If a BOM_COMPONENT is a parent to other components, there is a BOM_STRUCTURES_B record for that. (In other words, everything that is a parent has a BOM_STRUCTURES_B record, and all the children have a BOM_COMPONENTS_B record that point to the parent - BOM_STRUCTURES_B). The only exception to this is the parent node, which only has a BOM_STRUCTURES_B record (it is NOT a child, so there is no BOM_COMPONENTS_B record). I've added a "UNION" to the bottom of the script below, but it changes my sort order completely.
    Here's my script:
    select bbm.assembly_item_id,
    bic.component_item_id Component ,
    msi.segment1 Name,
    msi.description Description,
    bic.component_quantity Quantity,
    lpad( ' ', level*2 ) || level MyLevel
    from bom_structures_b bbm
    ,bom_components_b bic
    , mtl_system_items msi
    where bbm.bill_Sequence_id = bic.bill_sequence_id
    and msi.inventory_item_id = bic.component_item_id
    and msi.organization_id = bbm.organization_id
    start with bbm.assembly_item_id = 271962
    and bbm.organization_id = 85
    connect by prior bic.component_item_id = bbm.assembly_item_id;
    I've hard-coded "start with bbm.assembly_item_id = 271962", as it is the root node of a tree (BOM).
    Here's my structure, with extra fields clipped out:
    DBMS_METADATA.GET_DDL('TABLE','BOM_STRUCTURES_B','BOM')
    CREATE TABLE "BOM"."BOM_STRUCTURES_B"
    ( "ASSEMBLY_ITEM_ID" NUMBER,
    "ORGANIZATION_ID" NUMBER NOT NULL ENABLE,
    "COMMON_BILL_SEQUENCE_ID" NUMBER NOT NULL ENABLE,
    ) PCTFREE 20 PCTUSED 80 INITRANS 10 MAXTRANS 255 NOCOMPRESS LOGGING
    STORAGE(INITIAL 131072 NEXT 131072 MINEXTENTS 1 MAXEXTENTS 2147483645
    PCTINCREASE 0 FREELISTS 4 FREELIST GROUPS 4 BUFFER_POOL DEFAULT)
    TABLESPACE "APPS_TS_TX_DATA"
    DBMS_METADATA.GET_DDL('TABLE','BOM_COMPONENTS_B','BOM')
    CREATE TABLE "BOM"."BOM_COMPONENTS_B"
    ("COMPONENT_ITEM_ID" NUMBER,
    "BILL_SEQUENCE_ID" NUMBER NOT NULL ENABLE,
    "PARENT_BILL_SEQ_ID" NUMBER,
    ) PCTFREE 35 PCTUSED 50 INITRANS 10 MAXTRANS 255 NOCOMPRESS LOGGING
    STORAGE(INITIAL 131072 NEXT 131072 MINEXTENTS 1 MAXEXTENTS 2147483645
    PCTINCREASE 0 FREELISTS 4 FREELIST GROUPS 4 BUFFER_POOL DEFAULT)
    TABLESPACE "APPS_TS_TX_DATA"
    DBMS_METADATA.GET_DDL('TABLE','MTL_SYSTEM_ITEMS_B','INV')
    CREATE TABLE "INV"."MTL_SYSTEM_ITEMS_B"
    ( "INVENTORY_ITEM_ID" NUMBER NOT NULL ENABLE,
    "ORGANIZATION_ID" NUMBER NOT NULL ENABLE,
    "DESCRIPTION" VARCHAR2(240),
    ) PCTFREE 10 PCTUSED 70 INITRANS 10 MAXTRANS 255 NOCOMPRESS LOGGING
    STORAGE(INITIAL 131072 NEXT 131072 MINEXTENTS 1 MAXEXTENTS 2147483645
    PCTINCREASE 0 FREELISTS 4 FREELIST GROUPS 4 BUFFER_POOL DEFAULT)
    TABLESPACE "APPS_TS_TX_DATA"
    The relationship between the tables is:
    A BOM_STRUCTURES_B has many BOM_COMPONENTS_B where BOM_COMPONENTS_B.PARENT_BILL_SEQ_ID = BOM_STRUCTURES_B.BILL_SEQUENCE_ID. Both BOM_STRUCTURES_B.ASSEMBLY_ITEM_ID and BOM_COMPONENTS_B.COMPONENT_ITEM_ID are related to a single MTL_SYSTEM_ITEMS_B.INVENTORY_ITEM_ID.
    Thanks to anyone who can help with this horrible problem I've been fighting with for much too long! :)
    Thank you!
    Steve

    Due to an error in the otn forums (*), this got posted to the wrong forum, sorry for that, I'll repost in the correct one.
    *) For the forum admins: I was browsing the SQL/PLSQL forum, clicked on 'new message', got the login prompt, logged in, and apparently this got me to the application server forum for some reason.

  • Connect by prior problem with order by

    Hi,
    I and using connect by prior within a query on Oracle 8 and would like to order the results within the parent levels.
    All the documentation that I have read shows that in Oracle 9i there is an option to say order siblings by which looks like what I need, but this does not work on Oracle 8.
    Can anyone tell me how I can order the children within the parents without changing the tree structure?
    I have also tried SYS_CONNECT_BY_PATH and I just get an error saying that it is an invalid column name,

    Karen, see here for a dicussion on how to order the siblings in a pre-9i environment:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:9212348049

  • Problem Creating a query for a hierarchical tree. [using connect by prior]

    Hi all,
    I have 2 tables.
    box (box_id, box_name)
    item(item_id, item_name, box_id)
    In a box there are several items.
    I want to create a hierachical tree to display items that are present in each box.
    LIKE:
    |---BOX1
    | |----ITEM 1
    | |----ITEM 2
    |
    |---BOX2
    | |----ITEM 1
    | |----ITEM 2
    Currently i am trying this query:
    SELECT -1 state, box_name, 'icon' icon, box_id val
    from box b, item i;
    I don't know what value to put for level, i don't know how to code the 'connect by prior' part.
    Could you please advise me?
    Michaël.
    PS. Then i will eventually use this query in forms builder.

    Note the name of this forum is "SQL Developer *(Not for general SQL/PLSQL questions)*" - so only for issues with the SQL Developer tool. Please post these questions under the dedicated SQL And PL/SQL forum.
    Regards,
    K.

  • Connect by prior with the  hint index not improving the performance

    Hi
    I have a table ORDERS (transaction table) with a parent order and multiple child orders with a field called orig_tid which track it back to main order.Child order can be further amended it will have mapping to immediate parent and immediate parent will have mapping to its parent.
    I want find out al the order associated with main order using connect by prior.
    I have created an index on required fileds but still those are not reducing the bytes. It is foing full index scan which are involving big bytes.
    [code]
    create index idx_test on orders decode(length(nvl(orig_tid,0)),9,substr(tran_no,6),orig_tid)
    SELECT  /*+ (mmd IDX_TEST) */ *
    FROM ORDERS mmd
    connect by prior tid=decode(length(nvl(orig_tid,0)),9,substr(tran_no,6),orig_tid)
    start_with or_number='13454566654553'
    [/CODE]
    Explain Plan
    ID | OPERATION                                |  NAME                 | ROWS               | BYTES    | COST
    0 | SELECT STATEMENT         |                                         | 9768              | 5093K   |       6944
    1|CONNECT BY WITH FILTERING|                          |                         |              |
    2|TABLE ACCESS BY INDEX ROWID|ORDERS |               1                 |534|            319K
    3| INDEX FULL SCAN|            IDX_TEST|     976K|    | 5652
    4|NESTED LOOPS|      |  |  |
    5|CONNECT By PUMP |  |  |  |
    6| TABLE ACCESS BY INDEX ROWID| ORDERS|  9768 | 5093K| 6944
    7| INDEX FULL SCAN | IDX_TEST | 3907|  | 5689
    Message was edited by: NikhilJuneja

    post EXPLAIN PLAN for SQL

  • Connect by prior problem

    hi friends i have table like tree which have have two different type of leaf
    my tables
    deparment did,name
    person perid,name
    treetable id, pid,did,perid--tree table one row have only did or pid if did =2 , peridhave to be nullso i generate hierarchial structure between person and deparments
       id      pid       did     perid
        1     null       1        null
        2    1          null        1     
        3     2         null        2
        4     2          null       3   
        5     3          2          null
       6     5          null       4  
        7     5          null       5for example i select first deparment's person querry result must be (id=)2,3,4 not 6,7 because 6,7 below 5
    can we write with connect by prior it?

    Hi,
    JAVAMAN2 wrote:
    hi friends i have table like tree which have have two different type of leaf
    my tables
    deparment did,name
    person perid,name
    treetable id, pid,did,perid--tree table one row have only did or pid if did =2 , peridhave to be nullso i generate hierarchial structure between person and deparments
    id      pid       did     perid
    1     null       1        null
    2    1          null        1     
    3     2         null        2
    4     2          null       3   
    5     3          2          null
    6     5          null       4  
    7     5          null       5for example i select first deparment's person querry result must be (id=)2,3,4 not 6,7 because 6,7 below 5I'm not sure why you want to exclude 5, 6 and 7. Is it that you don't want any nodes (except the root) with NULL perid?
    can we write with connect by prior it?Yes.
    If my guess about excluding 5 and it's descendants is correct:
    SELECT     *     -- or whatever you want
    FROM     treetable
    START WITH     pid     IS NULL
    CONNECT BY     pid     = PRIOR id
         AND     depid     IS NULL
    ;If you need the names from the other tables, then outer-join both other tables, and use NVL to get the one name that is not NULL.

Maybe you are looking for