473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Limit the levels for a query with CONNECT BY

Hi, I have a table containing many millions of rows.
This table has a tree stucture, with the following columns

id
name
parent_id

I need to go through the tree, starting from a specific Id, to find
all the rows with a specific name.

If I use the following:

SELECT id, name
FROM Mytable
WHERE name = 'XYZ'
STARTING WITH id = &1
CONNECT BY id = PRIOR id;

I will find them, but if will traverse the complete tree structure,
even below the rows that contains name='XYZ'. The XYZ can be near the
starting point, let say 2 or 3 levels, when the tree structure can go
down to 50 levels and more (the table is used to store complex SGML
documents).

Is there a way, to save processing time, to say to the query : "Do not
go below XYZ ?" eg when you encounter the 'XYZ' stop connecting below
and go to another branch ?


Thank you

Bernard Drolet
Jul 19 '05 #1
7 15265
Use the "level" implicit variable. You can read about it in th PL/SQL
manual from Oracle.

Daniel
Hi, I have a table containing many millions of rows.
This table has a tree stucture, with the following columns

id
name
parent_id

I need to go through the tree, starting from a specific Id, to find
all the rows with a specific name.

If I use the following:

SELECT id, name
FROM Mytable
WHERE name = 'XYZ'
STARTING WITH id = &1
CONNECT BY id = PRIOR id;

I will find them, but if will traverse the complete tree structure,
even below the rows that contains name='XYZ'. The XYZ can be near the
starting point, let say 2 or 3 levels, when the tree structure can go
down to 50 levels and more (the table is used to store complex SGML
documents).

Is there a way, to save processing time, to say to the query : "Do not
go below XYZ ?" eg when you encounter the 'XYZ' stop connecting below
and go to another branch ?


Thank you

Bernard Drolet

Jul 19 '05 #2
le****@yahoo.ca (Bernard Drolet) wrote in message news:<ee**************************@posting.google. com>...
Hi, I have a table containing many millions of rows.
This table has a tree stucture, with the following columns

id
name
parent_id

I need to go through the tree, starting from a specific Id, to find
all the rows with a specific name.

If I use the following:

SELECT id, name
FROM Mytable
WHERE name = 'XYZ'
STARTING WITH id = &1
CONNECT BY id = PRIOR id;

I will find them, but if will traverse the complete tree structure,
even below the rows that contains name='XYZ'. The XYZ can be near the
starting point, let say 2 or 3 levels, when the tree structure can go
down to 50 levels and more (the table is used to store complex SGML
documents).

Is there a way, to save processing time, to say to the query : "Do not
go below XYZ ?" eg when you encounter the 'XYZ' stop connecting below
and go to another branch ?


Thank you

Bernard Drolet


Hint: there is a LEVEL pseudo function.

Sybrand Bakker
Senior Oracle DBA
Jul 19 '05 #3
sy******@yahoo.com wrote in message news:<a1**************************@posting.google. com>...

Hint: there is a LEVEL pseudo function.

Sybrand Bakker
Senior Oracle DBA


I have two concerns with LEVEL:

First,

I do not know in advance at what level the specific row is ! The table
contains the structure and content of complex SGML documents; the SGML
structure (DTD) can be different from one document to the other and
can even change over time

(for example, I can have a document with
PUBLICATION - CHAPTER - SECTION - TASK
structure today, but in future, the person in charge of the
publication can decide to add a layer, as in
PUBLICATION - CHAPTER - SECTION - PAGEBLOCK - TASK,
so the level to find the TASK is now changed and the query will not
find it anymore).

Second,

Even if I ask to display only the, for example, 3 first LEVELs, am I
right when I say the query will process the complete tree structure
anyway before returning me only the rows I'm interested in ?

Bernard Drolet
Jul 19 '05 #4
If you don't know beforehand the LEVEL value, I'm afraid you will need
first to run a query to find this value. As for your concern that
Oracle will need to process the whole tree just to display N levels,
you're probably right. Run the explain plan, and we'll be able to see
for sure.

Daniel

I have two concerns with LEVEL:

First,

I do not know in advance at what level the specific row is ! The table
contains the structure and content of complex SGML documents; the SGML
structure (DTD) can be different from one document to the other and
can even change over time

(for example, I can have a document with
PUBLICATION - CHAPTER - SECTION - TASK
structure today, but in future, the person in charge of the
publication can decide to add a layer, as in
PUBLICATION - CHAPTER - SECTION - PAGEBLOCK - TASK,
so the level to find the TASK is now changed and the query will not
find it anymore).

Second,

Even if I ask to display only the, for example, 3 first LEVELs, am I
right when I say the query will process the complete tree structure
anyway before returning me only the rows I'm interested in ?

Bernard Drolet

Jul 19 '05 #5
le****@yahoo.ca (Bernard Drolet) wrote in message news:<ee**************************@posting.google. com>...
sy******@yahoo.com wrote in message news:<a1**************************@posting.google. com>...

