Buscar este blog

lunes, 2 de mayo de 2011

Histórico de Ejecución de un SP

Amigos,

Les traigo del botiquín otro script interesante cuando desean estudiar el histórico de ejecución de un job. Simplemente completen el script con el nombre del job deseado y listo. Ya lo saben.. nada de nada perdiendo el tiempo con el "view history" ;). Saludos.


---------------------------------------------------------
-- Autor: Gustavo Herrera --
-- Listar histórico tiempo de ejecución de un job --
---------------------------------------------------------

select job_name, run_datetime, run_duration
from
(
select job_name, run_datetime,
SUBSTRING(run_duration, 1, 2) + ':' + SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) AS run_duration
from
(
select DISTINCT
j.name as job_name,
run_datetime = CONVERT(DATETIME, RTRIM(run_date)) +
(run_time * 9 + run_time % 10000 * 6 + run_time % 100 * 10) / 216e4,
run_duration = RIGHT('000000' + CONVERT(varchar(6), run_duration), 6)
from msdb..sysjobhistory h
inner join msdb..sysjobs j
on h.job_id = j.job_id
) t
) t
where job_name = 'Nombre del Job'
order by run_datetime

Activity Monitor ? no, no.. mejor vista del Sistema Sys.Sysprocesses

Amigos,

Cuántas veces se han sentido frustrados ante la imposibilidad de poder estudiar "por consola" cuáles son los procesos que están utilizando recursos de Sql Server mediante la poco efectiva herramienta Activity Monitor? Apuesto a que muchas veces que han intentado utilizar esta herramienta en ocasiones en las cuales el Sql está siendo fuertemente impactado, se han topado con un desalentado "Time Out"

Pues bien, a no remar contra la corriente... Bill Gates pensó en nostros y nos da una vista del Sistema llamada Sys.Sysprocesses, la cual nos permite obtener los mismos resultados que el Activity Monitor, sin Time Outs de por medio.

A utlizarla entonces, va un ejemplo.


-- Sys.sysprocesses --
select
spid,
blocked, -- solo valor si está bloqueada
waittime, -- 0 = process its not waiting -
lastwaittype, -- Description of last waiting
dbid, -- Data base id used by the process
cpu, -- Cumulative cpu time for the process
physical_io, -- Cumulative disk reads and writes
memusage, -- Number of page in the cache allocated by the process
Login_time, -- Time in which a process begin to log
last_batch, -- Time in wich a last process has ocurred
open_tran, -- Number of open transactions for the process
status, -- "dormant" -- is being resetting the session --
-- "running" -- is running one or more batches --
-- "background" -- running background process such as rollback process --
-- "pending" -- waiting for thread to continue
-- "runnable" -- the task is in the runnable queue --
-- "suspended" -- waiting for an event to complete --
sid, -- user identificator
hostname, -- name of the workstation
cmd, -- command that is being executed --
nt_username -- user name for the process
from sys.sysprocesses


-- En caso de quere5 "asesinar" un proceso --
KILL SPID;

Listar Todos Los Jobs de una Base de Datos

Amigos,
Les dejo un script que permite listar todos los jobs correspondientes a un servidor de Bases de Datos.
Creo que el script es realmente interesante. Ojalá así les resulte.
Hasta la próxima.


------------------------------------------------
-- Autor: Gustavo Herrera (Mayo 2011 --
-- Detalle: Lista Todos los Jobs de un Server --
-----------------------------------------------
select
'Server' = left(@@ServerName,20),
'JobName' = S.name,--left(S.name,90),
'ScheduleName' = left(ss.name,25),
'Enabled' = CASE (S.enabled)
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE '??'
END,
'Frequency' = CASE(ss.freq_type)
WHEN 1 THEN 'Once'
WHEN 4 THEN 'Daily'
WHEN 8 THEN
(case when (ss.freq_recurrence_factor > 1)
then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)
WHEN 16 THEN
(case when (ss.freq_recurrence_factor > 1)
then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' else 'Monthly' end)
WHEN 32 THEN 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' -- RELATIVE
WHEN 64 THEN 'SQL Startup'
WHEN 128 THEN 'SQL Idle'
ELSE '??'
END,
'Interval' = CASE
WHEN (freq_type = 1) then 'One time only'
WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'
WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'
WHEN (freq_type = 8) then (select 'Weekly Schedule' = D1+ D2+D3+D4+D5+D6+D7
from (select ss.schedule_id,
freq_interval,
'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,
'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,
'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,
'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,
'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,
'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,
'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END
from msdb..sysschedules ss
where freq_type = 8
) as F
where schedule_id = sj.schedule_id
)
WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)
WHEN (freq_type = 32) then (select freq_rel + WDAY
from (select ss.schedule_id,
'freq_rel' = CASE(freq_relative_interval)
WHEN 1 then 'First'
WHEN 2 then 'Second'
WHEN 4 then 'Third'
WHEN 8 then 'Fourth'
WHEN 16 then 'Last'
ELSE '??'
END,
'WDAY' = CASE (freq_interval)
WHEN 1 then ' Sun'
WHEN 2 then ' Mon'
WHEN 3 then ' Tue'
WHEN 4 then ' Wed'
WHEN 5 then ' Thu'
WHEN 6 then ' Fri'
WHEN 7 then ' Sat'
WHEN 8 then ' Day'
WHEN 9 then ' Weekday'
WHEN 10 then ' Weekend'
ELSE '??'
END
from msdb..sysschedules ss
where ss.freq_type = 32
) as WS
where WS.schedule_id =ss.schedule_id
)
END,
'Time' = CASE (freq_subday_type)
WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(Active_Start_Time)))+ convert(varchar(6),Active_Start_Time),3,0,':')),6,0,':'),8)
WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'
WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'
WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'
ELSE '??'
END,

'Next Run Time' = CASE SJ.next_run_date
WHEN 0 THEN cast('n/a' as char(10))
ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)
END

from msdb.dbo.sysjobschedules SJ
join msdb.dbo.sysjobs S on S.job_id = SJ.job_id
join msdb.dbo.sysschedules SS on ss.schedule_id = sj.schedule_id
order by S.name