martes, 5 de junio de 2012

Script - Control de Sesiones en base a Planes de Ejecución

Script para el control de sesiones activas en base a métodos de acceso en sus planes de ejecución.

En el ejemplo: Sesiones activas por más de 1 hora con MERGE JOIN CARTESIAN, el control en este caso consiste en cerrar la sesión mediante un kill immediate.

declare
cursor sql_id is
select l.sql_id from V$session l, v$sql s where SUBSTR(l.STATUS,1,10) LIKE 'ACTIV%' and
            l.sql_id is not null and
            l.username is not null and
            l.sql_id=s.sql_id and
            -- CPU_TIME > 1 hora
            s.cpu_time > (3600)*1000000 ;
v_ind number;
v_mata varchar2(1000);
begin
v_ind:=0;
for v_reg in sql_id
loop
    selecT COUNT(1) INTO v_ind
    FROM TABLE(DBMS_XPLAN.DISPLAY_cursor(v_reg.sql_id,null)) where plan_table_output like '%MERGE JOIN CARTESIAN%';
    if v_ind > 0 then
        select 'ALTER SYSTEM KILL SESSION '''||    L.SID||','||SUBSTR(TO_CHAR(L.SERIAL#),1,8)||''' immediate' into v_mata FROM V$SESSION L WHERE  L.sql_id=v_reg.sql_id and l.username is not null;
        execute immediate v_mata;
    end if;
    v_ind:=0;
end loop;
exception
    when others then null;
end;
/

No hay comentarios:

Publicar un comentario