본문 바로가기

Paul Work/Sql

INNER JOIN을 이용한 SQL 예제

sg_menu_acl  : 581
SG016 : 52
SG015 : 12
위는 각 조건에 의한 데이터 껀수이다.
처음 생각으로는 예제1) 번이 2번보다 늦을꺼라고 생각을 했는데 생각외로 1번의 조건이
0.15 ~ 0.16sec를 나타냈고
예제2)번은 0.16 ~ 0.32의 시간을 나타냈다.
특히 예제 1)번에서 ORDER BY 1, 3을 빼게 되면 0.32의 시간을 나타내고 있다.

계산후 최종 값에서 처리를 하는것보다 더 빠르다니... 좀더 공부를 해보아야 할듯... -_-??

예제 1)
======================================================================================================
SELECT role_cd,role_name,up_cd, cd,menu_name,group_cd,cd_name,level_no,sort_no
  FROM (
              SELECT a.role_cd, c.role_name, a.menu_cd, b.menu_name
                 FROM sg_menu_acl a,
                (SELECT cd AS menu_cd, cd_name AS menu_name FROM sg_cd WHERE group_cd = 'SG016') b,
                (SELECT cd AS role_cd, cd_name AS role_name FROM sg_cd WHERE group_cd = 'SG015') c
               WHERE a.menu_cd = b.menu_cd
                    AND a.role_cd = c.role_cd
                    AND a.use_flag = 'Y'
               ORDER BY 1, 3
             )d INNER JOIN (
               SELECT cd,group_cd,cd_name,level_no,sort_no,up_cd
                  FROM SG_CD
                WHERE group_cd='SG021'
            )e
      ON e.cd=d.menu_cd
WHERE role_cd='AD'
ORDER BY level_no,sort_no

예제 2)
======================================================================================================
 SELECT role_cd,
              (SELECT cd_name FROM sg_cd B WHERE group_cd = 'SG015' AND B.cd = role_cd ) role_name,
              up_cd, cd,
              (SELECT cd_name FROM sg_cd B WHERE group_cd = 'SG016' AND B.cd = E.cd) AS menu_name,
              group_cd, cd_name, level_no,sort_no
    FROM (
                   SELECT a.role_cd ,a.menu_cd
                     FROM sg_menu_acl a
                    WHERE a.use_flag = 'Y'
               )d,
               (
                    SELECT cd,group_cd,cd_name,level_no,sort_no,up_cd
                      FROM SG_CD
                     WHERE group_cd='SG021'
               )E
   WHERE E.cd=d.menu_cd
        AND role_cd='AD'
       order by level_no,sort_no