๐Ÿ“– 16์žฅ. Resource Manager

๐Ÿ’ก ์˜ค๋ผํด ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค. ์ด๊ฒƒ์€ ํŠน์ • ์„ธ์…˜์ด database์˜ ์ž์›์„ ๋ฌดํ•œํžˆ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ œํ•œ์„ ๊ฑฐ๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

โœ”๏ธ ์ž์›(Resource) ์‚ฌ์šฉ ์ œํ•œ์„ ๊ฑฐ๋Š” ์˜ค๋ผํด ๊ธฐ๋Šฅ 2๊ฐ€์ง€

  • ํ”„๋กœํŒŒ์ผ (Profile)
    • ์ž์›๊ด€๋ฆฌ
    • ํŒจ์Šค์›Œ๋“œ
  • ๋ฆฌ์†Œ์Šค ๊ด€๋ฆฌ (Resource Manager)
    • ํ”„๋กœํŒŒ์ผ ๋ณด๋‹ค ๋” ๋งŽ์€ ์ž์› ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ œํ•œ์„ ๊ฑธ ์ˆ˜ ์žˆ๋‹ค.
      • ์–ด๋– ํ•œ ์ œํ•œ์„ ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?
      1. ์•…์„ฑ SQL์ด ์‹คํ–‰ ์•ˆ๋˜๋„๋ก ์ œํ•œ
      2. lock waiting ํ˜„์ƒ์— ๋Œ€ํ•œ ์ œํ•œ
      3. ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ์— ๋Œ€ํ•œ ์ œํ•œ (ex: select /*_ parallel 4 */ ...) โžก๏ธ ๋ณธ์ธ์€ ๋น ๋ฅด์ง€๋งŒ ๋‹ค๋ฅธ์‚ฌ๋žŒ๋“ค์€ ๋Š๋ ค์ง„๋‹ค! (๋ณ‘๋ ฌ๋„ : cpu_count * 2)
      4. Undo ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ œํ•œ - ๊ณผ๋„ํ•œ DML ์ž‘์—…์„ ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ!
        (ex: ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ sales ํ…Œ์ด๋ธ” ๊ฐ™์€ ํฐ ํ…Œ์ด๋ธ”์„ update ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด undo tablespace ํ’€๋‚œ๋‹ค.)
        update sales -- 2์–ต๊ฑด ์ด์ƒ ์žˆ๋Š” ํ…Œ์ด๋ธ”์ด๋‹ค.
          set amount_sold=9000; 
        -- ์œ„์ฒ˜๋Ÿผ ํ•ด์„œ ์•ˆ๋œ๋‹ค๊ณ  ํ•˜๋ฉด where์ ˆ ์จ์„œ ํ•˜๋‚˜์”ฉ ํ•˜์‹œ์˜ค ๋ผ๊ณ  ๋งํ•ด์ค€๋‹ค.  
      5. Active session (waiting ํ•˜๋Š” ์„ธ์…˜) ๋“ค์— ์ œํ•œ - ์ง€๊ธˆ ์ˆ˜ํ–‰ํ•˜๋ ค๋Š” SQL์€ ์•…์„ฑ SQL์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ์งœ์„ธ์š” ๋ผ๋Š” ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ํ•˜๋ฉด์„œ ์•„์˜ˆ ์•…์„ฑSQL์€ ๋Œ์ง€ ๋ชปํ•˜๊ฒŒ ํ•œ๋‹ค.
      6. Idle ์‹œ๊ฐ„์— ๋Œ€ํ•œ ์ œํ•œ (db์ ‘์†ํ•ด๋†“๊ณ  ์•„๋ฌด๊ฒƒ๋„ ์•ˆํ•˜๋Š” ์„ธ์…˜๋“ค์„ ๋Š์–ด๋ฒ„๋ฆผ)
      7. execution ์‹œ๊ฐ„์— ๋Œ€ํ•œ ์ œํ•œ

โœ… 4๋ฒˆ๊นŒ์ง€๋Š” ํ™•์‹คํ•˜๊ฒŒ ํ•ด๋†“์•„์•ผ ์ผ์ด ํž˜๋“ค์ง€ ์•Š๋‹ค..!
ex ) ํ˜„๋Œ€์นด๋“œ dba์˜ ์‚ฌ์šฉ ์˜ˆ

$ sh daytime.sh

์ œํ•œํ•  ๋ณ‘๋ ฌ๋„๋Š” ? 4
์ œํ•œํ•  undo ์‚ฌ์šฉ๋Ÿ‰์€? 50m
์ œํ•œํ•  execution time(์ดˆ)?
์ œํ•œํ•  idle time(์ดˆ)? 600
์ œํ•œํ•  blocking idle time(์ดˆ)? 10

๋ฝ waiting์ด 10์ดˆ ์ด์ƒ ๋˜๋ฉด ์ž๋™์œผ๋กœ lock holder๋ฅผ kill ์‹œํ‚ด.

โœ๏ธ resource manager ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

(p16-4)

1. resource plan : ๋ฆฌ์†Œ์Šค ํ• ๋‹น์— ๋Œ€ํ•œ ์ฒญ์‚ฌ์ง„(ํฐ ๊ณ„ํš)
2. resource consumer group : ๋ฆฌ์†Œ์Šค์˜ ์š”๊ตฌ์‚ฌํ•ญ์ด ์œ ์‚ฌํ•œ ๊ทธ๋ฃน

dba ๊ทธ๋ฃน, ๊ฐœ๋ฐœ์ž ๊ทธ๋ฃน, ์™ธ๋ถ€ ์—”์ง€๋‹ˆ์–ด ๊ทธ๋ฃน, ์šด์˜์ž ๊ทธ๋ฃน

3. resource plan directive : ๋ฆฌ์†Œ์Šค๋ฅผ ๋ถ„๋ฐฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์ •

  • cpu
  • locl waiting time
  • undo
  • degree (๋ณ‘๋ ฌ๋„)
  • active session์˜ ๊ฐฏ์ˆ˜
  • idle time

โœ”๏ธ resourec manager ๊ตฌํ˜„

