Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, HongHui.

Asked: September 15, 2017 - 11:07 am UTC

Last updated: September 19, 2017 - 1:49 am UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

SQL> select * from scott.t0909_1;

ID S E
---------- ---------- ----------
1 10 11
2 1 5
3 88 92

-----Scene 1
select * from scott.t0909_1 connect by rownum<=3;

ID S E
---------- ---------- ----------
1 10 11
1 10 11
1 10 11
2 1 5
3 88 92

-----Scene 2
select * from scott.t0909_1 where id=1 connect by rownum<=3;
ID S E
---------- ---------- ----------
1 10 11
1 10 11
1 10 11

-----Scene 3
select * from scott.t0909_1 where id=2 connect by rownum<=3;
*
ERROR at line 1:
ORA-30009: Not enough memory for CONNECT BY operation

-----Scene 4
select * from scott.t0909_1 where id=3 connect by rownum<=3
*
ERROR at line 1:
ORA-30009: Not enough memory for CONNECT BY operation

-----Scene 5
select * from scott.t0909_1 where id in (2,3) connect by rownum<=3;
*
ERROR at line 1:
ORA-30009: Not enough memory for CONNECT BY operation


-----Scene 6
select * from scott.t0909_1 where id in (1,3) connect by rownum<=3;

ID S E
---------- ---------- ----------
1 10 11
1 10 11
1 10 11
3 88 92

-----Scene 7
select * from scott.t0909_1 where id in (1,2) connect by rownum<=3
ID S E
---------- ---------- ----------
1 10 11
1 10 11
1 10 11
2 1 5

It seems that if the first row which has "id=1" is excluded from the query result there will be ORA-30009. Why ?

and Connor said...

In a CONNECT BY query, the hierarchy is built first, and then WHERE predicate is applied. For example - I start with someone in deptno=20

SQL> select level, empno, mgr, deptno
  2  from scott.emp
  3  start with empno = 7566
  4  connect by prior mgr = empno;

     LEVEL      EMPNO        MGR     DEPTNO
---------- ---------- ---------- ----------
         1       7566       7839         20
         2       7839                    10


Now I add a WHERE predicate

SQL> select level, empno, mgr, deptno
  2  from scott.emp
  3  where deptno = 10
  4  start with empno = 7566
  5  connect by prior mgr = empno;

     LEVEL      EMPNO        MGR     DEPTNO
---------- ---------- ---------- ----------
         2       7839                    10



Now if deptno=10 had been applied *first*, then we would have got no rows because my starting row was deptno 20, which doesnt match the WHERE predicate so we would have nothing to walk the hierarchy with. So we can apply that to your cases:


select * from scott.t0909_1 where id=1 connect by rownum<=3;

- start with the first row we find *based on the hierarchy rule* which is "hey, match anything"
- it matches the where clause, so rownum gets bumped up to 1
- our hierarchy has no "real" condition so we loop around again
- we get the first row (id=1)
- it matches the where clause, so rownum gets bumped up to 2
and so on, and rownum will hit 3 and we're done.

Now look at this one:

select * from scott.t0909_1 where id=2 connect by rownum<=3;

- start with the first row we find *based on the hierarchy rule* which is "hey, match anything"
- it doesn't match the where clause, so rownum stays zero
- our hierarchy has no "real" condition so we loop around again
- we get the first row (id=1)
- it doesn't match the where clause, so rownum stays zero
- our hierarchy has no "real" condition so we loop around again
- we get the first row (id=1)
- it doesn't match the where clause, so rownum stays zero
- our hierarchy has no "real" condition so we loop around again
- we get the first row (id=1)
etc
etc
etc

eventually we get so deep in the hierarchy we run out of memory

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Which memory area will be run out?

oliseh, September 18, 2017 - 1:27 pm UTC

The PGA or share pool , which one
Connor McDonald
September 19, 2017 - 1:49 am UTC

Typically PGA

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library