조회 쿼리입니다
select categoryid,name, depth from rule_category
where categoryid in ( select distinct categoryid
from rule_document
where locationcode = 'BBAA'
and name LIKE '%의사소통%'
and ref_historyid <> 0
and isdelete = 'n' )
union all
select categoryid,name, depth from rule_category
where categoryid in (select distinct decode(sign(10-length(categoryid)), -1, substr(categoryid, 1, 10), '')
from rule_document
where locationcode = 'BBAA'
and name LIKE '%의사소통%'
and ref_historyid <> 0
and isdelete = 'n')
union all
select categoryid,name, depth from rule_category
where categoryid in (select distinct decode(sign(8-length(categoryid)), -1, substr(categoryid, 1, 8), '')
from rule_document
where locationcode = 'BBAA'
and name LIKE '%의사소통%'
and ref_historyid <> 0
and isdelete = 'n')
union all
select categoryid,name, depth from rule_category
where categoryid in (select distinct decode(sign(6-length(categoryid)), -1, substr(categoryid, 1, 6), '')
from rule_document
where locationcode = 'BBAA'
and name LIKE '%의사소통%'
and ref_historyid <> 0
and isdelete = 'n')
union all
select categoryid,name, depth from rule_category
where categoryid in (select distinct decode(sign(4-length(categoryid)), -1, substr(categoryid, 1, 4), '')
from rule_document
where locationcode = 'BBAA'
and name LIKE '%의사소통%'
and ref_historyid <> 0
and isdelete = 'n')
union all
select categoryid,name, depth from rule_category
where categoryid in (select distinct decode(sign(2-length(categoryid)), -1, substr(categoryid, 1, 2), '')
from rule_document
where locationcode = 'BBAA'
and name LIKE '%의사소통%'
and ref_historyid <> 0
and isdelete = 'n');
원본 쿼리 자체도 비효율이 많네요.
같은 테이블을 너무 여러번 반복해서 사용하고 있네요.
한번만 사용하도록 개선이 필요해 보입니다.
WITH rule_document AS ( SELECT 'BBAA' locationcode, 1 ref_historyid, 'n' isdelete, '의사소통' name, '22010702' categoryid FROM dual UNION ALL SELECT 'BBAA', 1, 'n', '의사소통', '220401' FROM dual UNION ALL SELECT 'BBAA', 1, 'n', '의사소통', '220212' FROM dual ) , rule_category AS ( SELECT '22' categoryid, '업무표준' name, 1 depth FROM dual UNION ALL SELECT '2204' , '제품' , 2 FROM dual UNION ALL SELECT '2202' , '공통2' , 2 FROM dual UNION ALL SELECT '2201' , '공통1' , 2 FROM dual UNION ALL SELECT '220107' , '안전환경' , 3 FROM dual UNION ALL SELECT '220401' , '공통' , 3 FROM dual UNION ALL SELECT '220212' , '안전' , 3 FROM dual UNION ALL SELECT '22010702', '(O)안전환경(구)', 4 FROM dual ) SELECT SUBSTR(SYS_CONNECT_BY_PATH(name, '>'), 2) x FROM (SELECT DISTINCT b.categoryid, b.name, b.depth FROM rule_document a , rule_category b WHERE a.locationcode = 'BBAA' AND a.name LIKE '%의사소통%' AND a.ref_historyid <> 0 AND a.isdelete = 'n' -- AND INSTR(a.categoryid, b.categoryid) > 0 -- 조건 수정 AND INSTR(a.categoryid, b.categoryid) = 1 ) START WITH depth = 1 CONNECT BY categoryid LIKE PRIOR categoryid || '__' ORDER SIBLINGS BY categoryid ;