โœ”๏ธ SYSTEM user๋กœ ์ ‘์†ํ•˜์—ฌ consumer group ONLINE_USERS ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค

SQL> conn system/oracle
SYSTEM> exec dbms_resource_manager.create_pending_area();
--  ๋ฆฌ์†Œ์Šค ๋ฉ”๋‹ˆ์ €๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ  ์˜์—ญ์„ ์„ค์ •ํ•˜๊ฒ ๋‹ค. 

โœ”๏ธ ์ปจ์Šˆ๋จธ ๊ทธ๋ฃน ์ƒ์„ฑ (ONLINE_USERS) / SYS_GROUP ๊ณผ OTHER_GROUP๋Š” ํ•ญ์ƒ ๋””ํดํŠธ๋กœ ์žˆ๋‹ค.

SYSTEM> exec dbms_resource_manager.create_consumer_group('ONLINE_USERS','Users for Online');

-- SYS_GROUP ๊ณผ OTHER_GROUP ์€ ์ด๋ฏธ ๋งŒ๋“ค์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ONLINE_USERS ๋งŒ ์ƒ์„ฑํ•œ๋‹ค.
    โ†‘               โ†‘
 dba๊ถŒํ•œ ์œ ์ €๋“ค    ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ œํ•œ์ด ๋†’์€ ์œ ์ €๋“ค

๐Ÿ’ก db ์— ๋งŒ๋“ค์–ด์ ธ์žˆ๋Š” Consumer ๊ทธ๋ฃนํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด

 select consumer_group 
    from dba_rsrc_consumer_groups
    where consumer_group like '%ONLINE%';

CONSUMER_GROUP
------------------------------
ONLINE_USERS

โœ”๏ธ resource plan DAYTIME ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑํ•œ ํ›„ view๋ฅผ ํ†ตํ•ด ํ™•์ธํ•œ๋‹ค.

A. SYS_GROUP    : CPU_P1=100

B. ONLINE_USERS : CPU_P2=70, MAX_IDEL_BLOCKER_TIME=30
-- MAX_IDEL_BLOCKER_TIME  :  lock  wating time ์„ 30์ดˆ๋กœ ์ œํ•œํ•˜๊ฒ ๋‹ค.

C. OTHER_GROUPS : CPU_P2=30, PARALLEL_DEGREE_P1=4 
-- PARALLEL_DEGREE_P1 : ๋ณ‘๋ ฌ๋„๋ฅผ 4๋กœ ์ œํ•œํ•˜๊ฒ ๋‹ค   

select  /*+  parallel(emp, 8)  */   count(*)
  from  emp;  

โœ”๏ธ ๋ฆฌ์†Œ์Šค ํ”Œ๋žœ ์ƒ์„ฑ (daytime)

SQL> 
begin
 dbms_resource_manager.create_plan('DAYTIME','Plan for day');
end;
/

PL/SQL procedure successfully completed.

โœ”๏ธ ์œ„์—์„œ ์ƒ์„ฑํ•œ daytime plan ์ด ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์‹œ์˜ค !

select plan
  from dba_rsrc_plans; 

โœ”๏ธ ๋ฆฌ์†Œ์Šค ๋””๋ ‰ํ‹ฐ๋ธŒ ์ƒ์„ฑ (๋””๋ ‰ํ‹ฐ๋ธŒ : ๋ถ„๋ฐฐ๋ฐฉ๋ฒ•)
1. ๊ธฐ์กด์— ์ด๋ฏธ ์žˆ๋Š” SYS_GROUP์€ CPU๋ฅผ 1์ˆœ์œ„๋กœ 100% ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์‹œ์–ด ์ƒ์„ฑ

SQL> 
begin
 dbms_resource_manager.create_plan_directive('DAYTIME', 'SYS_GROUP','1st Group Set', cpu_p1=>100);
end;
/

PL/SQL procedure successfully completed.
  1. online_users ๊ทธ๋ฃน์€ lock blocking time์„ 30์ดˆ๋กœ ์ง€์ •, cpu ์‚ฌ์šฉ ์šฐ์„ ์ˆœ์œ„ 2๋ฒˆ์งธ ์ด๊ณ  70%๋งŒ ์‚ฌ์šฉ
begin
 dbms_resource_manager.create_plan_directive('DAYTIME', 'ONLINE_USERS','2nd Group set', 
cpu_p2=>70,max_idle_blocker_time=>30);
end;
/
  1. other_groups ๋Š” ๋ณ‘๋ ฌ๋„๋ฅผ 4๋กœ ์ง€์ •, cpu ์‚ฌ์šฉ์œจ ์šฐ์„ ์ˆœ์œ„๊ฐ€ 2๋ฒˆ์งธ์ด๊ณ  30%๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •
begin
 dbms_resource_manager.create_plan_directive('DAYTIME', 'OTHER_GROUPS','3rd Group', 
cpu_p2=>30,parallel_degree_limit_p1=>4);
end;
/

โžก๏ธ 2, 3๋ฒˆ cpu_p2 (cpu ์‚ฌ์šฉ)์ด ๋‘๊ฐœ๊ฐ€ ํ•ฉ์ณ์„œ 100% ์ด ๋˜์–ด์•ผ ๊ฒ€์ฆ์—์„œ ์—๋Ÿฌ๋‚˜์ง€์•Š๋Š”๋‹ค.

โœ”๏ธ ๊ฒ€์ฆ ์ž‘์—…

SYSTEM> 
exec dbms_resource_manager.validate_pending_area();
PL/SQL procedure successfully completed.

โœ”๏ธ ๊ตฌํ˜„

SYSTEM> 
exec dbms_resource_manager.submit_pending_area();
PL/SQL procedure successfully completed.
SYSTEM> alter system set resource_manager_plan=daytime;

๐Ÿ’ก๋‚ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ–ˆ๋Š”์ง€ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•

SQL> select plan, group_or_subplan, cpu_p1, cpu_p2,  
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;


โœ”๏ธ SCOTT user๋ฅผ consumer group ONLINE_USERS๋กœ ์ง€์ •ํ•˜๊ณ  default consumer group์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.