Hint: there is a LEVEL pseudo function.

Sybrand Bakker
Senior Oracle DBA
I have two concerns with LEVEL:

First,

I do not know in advance at what level the specific row is ! The table
contains the structure and content of complex SGML documents; the SGML
structure (DTD) can be different from one document to the other and
can even change over time

(for example, I can have a document with
PUBLICATION - CHAPTER - SECTION - TASK
structure today, but in future, the person in charge of the
publication can decide to add a layer, as in
PUBLICATION - CHAPTER - SECTION - PAGEBLOCK - TASK,
so the level to find the TASK is now changed and the query will not
find it anymore).

Second,

Even if I ask to display only the, for example, 3 first LEVELs, am I
right when I say the query will process the complete tree structure
anyway before returning me only the rows I'm interested in ?


Let's try to reverse the task. If name column is indexed, you can
use something like this:

select id, name
from Mytable a
where name = 'XYZ'
and exists (select 1
from Mytable b
where id = &1
connect by id = prior parent_id
start with name = 'XYZ' and b.id = a.id)

Subquery should represent reverse tree starting from current line of
the main query.

Bernard Drolet

Jul 19 '05 #6
VC
Hello Bernard,

If I understand correctly what you want to do, then it's really simple:

SELECT id, name
FROM Mytable
WHERE name = 'XYZ'
STARTING WITH id = &1
CONNECT BY id = PRIOR id AND PRIOR name <> 'XYZ';
Rgds.
"Bernard Drolet" <le****@yahoo.ca> wrote in message
news:ee**************************@posting.google.c om...
sy******@yahoo.com wrote in message

news:<a1**************************@posting.google. com>...

Hint: there is a LEVEL pseudo function.

Sybrand Bakker
Senior Oracle DBA


I have two concerns with LEVEL:

First,

I do not know in advance at what level the specific row is ! The table
contains the structure and content of complex SGML documents; the SGML
structure (DTD) can be different from one document to the other and
can even change over time

(for example, I can have a document with
PUBLICATION - CHAPTER - SECTION - TASK
structure today, but in future, the person in charge of the
publication can decide to add a layer, as in
PUBLICATION - CHAPTER - SECTION - PAGEBLOCK - TASK,
so the level to find the TASK is now changed and the query will not
find it anymore).

Second,

Even if I ask to display only the, for example, 3 first LEVELs, am I
right when I say the query will process the complete tree structure
anyway before returning me only the rows I'm interested in ?

Bernard Drolet

Jul 19 '05 #7
I found my solution. To stop connecting after I found my required
levels of rows, I added the following condition in my query (I'm
looking down to a row named XYZ, and I do not want to go further):

SELECT
id
FROM
MyTable
WHERE
name = 'XYZ'
START WITH id = 1
CONNECT BY parentid = PRIOR id AND
PRIOR name != 'XYZ' -- THIS SOLVES THE PROBLEM !
So it will never accept to go further than my level with name 'XYZ'

I must admit I was not aware the CONNECT BY could be used with a
condition more complex than just the tree structure. I shall read
better the documentation next time.
Thank you all
Bernard Drolet
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gary | last post by:
Hi, guys! Some of my applications are sharing same SQL login/password to connect to a database called "MyDB" on server "MyServer" . The password is encrypted and stored in registry or some...
2
by: Dom | last post by:
I need to run a query based on a query in Access. The second query has a number of conditions which all work well, but there is one more contition I need to set to make it run properly. the...
2
by: SJM | last post by:
In some VBA code I am building an SQL string to append a new record to a table. The string involves 40 fields. What I am finding is that it works with 39 fields but when I add 40 it fails with the...
5
by: Shane | last post by:
I wonder if someone has any ideas about the following. I am currently producing some reports for a manufacturing company who work with metal. A finished part can contain multiple sub-parts to...
3
by: deko | last post by:
Is there any way to limit the number of records loaded into a ListBox? I looked at qdf.MaxRecords (to apply to the query that is the RowSource of the ListBox) but that only applies to ODBC data...
4
by: Bill | last post by:
Hi, I would be grateful if someone could clarify my rather confused ideas of the 10 connection limit on XP/2000 when its being used as a server. (I realise that XP is really a client op sys with...
0
by: teju | last post by:
Hi all, I am trying to populate tree view from the database. Till two levels i can populate it fine but when it reaches third level it doesn't expand. Below is the code, it has been taken from the...
7
by: Bernard Drolet | last post by:
Hi, I have a table containing many millions of rows. This table has a tree stucture, with the following columns id name parent_id I need to go through the tree, starting from a specific Id,...
0
by: federico | last post by:
Hello, is there a way to limit the time a "ManagementScope.Connect" execution takes to end before it times out by itself? I tried using ConnectionOptions.TimeOut property to no avail. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.