Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Query Issue with select level from dual

NoraMar 3 2009 — edited Mar 4 2009
Hi,
I have a question regarding this query. The problem seems that when selecting level from dual while including another table the rows returned seem to increase exponentially.
I can add distinct and get the correct number of rows but am worried that this will cause a possible performance issue. Am I using the level option wrong?

I have included details below.

There are 4 rows in tbl_incidents

When I run the following queries I get rows returned based on the total number of rows

select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
where incident_id = 6
connect by level <= 1;

returns 1 row



select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
where incident_id = 6
connect by level <= 2;

returns 5 rows


select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
connect by level <= 3 and incident_id = 6;

returns 21 rows



select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
connect by level <= 4 and incident_id = 6;

returns 85 rows


select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
connect by level <= 5 and incident_id = 6;

returns 341 rows


So with
r being the number of rows in tbl_incidents and
l being the number used in the connect by for level and
q being the number of rows returned by the query
it appears that

q(l) = r * q(l-1) + 1


level 2: 4 * 1 + 1 = 5
level 3: 4 * 5 + 1 = 21
level 4: 4 * 21 + 1 = 85
level 5: 4 * 85 + 1 = 341


Thanks much,
Nora
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 1 2009
Added on Mar 3 2009
7 comments
975 views