SQL> exec dbms_resource_manager_privs.grant_switch_consumer_group('SCOTT', 'ONLINE_USERS', true);
PL/SQL procedure successfully completed.

-- ์ง€๊ธˆ ๋‹น์žฅ consumer group์ด ONLINE_USERS๊ฐ€ ๋˜๋Š” ๊ฒƒ.
SQL> exec dbms_resource_manager.set_initial_consumer_group('SCOTT', 'ONLINE_USERS');
PL/SQL procedure successfully completed.   

online_users ๊ทธ๋ฃน์€ lock blocking time์„ 30์ดˆ๋กœ ์ง€์ •, cpu ์‚ฌ์šฉ ์šฐ์„ ์ˆœ์œ„ 2๋ฒˆ์งธ ์ด๊ณ  70%๋งŒ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ๋กœ ์œ„์—์„œ ์„ค์ •ํ–ˆ๋‹ค.

๋ฌธ์ œ scott ํ„ฐ๋ฏธ๋„ ์ฐฝ 2๊ฐœ๋ฅผ ์—ด๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด update๋ฅผ ์ˆ˜ํ–‰ํ•ด์„œ lock์ด 30์ดˆ ํ›„์— ํ’€๋ฆฌ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

SYS> alter system set resource_manager_plan=daytime;
          #scott ํ„ฐ๋ฏธ๋„ ์ฐฝ1                       #scott ํ„ฐ๋ฏธ๋„ ์ฐฝ2
           
           update emp
            set sal = 0
            where ename ='ALLEN';                update emp
                                                  set sal = 6000
                                                  where ename ='ALLEN';


lock์„ ๋จผ์ € ์žก์„ ์„ธ์…˜์„ ์ฃฝ์˜€๋‹ค.

โœ๏ธ resourec manager ์„ค์ • ๋ณ€๊ฒฝ

๋ฌธ์ œ ์œ„์˜ max_idle_bloker_time์„ 60์ดˆ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_max_idle_blocker_time=>60);
end;
/

SQL> select plan, group_or_subplan, cpu_p1, cpu_p2,  
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;

SYSTEM> exec dbms_resource_manager.submit_pending_area();

๋ฌธ์ œ max_idle_time์„ ์ˆ˜์ •ํ•ด์„œ scott์œ ์ €๋กœ ์ ‘์†ํ•ด์„œ 10์ดˆ๋™์•ˆ ์•„๋ฌด๋Ÿฐ ์ž‘์—…์„ ํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ์ ‘์†์ด ๋Š๊ธฐ๋„๋ก ์„ค์ •

SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_max_idle_time=>10);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;


โžก๏ธ ๋ฐ”๋€๊ฒƒ์„ ํ™•์ธ

โžก๏ธ scott์— ์ ‘์†ํ•ด์„œ 10์ดˆ๋™์•ˆ ๊ฐ€๋งŒํžˆ ์žˆ์œผ๋‹ˆ ๋Š๊ฒจ์žˆ๋‹ค.

๋ฌธ์ œ ๊ทธ๋Ÿผ ๋‹ค์‹œ idle_time์˜ ์˜ํ–ฅ์„ ๋ฐ›๊ณ  ๊ณ„์† ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•˜๊ธฐ

SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> 
begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_max_idle_time=>-1);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;

๋ฌธ์ œ scott ์œ ์ €๊ฐ€ undo ์‚ฌ์šฉ๋Ÿ‰์„ ์ œํ•œํ•˜๊ฒŒ๋” ์„ค์ •
new_undo_pool =>10

SYS> grant select on sales to scott;
SCOTT> create table sales500
       as
        select * from sh.sales;
SCOTT> delete from sales500;        
SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> 
begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_undo_pool=>10);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select undo_pool, plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;

์ ์‹ฌ์‹œ๊ฐ„ ๋ฌธ์ œ ๋‹ค์‹œ undo๋ฅผ ์ œํ•œ์—†์ด ์“ธ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •!

SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> 
begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_undo_pool=>-1);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select undo_pool, plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;


โœ๏ธ Resource Manager ๋กœ cpu ์‚ฌ์šฉ ์ œํ•œํ•˜๊ธฐ

select * from v$rsrc_consumer_group;
OTHER_GROUPS -- sys, system user (cpu_p1, 100%)
SYS_GROUP    -- scott (cpu_p2, 70%)
ONLINE_USERS -- hr, sh user (cpu_p2, 30%)

select name, consumed_cpu_time, cpu_wait_time
  from v$rsrc_consumer_group;

SYSTEM @ orcl3 > save cpu.sql
Created file cpu.sql

โ“ consumed_cpu_time : ์†Œ๋น„ํ•œ cpu ์‹œ๊ฐ„
โ“ cpu_wait_time : cpu๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋Œ€๊ธฐํ•œ ์‹œ๊ฐ„

๋ฌธ์ œ scott ์œ ์ €๋กœ ์ ‘์†ํ•˜์—ฌ ์•…์„ฑSQL์„ ์ˆ˜ํ–‰ํ•ด์„œ CPU ์‚ฌ์šฉ์œจ์„ ๋†’์ธ ํ›„ cpu.sql์„ ์ˆ˜ํ–‰ํ•˜๊ณ  online_users ๊ทธ๋ฃน์˜ cpu ์‚ฌ์šฉ์œจ์ด ์˜ฌ๋ผ๊ฐ€๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

SCOTT @ orcl3 > select count(*)from sh.sales, sh.sales;


โžก๏ธ scott ํ˜ผ์ž ๋‹ค ์“ฐ๋Š”๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ sys, system ์œ ์ €๊ฐ€ ์‚ฌ์šฉ์ค‘์ด์—ˆ๋‹ค๋ฉด scott์€ ๋‚จ์•„์žˆ๋Š” ๊ฒƒ ๊ฐ€์ง€๊ณ  ์“ฐ๋Š”๊ฒƒ์ž„

