Skip to content

Commit 3a0d783

Browse files
committed
Adicionado e revisado vários scritps de monitoramento geral da instancias e requisicoes
1 parent 3d03b0f commit 3a0d783

16 files changed

+1306
-0
lines changed

Dumps/DumpTrigger.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Comandos DBCC para ver ou setar dumptrigger.
8+
Mais info: https://techcommunity.microsoft.com/blog/sqlserversupport/how-do-i-determine-which-dump-triggers-are-enabled/315740
9+
e https://techcommunity.microsoft.com/blog/sqlserversupport/how-it-works-controlling-sql-server-memory-dumps/315875
10+
11+
*/
12+
13+
-- ver as triggers habilitadas
14+
dbcc traceon(3604)
15+
DBCC DUMPTRIGGER('display')
16+
17+
-- Set trigger para um erro especifico
18+
-- DBCC DUMPTRIGGER('set',CodigoErro)
19+
20+
-- remover trigger
21+
-- DBCC DUMPTRIGGER('clear',CodigoErro)

Instance/ActivityStatistics.sql

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Uma quer que usei muito para trazer estatísticas de execução, para ter uma visão rápida de tudo que estava rodando no servidor a qualquer momento.
8+
Com essa query vc sabe a quantidade de requisicoes rodando, o tempo da mais antiga, quantas estao rodando de fato, quantas estao esperando cpu (runnable), em wait, etc.
9+
Pra um visão rápida, sem muita precisão, é uma boa consulta.
10+
11+
*/
12+
13+
DECLARE
14+
@qsStatsTime int
15+
,@highRunMS int
16+
17+
SET @highRunMS = 1000;
18+
19+
SELECT
20+
*
21+
FROM
22+
(
23+
SELECT
24+
AllRequests = COUNT(*)
25+
,RequestCount = COUNT(CASE WHEN RS.IsNormal = 1 THEN RS.session_id END)
26+
,OldestRequest = MIN(CASE WHEN RS.IsNormal = 1 THEN RS.start_time END)
27+
,OldestTime = DATEDIFF(SS,MIN(CASE WHEN RS.IsNormal = 1 THEN RS.start_time END),CURRENT_TIMESTAMP)
28+
,Running = COUNT(CASE WHEN RS.status = 'running' AND RS.IsNormal = 1 THEN RS.session_id END)
29+
,Runnables = COUNT(CASE WHEN RS.status = 'runnable' AND RS.IsNormal = 1 THEN RS.session_id END)
30+
,Waiting = COUNT(CASE WHEN RS.status = 'suspended' AND RS.IsNormal = 1 THEN RS.session_id END)
31+
,HighRun = COUNT(CASE WHEN RS.HighRun = 1 AND RS.IsNormal = 1 THEN RS.session_id END)
32+
,AvgCPUFactor = AVG(CASE WHEN RS.IsNormal = 1 THEN RS.CPUFactor END)
33+
,MaxCPUFactor = MAX(CASE WHEN RS.IsNormal = 1 THEN RS.CPUFactor END)
34+
,AvgReads = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.reads) END)
35+
,AvgWrites = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.writes) END)
36+
,AvgWaiting = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.wait_time) END)
37+
,AvgElapsed = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.total_elapsed_time) END)
38+
FROM
39+
(
40+
SELECT
41+
*
42+
,CASE WHEN isAdmin = 0 AND isForcedWait = 0 THEN 1 ELSE 0 END IsNormal
43+
,CONVERT(bigint,CASE
44+
DATEDIFF(MS,RS.start_time,CURRENT_TIMESTAMP) WHEN 0 THEN 0
45+
ELSE RS.cpu_time*1.00/DATEDIFF(MS,RS.start_time,CURRENT_TIMESTAMP)
46+
END) as CPUFactor
47+
,CONVERT(bigint,CASE
48+
WHEN RS.total_elapsed_time > @highRunMS THEN 1
49+
ELSE 0
50+
END) as HighRun
51+
FROM
52+
(
53+
SELECT
54+
*
55+
,CASE
56+
WHEN R.command IN ('DBCC') THEN 1
57+
WHEN R.command LIKE '%BACKUP%' THEN 1
58+
ELSE 0
59+
END isAdmin
60+
,CASE
61+
WHEN R.command IN ('WAITFOR') THEN 1
62+
WHEN R.command LIKE '%BACKUP%' THEN 1
63+
ELSE 0
64+
END isForcedWait
65+
FROM
66+
sys.dm_exec_requests R
67+
WHERE
68+
R.session_id > 50
69+
AND
70+
R.session_id != @@SPID
71+
) RS
72+
) RS
73+
) R
74+
CROSS JOIN
75+
(
76+
SELECT
77+
AVG(last_worker_time/1000) AS S_MediaCPU
78+
,AVG(last_elapsed_time/1000) AS S_MediaTempo
79+
,MAX(last_execution_time) AS S_LastExecuted
80+
,COUNT(*) AS S_QtdQueries
81+
FROM
82+
sys.dm_exec_query_Stats
83+
WHERE
84+
last_execution_time > DATEADD(SS,-ISNULL(@qsStatsTime,1),CURRENT_TIMESTAMP)
85+
) QS
86+
OPTION(RECOMPILE) --> Evita que o plano fique em cache!

