Oracle hierarchical query - out of control?

I have a hierarchical query in Oracle 10 SQL that worked. However, I deleted the materialized view on which it was based, and now I can’t get it to exit normally, even leaving that opinion at all.

The initial request looked like this:

select oh.name, oh.description
  , sys_connect_by_path(groupname, ':') "Groups"
  , (select count(*) 
      from ml.lastobsmv 
      where lastobsmv.hdid = oh.hdid) as obscount
from ml.obshead oh
  join ml.hiergrps hg on oh.groupid = hg.groupid
    connect by prior hg.groupid = hg.parentid

I assume it still works, but without lastobsmv submission, I cannot test it.

If I cut it to

select oh.name, oh.description
from  ml.obshead oh
  join ml.hiergrps hg on oh.groupid = hg.groupid

it still works returning 41K records. However, when I use the connect by clause, it gets out of control by returning millions of records (usually I need to cancel it before I get the exact amount).

select oh.name, oh.description
  , sys_connect_by_path(groupname, ':') "Groups"
from ml.obshead oh
  join ml.hiergrps hg on oh.groupid = hg.groupid
    connect by prior hg.groupid = hg.parentid

Am I missing something really egregious here, or am I not understanding how this should work? Thank.


Vadim

, . ,

Obshead:

# CYCLE DAYS, number of days in menstrual cycle, 100

HierGrps:

100, 50, Gynecology
50, 10, Tx
10, 0, Basic

# CYCLE DAYS, number of days in menstrual cycle, :Basic:Tx:Gynecology

( , obs- , ).

+3
4
select
    oh.name,
    oh.description,
    hg."Groups"
from
    obshead oh
    join ( select
               groupid,
               sys_connect_by_path( groupname, ':' ) "Groups"
           from
               hiergrps
           start with
               parentid = 0
           connect by
               prior groupid = parentid
         ) hg
      on oh.groupid = hg.groupid

, :

create table obshead
( name varchar2(30)
, description varchar2(30)
, groupid number(3)
);
insert into obshead ( name, description, groupid )
     select 'Name One', 'Description One', 100 from dual union all
     select 'Name Two', 'Description Two', 200 from dual
;

create table hiergrps
( groupid number(3)
, parentid number(3)
, groupname varchar2(30)
);
insert into hiergrps ( groupid, parentid, groupname )
     select 100, 50, 'Gynecology' from dual union all
     select  50, 10, 'Tx'         from dual union all
     select  10,  0, 'Basic'      from dual
;
+3

, , , , ...

Gynecology:Tx:Basic ( ).

SELECT
  oh.name,
  oh.description,
  ( SELECT SYS_CONNECT_BY_PATH(groupname, ':')
    FROM hiergrps hg
    WHERE CONNECT_BY_ISLEAF = 1
      START WITH hg.groupid = oh.groupid
      CONNECT BY PRIOR hg.parentid = hg.groupid
  ) "groups"
FROM obshead oh

prior hg.groupid = hg.parentid, prior hg.parentid = hg.groupid?
, , groupid=100, parentid=50, groupid=50 parentid=10?

+2

, JOIN , . , :

SQL> SELECT oh.NAME, oh.description, 
  2         MAX(groups) keep(dense_rank LAST ORDER BY lvl) groups
  3    FROM obshead oh
  4    JOIN (SELECT sys_connect_by_path(groupname, ':') Groups, 
  5                 hg.groupid, hg.parentid, LEVEL lvl
  6            FROM hiergrps hg
  7          CONNECT BY PRIOR hg.groupid = hg.parentid) hg 
  8      ON oh.groupid = hg.groupid
  9   GROUP BY oh.NAME, oh.description;

NAME         DESCRIPTION                       GROUPS
------------ --------------------------------- ---------------------------
# CYCLE DAYS number of days in menstrual cycle :Basic:Tx:Gynecology
+2

, , :

create table obshead
(
cycledays number,
numdaysincycle number,
groupid number
);

create table hiergrps 
(
groupid number,
parent number,
groupname varchar2(40)
);


insert into obshead select 100 cycledays, 30 numdaysincycle, 100 groupid from dual;
insert into hiergrps select 100 groupid, 50 parent, 'Gyncecology' groupname from dual;
insert into hiergrps select 50 groupid, 10 parent, 'Tx' groupname from dual;
insert into hiergrps select 10 groupid, 0 parent, 'Basic' groupname from dual;

select cycledays,
       numdaysincycle,
       groups
  from (select groupid,
               parent,
               sys_connect_by_path(groupname, ':') groups
          from hiergrps hg 
        start with parent = 0
        connect by prior hg.groupid = hg.parent
       ) hg,
       obshead obs
 where obs.groupid = hg.groupid;

:

100 30  :Basic:Tx:Gyncecology

: , Vadim parentid = 0

+1

Source: https://habr.com/ru/post/1730093/


All Articles