๋ฌธ์ œ system ์œ ์ €์—์„œ ์•„๋ž˜์˜ ์•…์„ฑ SQL์„ ์ˆ˜ํ–‰ํ•œํ›„ scott ์œ ์ €์—์„œ ๋˜‘๊ฐ™์ด ์•„๋ž˜์˜ ์•…์„ฑSQL์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„ ์ฐฝ์„ ์—ด์–ด์„œ @cpu๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋ˆ„๊ฐ€ ๋” cpu๋ฅผ ๋งŽ์ด ์“ฐ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž!

SCOTT @ orcl3 > select count(*)from sh.sales, sh.sales;
SYS @ orcl3 > select count(*)from sh.sales, sh.sales;

โžก๏ธ ์ƒ๊ฐ๊ณผ ๋‹ค๋ฅด๊ฒŒ scott์ด ๋” cpu๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.

โœ๏ธ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ฅผ ์ด์šฉํ•ด์„œ ๋ณ‘๋ ฌ๋„ ์ œํ•œํ•˜๊ธฐ

โ“ ๋ณ‘๋ ฌ๋กœ ์ž‘์—… ํ•œ๋‹ค๋Š” ๊ฒƒ์€?

select /* parallel(emp 4) */ ename, sal
 from emp;

  • ๋ณ‘๋ ฌ ์ž‘์—…์ด ์ง€์›๋˜๋ ค๋ฉด ์˜ค๋ผํด์„ ๋ฐ˜๋“œ์‹œ enterprise edition์œผ๋กœ ์„ค์น˜ํ•ด์•ผํ•œ๋‹ค.

8์ฝ”์–ด ~ 24์ฝ”์–ด, ์˜ค๋ผํด 1 ์ฝ”์–ต์”ฉ 5์–ต์”ฉ ๋ฐ›ใ„ด๋Š”๋‹ค.
24์ฝ”์–ด๋ฉด 120์–ต์„ ์ค˜์•ผ ์˜ค๋ผํด ์“ธ ์ˆ˜ ์žˆ๊ฑฐ, ๋งค๋…„ ๊ตฌ์ž…๊ฐ€๊ฒฉ์˜ 3%๋ฅผ ์œ ์ง€๋ณด์ˆ˜๋กœ ๋‚ด์•ผํ•œ๋‹ค.

1. ๋ณ‘๋ ฌ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋ณ‘๋ ฌ ํ”„๋กœ์„ธ์„œ๋“ค์ด ์ž˜ ๋– ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์‹œ์˜ค.

SCOTT> select /*+  parallel(s1,2) parallel(s2,2) */  count(*)
        from sh.sales  s1, sh.sales  s2;

2. sys user์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ‘๋ ฌ ํ”„๋กœ์„ธ์„œ๋“ค์ด ๋– ์žˆ๋Š”์ง€ ํ™•์ธ !

SYS> col program for a25
SYS> col process for a10

SYS> select process, program
      from v$session
      where program like '%(P0%';

3. scott user๊ฐ€ ๋ณ‘๋ ฌ๋„๋ฅผ ๋งˆ์Œ๊ป ๋ณ‘๋ ฌ๋„๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•˜๊ธฐ!

SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> 
begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_parallel_degree_limit_p1=>-1);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select undo_pool, plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2;
    

SCOTT>
select /*+  parallel(s1,6) parallel(s2,6) */  count(*)
        from sh.sales s1, sh.sales s2;  
-- SYSTEM user์—์„œ ํ™•์ธ
SYSTEM> select process, program
      from v$session
      where program like '%(P0%';

โœ๏ธ ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋ฅผ ์ด์šฉํ•ด์„œ ์˜ˆ์ƒ๋˜๋Š” SQL ์‹คํ–‰์‹œ๊ฐ„์ด ์ง€์ •๋œ ์‹œ๊ฐ„๋ณด๋‹ค ๋” ์˜ค๋ž˜ ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™์œผ๋ฉด ๋Š๋Š” ์‹ค์Šต

1. SYSTEM ์œ ์ €์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •

SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> 
begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_max_est_exec_time=>120);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select undo_pool, plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2; 


โžก๏ธ ๋‚ฎ์‹œ๊ฐ„์—๋Š” ์„œ๋ฒ„์— ๋ถ€ํ•˜๋ฅผ ์ฃผ๋Š” ๋ฌด๊ฑฐ์šด SQL์„ ์ˆ˜ํ–‰๋˜์ง€ ๋ชปํ•˜๊ฒŒ ๋ง‰๋Š”๊ฒƒ!

๋ฌธ์ œ ์œ„ ์˜ˆ์ƒ ์‹คํ–‰์‹œ๊ฐ„์„ 5๋ถ„์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

SYS @ orcl3 > conn system/oracle;
SYSTEM> exec dbms_resource_manager.create_pending_area();

SYSTEM> 
begin
         dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_max_est_exec_time=>600);
end;
/

SYSTEM> exec dbms_resource_manager.submit_pending_area();

SQL> select undo_pool, plan, group_or_subplan, cpu_p1, cpu_p2, max_idle_time, 
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='DAYTIME'
     order by 1,2; 

๋ฌธ์ œ ๋‹ค์‹œ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค ๋งค๋‹ˆ์ €๋กœ ์ œํ•œ๋œ ๊ธฐ๋Šฅ์„ ํ’€์–ด์ค๋‹ˆ๋‹ค.
: ๋ฐค์—๋Š” ์ œํ•œ๋œ ๊ธฐ๋Šฅ์„ ํ’€์–ด์ค„ ์ˆ˜ ์žˆ๋„๋ก ์•„๋ž˜ ๋ช…๋ น์œผ๋กœ ํ’€์–ด์ค€๋‹ค. ์‰˜์ด๋‚˜ dbms ์Šค์ผ€์ค„์„ ์ด์šฉํ•˜์—ฌ ๊ฑธ์–ด์ค€๋‹ค.

SYSTEM> alter system set resource_manager_plan='';