Instance/Atividade.sql

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Por anos, essa query foi minha sp_whoisactive.
8+
Antes de conhecer a sp_whoisactie (e mais tarde a PowerIsActive, do power alerts), eu usei muito isso aqui para saber como estava o servidor.
9+
Uma query que me trazia as infos básicas de tudo que estava rodando. Me ajudou muito a responder rapidamente a problemas de várias natureza (locks, cpu, disco, etc.)
10+
11+
*/
12+
13+
use master
14+
go
15+
16+
select
17+
s.session_id Sessao
18+
,s.login_name Login
19+
--,s.host_name HOST
20+
--,user_name(r.user_id) Usuario
21+
,r.cpu_time CPUr
22+
,convert(decimal(5,2),CONVERT(bigint,r.cpu_time)*1.00/NULLIF(CONVERT(bigint,r.total_elapsed_time),0)) as CPUf
23+
,r.total_elapsed_time TMPr
24+
,r.status
25+
,r.blocking_session_id BLK
26+
,r.wait_type
27+
,r.percent_complete
28+
,r.arithabort
29+
,r.command
30+
,r.reads
31+
,r.granted_query_memory
32+
,r.last_wait_type
33+
,SUBSTRING(ex.text,r.statement_start_offset/2 + 1, ISNULL((NULLIF(r.statement_end_offset,-1) - r.statement_start_offset)/2 + 1,LEN(ex.text)) ) as Trecho
34+
,object_name( ex.objectid,ex.dbid ) objeto
35+
,db_name( r.database_id ) Banco
36+
,TSK.TaskCount
37+
,r.sql_handle
38+
,RequestPlan = qp.query_plan
39+
,CachedPLan = p.query_plan
40+
,PlanText = p.planText
41+
,qg.*
42+
from
43+
sys.dm_exec_sessions s
44+
inner join sys.dm_exec_requests r on r.session_id = s.session_id
45+
outer apply sys.dm_exec_sql_text( r.sql_handle ) as ex
46+
outer apply (
47+
select
48+
count(*) as TaskCount
49+
from
50+
sys.dm_os_tasks T
51+
WHERE
52+
T.session_id = r.session_id
53+
AND
54+
T.request_id = r.request_id
55+
) TSK
56+
outer apply sys.dm_exec_query_plan(r.sql_handle) qp
57+
outer apply (
58+
select
59+
qp.*
60+
,planText = tqp.query_plan
61+
from
62+
sys.dm_exec_query_stats qs
63+
outer apply
64+
sys.dm_exec_query_plan(qs.plan_handle) qp
65+
outer apply
66+
sys.dm_exec_text_query_plan(qs.plan_handle, qs.statement_start_offset, qs.statement_end_offset) tqp
67+
where
68+
qs.sql_handle = r.sql_handle
69+
) p
70+
outer apply (
71+
select
72+
qp.*
73+
,grantQueryPlan = qp.query_plan
74+
,qg.reserved_worker_count
75+
,qg.used_worker_count
76+
from
77+
sys.dm_exec_query_memory_grants qg
78+
outer apply
79+
sys.dm_exec_query_plan(qg.plan_handle) qp
80+
where
81+
qg.session_id = s.session_id
82+
and
83+
qg.request_id = r.request_id
84+
) qg
85+
where
86+
s.session_id > 50
87+
and
88+
s.session_id <> @@spid
89+
and
90+
s.is_user_process = 1
91+
-- and
92+
-- wait_type = 'LCK_M_X'
93+
order by
94+
r.cpu_time desc
95+
96+
97+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Uma query simples para comparar as estatísticas de execução de procedures.
8+
Ali one tem "nome proc" você especificar o filtro para achar as procs (E bancos)... Especificando mais de uma, você pode compará-las...
9+
Útil para comparar o desempenho de uma proc X e Y, sendo que a Y foi uma versão otimizada de X que você quer testar.
10+
11+
*/
12+
13+
14+
IF OBJECT_ID('tempdb..#TabelaResultados') IS NOT NULL
15+
DROP TABLE #TabelaResultados;
16+
17+
SELECT
18+
db_name(st.dbid) AS Banco
19+
,ISNULL(object_name(st.objectid, st.dbid), 'Ad Hoc') AS Objeto
20+
,SUBSTRING(CASE
21+
WHEN st.TEXT IS NULL THEN ''
22+
ELSE st.TEXT
23+
END, qs.statement_start_offset / 2, CASE qs.statement_end_offset
24+
WHEN -1 THEN LEN(st.TEXT)
25+
ELSE ( qs.statement_end_offset - qs.statement_start_offset ) / 2
26+
END) AS Trecho
27+
,qs.plan_generation_num AS Compilacoes
28+
,qs.last_worker_time AS CPU
29+
,qs.last_elapsed_time AS TempDec
30+
,qs.last_execution_time AS UltimaExec
31+
,qs.last_logical_writes AS Escritas
32+
,qs.last_logical_reads AS Leituras
33+
-- ,last_execution_time,execution_count,total_worker_time,last_worker_time
34+
-- ,last_logical_writes,last_logical_reads,last_elapsed_time
35+
INTO #TabelaResultados
36+
FROM
37+
sys.dm_exec_query_stats qs
38+
OUTER APPLY sys.dm_exec_sql_text(qs.sql_handle) st
39+
OUTER APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
40+
WHERE st.objectid IN ( OBJECT_ID('') ) -- nome proc
41+
AND ST.DBID = DB_ID('') -- nome banco
42+
43+
--ORDER BY
44+
-- CPU DESC
45+
SELECT
46+
row_number() OVER( ORDER BY Objeto )
47+
,Objeto
48+
,CAST(1.00 * SUM(cpu) / 1000 AS DECIMAL(10, 2)) AS CPU
49+
,CONVERT(VARCHAR(15), MAX( UltimaExec ), 103) + ' ' + CONVERT(VARCHAR(12), MAX( UltimaExec ), 114) AS UltimaExec
50+
,SUM(ESCRiTAS) AS Escritas
51+
,SUM(leituras) AS Leituras
52+
,CAST(1.00 * SUM(TempDec) / 1000 AS DECIMAL(10, 2)) AS TempoDecorrido
53+
FROM
54+
#TabelaResultados
55+
GROUP BY Objeto
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Traz as tops X queries que rodaram (estão no cache ainda) mais lentas.
8+
Você poe trocar o criterio, alterando o ORDER BY na CTE.
9+
10+
*/
11+
12+
WITH qTOP AS
13+
(
14+
SELECT TOP 10
15+
db_name( st.dbid ) as Banco
16+
,ISNULL(object_name( st.objectid, st.dbid ),'Ad Hoc') as Objeto
17+
,SUBSTRING(
18+
CASE WHEN st.text IS NULL THEN '' ELSE st.text END
19+
,qs.statement_start_offset/2
20+
,CASE qs.statement_end_offset WHEN -1 THEN LEN( st.text ) ELSE (qs.statement_end_offset- qs.statement_start_offset)/2 END
21+
) as Trecho
22+
,qs.plan_generation_num as Compilacoes
23+
,qs.total_worker_time/qs.execution_count as CPU
24+
,qs.total_elapsed_time as TempDec
25+
,qs.last_execution_time as UltimaExec
26+
,qs.max_logical_writes as Escritas
27+
,qs.max_logical_reads as Leituras
28+
FROM
29+
sys.dm_exec_query_stats qs
30+
OUTER APPLY sys.dm_exec_sql_text( qs.sql_handle ) st
31+
OUTER APPLY sys.dm_exec_query_plan( qs.plan_handle ) qp
32+
ORDER BY
33+
CPU DESC
34+
)
35+
SELECT
36+
Banco
37+
,Objeto
38+
,Compilacoes
39+
,CPU
40+
,Trecho
41+
,UltimaExec
42+
FROM
43+
qTOP

