Oracle Hierarchy – Understanding Connect By and Row Multiplication

hierarchyoracle

I know (on high abstract level) how hierarchies in Oracle works, but this one I do not understand.
When issued following query:

select rownum-1 as num from dual

I've got one row as result.
The following query:

select rownum-1 as num from dual connect by level<=1000

is returning 1000 rows.
What is really happening in background of this query? How Oracle engine is multiplying rows when tries to resolve hierarchy.

Best Answer

If you look at the standard SQL way of constructing a recursive query, you would see something like this:

with t (num) as (
  select rownum-1 num from dual 
  union all 
  select num+1 from t where num+1 < 10
) select * from t;

SQLFiddle: http://sqlfiddle.com/#!4/9eecb/4371/0

As you can see, it starts with selecting one row from the DUAL table, then recursively applying a UNION ALL to that single row while the limiting condition remains true. The CONNECT BY construct is just hiding this SQL plumbing from you.

If you click on the Execution Plan link in the fiddle above, you'll see that.