๋‹ค์‹œ ์•„์นจ์— ์—…๋ฌด์‹œ๊ฐ„์ด ๋˜๊ธฐ ์ „์— resource_manager_plan์„ daytime์œผ๋กœ ์ง€์ •ํ•œ๋‹ค. ๋ฐค์—๋Š” ๋ฐฐ์น˜ ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๋ฉด ํ•˜๋ฃจ ๊ฒฐ์‚ฐ์„ ํ•˜๊ฑฐ๋‚˜ ํ•˜๋Š” ์•„์ฃผ ๋ฌด๊ฑฐ์šด SQL ๋“ค์ด ๋งŽ์ด ์ž๋™์œผ๋กœ ๋•๋‹ˆ๋‹ค. (์•„์นจ์— ๊ฑธ๊ณ , ๋ฐค์—๋Š” ํ’€๊ณ  !)

crotab, dbms_job, dbms_scheduler ?

โžก๏ธ ์œ„ ๊ธฐ๋Šฅ์„ ์ž๋™์œผ๋กœ ํ•˜๋Š” ๋ฐฉ๋ฒ•
1. ๋ฆฌ๋ˆ…์Šค์˜ crotab์„ ์ด์šฉ
2. ์˜ค๋ผํด์˜ dbms_job ๋˜๋Š” dbms_scheduler๋ฅผ ์ด์šฉ

๋ฌธ์ œ resource manager์— ๋Œ€ํ•œ ์‚ฌ์šฉ์„ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋Š” ํ”„๋กœ์‹œ์ €๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ (์ต๋ช… PL/SQL ์ž‘์„ฑ)

SYSTEM> @resource.sql

์ œํ•œํ•  ๋ณ‘๋ ฌ๋„๋Š”?                new_paralle_degree_limit_p1
์ œํ•œํ•  undo ์‚ฌ์šฉ๋Ÿ‰์€?           new_undo_pool
์ œํ•œํ•  execution time(์ดˆ)?     new_max_est_exec_time
์ œํ•œํ•  idle time(์ดˆ)?          new_max_idle_time
์ œํ•œํ•  blocking idle time(์ดˆ)? new_max_idle_blocker_time
accept  p_degree  prompt '์ œํ•œํ•  ๋ณ‘๋ ฌ๋„๋Š” ?  ' 
accept  p_undo    prompt '์ œํ•œํ•  undo ์‚ฌ์šฉ๋Ÿ‰์€? '
accept  p_exec    prompt '์ œํ•œํ•  execution time(์ดˆ) ? '
accept  p_idle    prompt '์ œํ•œํ•  idle time(์ดˆ) ? '
accept  p_block   prompt '์ œํ•œํ•  blocking idle time(์ดˆ) ? '

exec dbms_resource_manager.create_pending_area();

begin
          dbms_resource_manager.update_plan_directive(
          plan=>'DAYTIME',
          group_or_subplan=>'ONLINE_USERS',
          new_parallel_degree_limit_p1 => &p_degree,
          new_undo_pool => &p_undo, 
          new_max_est_exec_time => &p_exec,
          new_max_idle_time => &p_idle,
               new_max_idle_blocker_time  =>  &p_block 
);
end;
/

exec dbms_resource_manager.submit_pending_area();

alter system  set  resource_manager_plan= 'daytime';

alter system set resource_manager_plan= ''; ๋กœ ๋„๊ธฐ

๋ฌธ์ œ nighttime์ด๋ผ๋Š” resouce_manager_plan์„ ์ƒ์„ฑํ•˜๊ธฐ ๊ทธ๋ฆฌ๊ณ  hr ๊ณ„์ •์—์„œ ์ด ํ”Œ๋žœ์„ ์ง€์ •ํ•˜์„ธ์š”

์ œํ•œํ•  ๋ณ‘๋ ฌ๋„๋Š”? 4                  new_paralle_degree_limit_p1
์ œํ•œํ•  undo ์‚ฌ์šฉ๋Ÿ‰์€? 500๋ฐ”์ดํŠธ      new_undo_pool
์ œํ•œํ•  execution time(์ดˆ)? 300     new_max_est_exec_time
์ œํ•œํ•  idle time(์ดˆ)? 10           new_max_idle_time
์ œํ•œํ•  blocking idle time(์ดˆ)? 5   new_max_idle_blocker_time

โœ”๏ธ SYSTEM user๋กœ ์ ‘์†ํ•˜์—ฌ consumer group ONLINE_USERS2 ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค

SQL> conn system/oracle
SYSTEM> exec dbms_resource_manager.create_pending_area();
--  ๋ฆฌ์†Œ์Šค ๋ฉ”๋‹ˆ์ €๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ  ์˜์—ญ์„ ์„ค์ •ํ•˜๊ฒ ๋‹ค. 

โœ”๏ธ ์ปจ์Šˆ๋จธ ๊ทธ๋ฃน ์ƒ์„ฑ (ONLINE_USERS2) / SYS_GROUP ๊ณผ OTHER_GROUP๋Š” ํ•ญ์ƒ ๋””ํดํŠธ๋กœ ์žˆ๋‹ค.

SYSTEM> exec dbms_resource_manager.create_consumer_group('ONLINE_USERS2','Users for Online2');

-- SYS_GROUP ๊ณผ OTHER_GROUP ์€ ์ด๋ฏธ ๋งŒ๋“ค์–ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ONLINE_USERS ๋งŒ ์ƒ์„ฑํ•œ๋‹ค.
    โ†‘               โ†‘
 dba๊ถŒํ•œ ์œ ์ €๋“ค    ๋ฆฌ์†Œ์Šค ์‚ฌ์šฉ์— ๋Œ€ํ•œ ์ œํ•œ์ด ๋†’์€ ์œ ์ €๋“ค

๐Ÿ’ก db ์— ๋งŒ๋“ค์–ด์ ธ์žˆ๋Š” Consumer ๊ทธ๋ฃนํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด

 select consumer_group 
    from dba_rsrc_consumer_groups
    where consumer_group like '%ONLINE%';

CONSUMER_GROUP
------------------------------
ONLINE_USERS2

โœ”๏ธ ๋ฆฌ์†Œ์Šค ํ”Œ๋žœ ์ƒ์„ฑ (nighttime)