Instance/IOPendings.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Lista todos os io que estão pendings.
8+
Você quer ver isso vazio ou com números muito baixo o máximo de tempo possível.
9+
10+
11+
*/
12+
13+
select
14+
r.io_pending_ms_ticks
15+
,r.io_type
16+
,r.io_pending
17+
,db_name(vfs.database_id)
18+
,mf.physical_name
19+
from
20+
sys.dm_io_pending_io_requests r
21+
left join
22+
sys.dm_io_virtual_file_stats(null,null) vfs on vfs.file_handle = r.io_handle
23+
left join
24+
sys.master_files mf
25+
on mf.database_id = vfs.database_id
26+
and mf.file_id = vfs.file_id
27+
ORDER BY
28+
r.io_pending_ms_ticks desc

Instance/InstanceInfoSimple.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Uma query simpels para me trazer as infos de requests cpu e memória.
8+
9+
*/
10+
11+
SELECT
12+
DI.DatabaseCount
13+
,SI.cpu_count
14+
,SI.hyperthread_ratio
15+
,SI.physical_memory_kb/1024/1024 as MemoGB
16+
,SES.SessionsCounts
17+
,REQ.RequestsCount
18+
FROM
19+
(
20+
SELECT
21+
COUNT(*) DatabaseCount
22+
FROM
23+
sys.databases D
24+
WHERE
25+
D.database_id > 4
26+
) DI
27+
CROSS JOIN
28+
sys.dm_os_sys_info SI
29+
CROSS JOIN
30+
(
31+
SELECT
32+
COUNT(*) as SessionsCounts
33+
FROM
34+
sys.dm_exec_sessions S
35+
WHERE
36+
S.session_id > 50
37+
) SES
38+
CROSS JOIN
39+
(
40+
SELECT
41+
COUNT(*) as RequestsCount
42+
FROM
43+
sys.dm_exec_requests R
44+
WHERE
45+
R.session_id > 50
46+
) REQ
47+

0 commit comments

Comments
 (0)