SQL> 
begin
 dbms_resource_manager.create_plan('NIGHTTIME','Plan for night');
end;
/

PL/SQL procedure successfully completed.

โœ”๏ธ ์œ„์—์„œ ์ƒ์„ฑํ•œ nighttime plan ์ด ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜์‹œ์˜ค !

select plan
  from dba_rsrc_plans; 

โœ”๏ธ ๋ฆฌ์†Œ์Šค ๋””๋ ‰ํ‹ฐ๋ธŒ ์ƒ์„ฑ (๋””๋ ‰ํ‹ฐ๋ธŒ : ๋ถ„๋ฐฐ๋ฐฉ๋ฒ•)
1. ๊ธฐ์กด์— ์ด๋ฏธ ์žˆ๋Š” SYS_GROUP์€ CPU๋ฅผ 1์ˆœ์œ„๋กœ 100% ์‚ฌ์šฉํ•˜๋„๋ก ์ง€์‹œ์–ด ์ƒ์„ฑ

SQL> 
begin
 dbms_resource_manager.create_plan_directive('NIGHTTIME', 'SYS_GROUP','1st Group Set', cpu_p1=>100);
end;
/

PL/SQL procedure successfully completed.
์ œํ•œํ•  ๋ณ‘๋ ฌ๋„๋Š”? 4                  new_paralle_degree_limit_p1
์ œํ•œํ•  undo ์‚ฌ์šฉ๋Ÿ‰์€? 500๋ฐ”์ดํŠธ      new_undo_pool
์ œํ•œํ•  execution time(์ดˆ)? 300     new_max_est_exec_time
์ œํ•œํ•  idle time(์ดˆ)? 10           new_max_idle_time
์ œํ•œํ•  blocking idle time(์ดˆ)? 5   new_max_idle_blocker_time
  1. online_users2 ๊ทธ๋ฃน์€ lock blocking time์„ 30์ดˆ๋กœ ์ง€์ •, cpu ์‚ฌ์šฉ ์šฐ์„ ์ˆœ์œ„ 2๋ฒˆ์งธ ์ด๊ณ  70%๋งŒ ์‚ฌ์šฉ
begin
 dbms_resource_manager.create_plan_directive('NIGHTTIME', 'ONLINE_USERS2','2nd Group set', 
  cpu_p2=>70,
  parallel_degree_limit_p1=>4,
  undo_pool=>500,
  max_est_exec_time=>300,
  max_idle_time=>10,
  max_idle_blocker_time=>5);
end;
/
  1. other_groups ๋Š” ๋ณ‘๋ ฌ๋„๋ฅผ 4๋กœ ์ง€์ •, cpu ์‚ฌ์šฉ์œจ ์šฐ์„ ์ˆœ์œ„๊ฐ€ 2๋ฒˆ์งธ์ด๊ณ  30%๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •
begin
 dbms_resource_manager.create_plan_directive('NIGHTTIME', 'OTHER_GROUPS','3rd Group', 
cpu_p2=>30,parallel_degree_limit_p1=>4);
end;
/

โžก๏ธ 2, 3๋ฒˆ cpu_p2 (cpu ์‚ฌ์šฉ)์ด ๋‘๊ฐœ๊ฐ€ ํ•ฉ์ณ์„œ 100% ์ด ๋˜์–ด์•ผ ๊ฒ€์ฆ์—์„œ ์—๋Ÿฌ๋‚˜์ง€์•Š๋Š”๋‹ค.

โœ”๏ธ ๊ฒ€์ฆ ์ž‘์—…

SYSTEM> 
exec dbms_resource_manager.validate_pending_area();
PL/SQL procedure successfully completed.

โœ”๏ธ ๊ตฌํ˜„

SYSTEM> 
exec dbms_resource_manager.submit_pending_area();
alter system set resource_manager_plan=nighttime;
PL/SQL procedure successfully completed.

๐Ÿ’ก๋‚ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ–ˆ๋Š”์ง€ ์กฐํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•

SQL> select plan, group_or_subplan, cpu_p1, cpu_p2,  
            parallel_degree_limit_p1 dop, max_idle_blocker_time block_t
     from dba_rsrc_plan_directives
     where plan='NIGHTTIME'
     order by 1,2;

โœ”๏ธ HR user๋ฅผ consumer group ONLINE_USERS2๋กœ ์ง€์ •ํ•˜๊ณ  default consumer group์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.

SQL> exec dbms_resource_manager_privs.grant_switch_consumer_group('HR', 'ONLINE_USERS2', true);
PL/SQL procedure successfully completed.

-- ์ง€๊ธˆ ๋‹น์žฅ consumer group์ด ONLINE_USERS๊ฐ€ ๋˜๋Š” ๊ฒƒ.
SQL> exec dbms_resource_manager.set_initial_consumer_group('HR', 'ONLINE_USERS2');
PL/SQL procedure successfully completed.   

๐Ÿ“– 17์žฅ. Scheduler(์Šค์ผ€์ค„๋Ÿฌ)

๐Ÿ’ก os์˜ crontab๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์˜ค๋ผํด์—์„œ ๊ตฌํ˜„ํ•œ๊ฒƒ. ํŠน์ • ์‹œ๊ฐ„์— ํŠน์ • ์ž‘์—…์ด ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰๋˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๊ธฐ๋Šฅ!

โœ”๏ธ ์Šค์ผ€์ค„๋Ÿฌ์˜ ์ข…๋ฅ˜ 2๊ฐ€์ง€?

  • time base ์Šค์ผ€์ค„๋Ÿฌ : ํŠน์ • ์‹œ๊ฐ„์— ํŠน์ • ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๋„๋ก
  • event base ์Šค์ผ€์ค„๋Ÿฌ : ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํŠน์ • ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๋„๋ก

โžก๏ธ dba ์ž‘์—…์ด ์Šค์ผ€์ค„๋Ÿฌ ์‚ฌ์šฉ์— ์œ ์šฉํ•œ ๊ฒฝ์šฐ
1. ๋งค์ผ๋ฐค ๋˜๋Š” ๋งค์›” ๋ง์ผ์— ์›”๋ง ์ž‘์—…(ํ†ต๊ณ„ ์ •๋ณด ์ˆ˜์ง‘)์„ ์ž๋™ํ™”
2. database ๋ฐฑ์—…์„ ์ž๋™ํ™”
3. ํ…Œ์ด๋ธ” ํ†ต๊ณ„์ •๋ณด ์ˆ˜์ง‘ ์ž๋™ํ™”
4. invalid๋œ ์ธ๋ฑ์Šค๋‚˜ ํ”„๋กœ์‹œ์ €๋ฅผ ๋‹ค์‹œ valid์‹œํ‚ค๋Š” ์ž‘์—…์ด ํŠน์ •์‹œ๊ฐ„์— ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰๋˜๊ฒŒ ํ•  ๋•Œ

โžก๏ธ crontab๊ณผ ๋น„๊ตํ•ด์„œ ๋” ์ข‹์€ ๊ธฐ๋Šฅ
1. ์ž‘์—… ์ˆ˜ํ–‰๊ณผ์ •์ด ์ž˜ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋ง ๊ฐ€๋Šฅ
2. ์ž‘์—… ์ˆ˜ํ–‰ ์ด๋ ฅ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
3. ์Šค์ผ€์ค„์„ ์„ค์ •ํ•˜๋Š”๊ฒŒ ํ›จ์”ฌ ๋” ์ž์„ธํžˆ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
(์˜ˆ: ์›”, ์ˆ˜, ๊ธˆ ๋ฐค 10์‹œ์— ํŠน์ • ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๊ฒŒ ํ•ด๋ผ! / ๋งค์›” ๋ง์ผ ๋ฐค 10์‹œ์— ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๊ฒŒ ํ•˜๋ผ!)

โœ๐Ÿป ์Šค์ผ€์ค„๋Ÿฌ ๊ตฌํ˜„ ๋‹จ๊ณ„ 4๊ฐ€์ง€

  1. ํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ (dbms_scheduler.create_program)
  2. ์Šค์ผ€์ค„ ์ƒ์„ฑ (dbms_scheduler.create_scheduler)
  3. ์ž‘์—… ์ƒ์„ฑ (dbms_scheduler.create_job)
  4. ์ž‘์—…์ด ์ž˜ ์ˆ˜ํ–‰๋˜๋Š”์ง€ ๋ชจ๋‹ˆํ„ฐ๋ง (dbms_scheduler_job_run_details)

โœ๏ธ Time base ์Šค์ผ€์ค„๋Ÿฌ ์ž‘์—… ์‹ค์Šต

0. scott ์—๊ฒŒ dba ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๊ณ ,scott์œ ์ €์—์„œ emp01 ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ

SYS> grant dba to scott;
SCOTT> create table emp01
         ( hiredate date );         

1. scott ์œผ๋กœ ์ ‘์†ํ•ด์„œ ์Šค์ผ€์ค„์„ ์ƒ์„ฑํ•œ๋‹ค.

SCOTT> 
begin
       dbms_scheduler.create_schedule(
       schedule_name=>'scott_insert_sch', 
       repeat_interval=>'freq=secondly; interval=5');
end;
/

โžก๏ธ ์ด๋ฆ„์€ scott_insert_sch์ด๊ณ , repeat_interval=>'freq=secondly; interval=5๋Š” 5์ดˆ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ์ž‘๋™๋˜๋Š” ์Šค์ผ€์ค„ ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค!

2. ํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ

SCOTT> 
begin
       dbms_scheduler.create_program(
       program_name=>'scott_insert_prog',
       program_type=>'PLSQL_BLOCK',
       program_action=>'begin insert into emp01(hiredate) values (sysdate); end;');
end;
/

โžก๏ธ program_action begin, end์‚ฌ์ด์— ์žˆ๋Š”๊ฒƒ์„ ์ˆ˜ํ–‰ํ• ๊ฒƒ์ด๋‹ค.๊ทธ๊ฒƒ์ด emp01 ํ…Œ์ด๋ธ”์— ์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ

3. ์ž‘์—…(job) ์ƒ์„ฑ

SCOTT> 
begin
       dbms_scheduler.create_job(
       job_name=>'scott_insert',
       program_name=>'scott_insert_prog',
       schedule_name=>'scott_insert_sch' );
end;
/

4. ์ž‘์—…, ํ”„๋กœ๊ทธ๋žจ enable ์‹œํ‚ค๊ธฐ

SCOTT> exec dbms_scheduler.enable('scott_insert');
SCOTT> exec dbms_scheduler.enable('scott_insert_prog');

5. ๊ฒฐ๊ณผ ํ™•์ธ

SCOTT> select * from emp01;


6. ์Šค์ผ€์ค„์ด ์ž˜ ๋„๋Š”์ง€ ํ™•์ธํ•˜์‹œ์˜ค !

select * from  dba_scheduler_job_run_details 
 WHERE owner='SCOTT';

schedule, job, program์„ ์ค‘์ง€/Dropํ•˜๊ธฐ

๋ฌธ์ œ scott_insert job์„ ์ค‘์ง€์‹œํ‚ค๊ธฐ

SCOTT> exec dbms_scheduler.disable('scott_insert');

๋ฌธ์ œ scott_insert job์„ drop ์‹œํ‚ค์‹œ์˜ค!

SCOTT> exec dbms_scheduler.drop_job('scott_insert');

๋ฌธ์ œ scott_insert_sch ์Šค์ผ€์ค„์„ dropํ•˜๊ธฐ!

SCOTT> exec dbms_scheduler.drop_schedule('scott_insert_sch');

๋ฌธ์ œ scott_insert_prog ํ”„๋กœ๊ทธ๋žจ์„ dropํ•˜๊ธฐ!

SCOTT> exec dbms_scheduler.drop_program('scott_insert_prog');

๋ฌธ์ œ ์•„๋ž˜์˜ ๋กœ๊ทธ์Šค์œ„์น˜๋ฅผ ์ผ์œผํ‚ค๋Š” pl/sql์ด 5์ดˆ์— ํ•œ๋ฒˆ์”ฉ ์ˆ˜ํ–‰๋˜๊ฒŒ ์Šค์ผ€์ค„, ํ”„๋กœ๊ทธ๋žจ, ์ž‘์—…(job)์„ ๋งŒ๋“œ์‹œ์˜ค!

begin 
 execute immediate 'alter system switch logfile';
end; --์–˜๋ฅผ 5์ดˆ์— ํ•œ๋ฒˆ์”ฉ ์ˆ˜ํ–‰๋˜๋„๋ก!!!

'begin 
 execute immediate ''alter system switch logfile'';
end;' 

1. scott ์œผ๋กœ ์ ‘์†ํ•ด์„œ ์Šค์ผ€์ค„์„ ์ƒ์„ฑํ•œ๋‹ค.

SCOTT> 
begin
       dbms_scheduler.create_schedule(
       schedule_name=>'scott_logs_sch', 
       repeat_interval=>'freq=secondly; interval=5');
end;
/

โžก๏ธ ์ด๋ฆ„์€ scott_logs_sch์ด๊ณ , repeat_interval=>'freq=secondly; interval=5๋Š” 5์ดˆ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ์ž‘๋™๋˜๋Š” ์Šค์ผ€์ค„ ์ด๋ผ๋Š” ๊ฒƒ์ด๋‹ค!

2. ํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ

SCOTT> 
begin
       dbms_scheduler.create_program(
       program_name=>'scott_logs_prog',
       program_type=>'PLSQL_BLOCK',
       program_action=>'begin 
 execute immediate ''alter system switch logfile'';
end;');
end;
/

โžก๏ธ program_action begin, end์‚ฌ์ด์— ์žˆ๋Š”๊ฒƒ์„ ์ˆ˜ํ–‰ํ• ๊ฒƒ์ธ๋ฐ ๊ทธ๊ฒƒ์€ ๋กœ๊ทธ์Šค์œ„์น˜๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒƒ !

3. ์ž‘์—…(job) ์ƒ์„ฑ

SCOTT> 
begin
       dbms_scheduler.create_job(
       job_name=>'scott_logs',
       program_name=>'scott_logs_prog',
       schedule_name=>'scott_logs_sch' );
end;
/

4. ์ž‘์—…, ํ”„๋กœ๊ทธ๋žจ enable ์‹œํ‚ค๊ธฐ

SCOTT> exec dbms_scheduler.enable('scott_logs');
SCOTT> exec dbms_scheduler.enable('scott_logs_prog');

5. ๊ฒฐ๊ณผ ํ™•์ธ - alert log file ํ™•์ธ

$ tail -f alert*


6. ์Šค์ผ€์ค„์ด ์ž˜ ๋„๋Š”์ง€ ํ™•์ธํ•˜์‹œ์˜ค !

select * from  dba_scheduler_job_run_details 
 WHERE owner='SCOTT';


7. ๋‹ค์‹œ disable์‹œํ‚ค๊ธฐ

SCOTT> exec dbms_scheduler.disable('scott_logs');

์˜ค๋Š˜์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ dept table์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์•„๋ž˜์˜ ํ”„๋กœ์‹œ์ €๋ฅผ ์ˆ˜ํ–‰ํ•˜์‹œ์˜ค!

create or replace procedure dept_insert
is
   
    v_deptno dept.deptno%type;
    
begin  

  select max(deptno) + 10 into v_deptno
    from dept;
    
  insert into dept(deptno, dname, loc)
    values(v_deptno, 'aaa','bbb');

end;
/
------------------------------------------------
exec dept_insert

1. scott ์œผ๋กœ ์ ‘์†ํ•ด์„œ ์Šค์ผ€์ค„์„ ์ƒ์„ฑํ•œ๋‹ค.

SCOTT> 
begin
       dbms_scheduler.create_schedule(
       schedule_name=>'scott_dept_insert_sch', 
       repeat_interval=>'freq=secondly; interval=5');
end;
/

2. ํ”„๋กœ๊ทธ๋žจ ์ƒ์„ฑ

SCOTT> 
begin
       dbms_scheduler.create_program(
       program_name=>'scott_dept_insert_prog',
       program_type=>'STORED_PROCEDURE',
       program_action=>'dept_insert');
end;
/

SCOTT @ orcl3 > begin
       dbms_scheduler.create_program(
       program_name=>'scott_dept_insert_prog',
       program_type=>'PLSQL_BLOCK',
       program_action=>'begin dept_insert; end;');
end;
/ 

โžก๏ธ program_action begin, end์‚ฌ์ด์— ์žˆ๋Š”๊ฒƒ์„ ์ˆ˜ํ–‰!

3. ์ž‘์—…(job) ์ƒ์„ฑ

SCOTT> 
begin
       dbms_scheduler.create_job(
       job_name=>'scott_insert_dept',
       program_name=>'scott_dept_insert_prog',
       schedule_name=>'scott_dept_insert_sch' );
end;
/

4. ์ž‘์—…, ํ”„๋กœ๊ทธ๋žจ enable ์‹œํ‚ค๊ธฐ

SCOTT> exec dbms_scheduler.enable('scott_insert_dept');
SCOTT> exec dbms_scheduler.enable('scott_dept_insert_prog');

5. ๊ฒฐ๊ณผ ํ™•์ธ

SCOTT> select * from dept;

6. ์Šค์ผ€์ค„์ด ์ž˜ ๋„๋Š”์ง€ ํ™•์ธํ•˜์‹œ์˜ค !

select * from  dba_scheduler_job_run_details 
 WHERE owner='SCOTT';

7. ๋‹ค์‹œ disable์‹œํ‚ค๊ธฐ

SCOTT> exec dbms_scheduler.disable('scott_insert_dept');

exec dbms_scheduler.drop_program('scott_dept_insert_prog');
exec dbms_scheduler.drop_schedule('scott_dept_insert_sch');
exec dbms_scheduler.drop_job('scott_insert_dept');

profile
Slow and steady wins the race.

0๊ฐœ์˜ ๋Œ“๊ธ€