Skip to content

Commit 4c57874

Browse files
committed
Scripts de senha e info de queries e requisicoes
1 parent ed9bf1e commit 4c57874

File tree

10 files changed

+1255
-0
lines changed

10 files changed

+1255
-0
lines changed

Instance/CollectWaitSession.sql

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Faz várias coletas das sessoes em wait.
8+
Util para analisar o progressos dos waits que uma sessão tinha...
9+
Isso foi antes de surgir a DMV sys.dm_exec_sessions_Wait_Stats, que traz isso melhor;
10+
11+
12+
13+
14+
*/
15+
16+
declare
17+
@session bigint
18+
,@MaxCollects int = 100
19+
20+
if object_id('tempdb..#waitinfo') is not null drop table #waitinfo;
21+
select
22+
convert(datetime,null) as start_time
23+
,session_id
24+
,wait_type
25+
,wait_duration_ms
26+
,W.wait_started_ms_ticks
27+
,W.wait_resumed_ms_ticks
28+
,CONVERT(bigint,NULL) as RunnableTime
29+
into
30+
#waitinfo
31+
from
32+
sys.dm_os_waiting_tasks WT
33+
JOIN
34+
sys.dm_os_workers W
35+
ON W.task_address = WT.waiting_task_address
36+
where 1 = 2
37+
38+
39+
declare @i int
40+
while @i <= @MaxCollects
41+
begin
42+
set @i += 1;
43+
insert into
44+
#waitinfo
45+
select
46+
R.start_time
47+
,R.session_id
48+
,ISNULL(wt.wait_type,R.wait_type)
49+
,ISNULL(wt.wait_duration_ms,R.wait_time)
50+
,W.wait_started_ms_ticks
51+
,W.wait_resumed_ms_ticks
52+
,CASE
53+
WHEN W.state = 'RUNNABLE' THEN SI.ms_ticks-W.wait_resumed_ms_ticks
54+
ELSE 0
55+
END as RunnableTime
56+
from
57+
sys.dm_exec_requests R
58+
JOIN
59+
sys.dm_os_workers W
60+
ON W.task_address = R.task_address
61+
CROSS JOIN
62+
sys.dm_os_sys_info SI
63+
LEFT JOIN
64+
sys.dm_os_waiting_tasks wt
65+
on wt.waiting_task_address = R.task_Address
66+
where
67+
R.session_id > 50
68+
and
69+
R.session_id != @@spid
70+
and
71+
(R.wait_type IS NOT NULL OR W.state = 'RUNNABLE')
72+
and
73+
(
74+
R.session_id = @session
75+
OR
76+
@session is null
77+
)
78+
end
79+
80+
81+
SELECT
82+
start_time
83+
,session_id
84+
,wait_type
85+
,SUM(wait_duration_ms) as Total
86+
,MIN(wait_duration_ms) as Minimum
87+
,MAX(wait_duration_ms) as Maximum
88+
,SUM(RunnableTime) AS RunnableTime
89+
,COUNT(*) as Qty
90+
FROM
91+
(
92+
select
93+
*
94+
,ROW_NUMBER() OVER(PARTITION BY wait_started_ms_ticks ORDER BY wait_duration_ms DESC) as Seq
95+
from
96+
#waitinfo
97+
) WI
98+
WHERE
99+
WI.Seq = 1
100+
GROUP BY
101+
start_time
102+
,session_id
103+
,wait_type
104+
105+

Instance/FileStats.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Info rápida sobre o IO dos arquivos.
8+
Como é um valor acumulado, pode não refletir a realidade, mas é um ponto de partida pra identificar algo fora de um padrão.
9+
10+
11+
*/
12+
13+
SELECT
14+
DB_NAME(vf.database_id)
15+
,MF.physical_name
16+
,vf.num_of_reads
17+
,vf.num_of_bytes_read
18+
,vf.io_stall_read_ms
19+
,vf.sample_ms
20+
,vf.num_of_reads/(vf.io_stall_read_ms/1000.00) AvgReadsPerSec
21+
,vf.num_of_bytes_read/(vf.io_stall_read_ms/1000.00) AvgReadsBytesPerSec
22+
,(vf.io_stall_read_ms/1000.00)/vf.num_of_reads SecsPerRead
23+
FROM
24+
sys.dm_io_virtual_file_stats(null,null) vf
25+
LEFT JOIN
26+
sys.master_files MF
27+
on MF.database_id = VF.database_id
28+
and MF.file_id = VF.file_id
29+
ORDER BY
30+
SecsPerRead DESC
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*#info
2+
3+
# Autor
4+
Rodrigo Ribeiro Gomes
5+
6+
# Descricao
7+
Esta query foi uma tentativa rápida de ter algo que me desse informações sobre o que está executando na instância.
8+
Eu queria que, com 1 simples query, tivesse uma noção do qual rápido ou lento uma instância poderia estar.
9+
Este script foi uma das primeiras tentativas disso, e, de fato, me ajudou:
10+
- Consigo saber quantas requisições estão rodando
11+
- Há quanto tempo a mais antiga está rodando
12+
- Quantoas estão em Wait e quantas runnable (gargalo de cpu)
13+
- Quantas em paralelo, e quantos estão rodando acima de um threhsold (highrun)
14+
- Quanto de cpu as queries estão usando (colunas com cpufactor no nome, que mede o total de cpu cosideranod a vida itneira da req)
15+
- Qtd de queries que rodou no ultimo segundo e a média de tempo
16+
- Com estão as requisicoes de dico, quanto pending e quanto tempo em média
17+
18+
19+
20+
21+
*/
22+
23+
DECLARE
24+
@LastQueryTime int
25+
,@highRunMS int
26+
,@highDisk int
27+
,@highNet int
28+
29+
SELECT @LastQueryTime = 1
30+
,@highRunMS = 1000
31+
,@highDisk = 5
32+
,@highNet = 5
33+
34+
35+
SELECT
36+
*
37+
,PorcEspera = Waiting*100/NULLIF(RequestCount,0)
38+
,PorcHighs = HighRun*100/NULLIF(RequestCount,0)
39+
,PorcUsedWorkers = WS.BusyWorkers*100/SI.MaxWorkersCount
40+
FROM
41+
(
42+
SELECT
43+
AllRequests = COUNT(*)
44+
,RequestCount = COUNT(CASE WHEN RS.IsNormal = 1 THEN RS.session_id END)
45+
,OldestRequest = MIN(CASE WHEN RS.IsNormal = 1 THEN RS.start_time END)
46+
,OldestTime = DATEDIFF(SS,MIN(CASE WHEN RS.IsNormal = 1 THEN RS.start_time END),CURRENT_TIMESTAMP)
47+
,Running = COUNT(CASE WHEN RS.status = 'running' AND RS.IsNormal = 1 THEN RS.session_id END)
48+
,Runnables = COUNT(CASE WHEN RS.status = 'runnable' AND RS.IsNormal = 1 THEN RS.session_id END)
49+
,Waiting = COUNT(CASE WHEN RS.status = 'suspended' AND RS.IsNormal = 1 AND RS.RealWaitsCount > 0 THEN RS.session_id END)
50+
,InParalell = COUNT(CASE WHEN RS.IsNormal = 1 AND RS.WorkerCount > 1 THEN RS.session_id END)
51+
,HighRun = COUNT(CASE WHEN RS.HighRun = 1 AND RS.IsNormal = 1 THEN RS.session_id END)
52+
,AvgCPUFactor = AVG(CASE WHEN RS.IsNormal = 1 THEN RS.CPUFactor END)
53+
,MaxCPUFactor = MAX(CASE WHEN RS.IsNormal = 1 THEN RS.CPUFactor END)
54+
,AvgReads = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.reads) END)
55+
,AvgWrites = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.writes) END)
56+
,AvgWaiting = AVG(CASE WHEN RS.IsNormal = 1 THEN CONVERT(bigint,RS.wait_time) END)
57+
--,AvgElapsed = AVG(CASE WHEN RS.IsNormal = 1 THEN RS.total_elapsed_time END)
58+
,AvgWorkers = AVG(CASE WHEN RS.IsNormal = 1 THEN RS.WorkerCount END)
59+
,MaxWorkers = MAX(CASE WHEN RS.IsNormal = 1 THEN RS.WorkerCount END)
60+
FROM
61+
(
62+
SELECT
63+
*
64+
,CASE WHEN isAdmin = 0 AND isForcedWait = 0 THEN 1 ELSE 0 END IsNormal
65+
,CASE
66+
DATEDIFF(SS,RS.start_time,CURRENT_TIMESTAMP) WHEN 0 THEN 0
67+
ELSE RS.cpu_time*100.00/DATEDIFF(SS,RS.start_time,CURRENT_TIMESTAMP)
68+
END as CPUFactor
69+
,CASE
70+
WHEN RS.total_elapsed_time > @highRunMS THEN 1
71+
ELSE 0
72+
END as HighRun
73+
,CASE
74+
WHEN W.WorkerCount > 1 THEN 1
75+
ELSE 0
76+
END IsParalell
77+
FROM
78+
(
79+
SELECT
80+
*
81+
,CASE
82+
WHEN R.command IN ('DBCC','DbccFilesCompact') THEN 1
83+
WHEN R.command LIKE '%BACKUP%' THEN 1
84+
ELSE 0
85+
END isAdmin
86+
,CASE
87+
WHEN R.command IN ('WAITFOR','BROKER_RECEIVE_WAITFOR') THEN 1
88+
WHEN R.command LIKE '%BACKUP%' THEN 1
89+
ELSE 0
90+
END isForcedWait
91+
FROM
92+
sys.dm_exec_requests R
93+
OUTER APPLY (
94+
SELECT
95+
COUNT(*) AS RealWaitsCount
96+
FROM
97+
sys.dm_os_waiting_tasks OT
98+
WHERE
99+
OT.session_id = R.session_id
100+
AND
101+
OT.wait_type NOT IN ('CXPACKET')
102+
) STA
103+
WHERE
104+
R.session_id > 50
105+
AND
106+
R.session_id != @@SPID
107+
) RS
108+
OUTER APPLY
109+
(
110+
SELECT
111+
COUNT(W.worker_address) as WorkerCount
112+
FROM
113+
sys.dm_os_tasks T WITH(nolock)
114+
INNER JOIN
115+
sys.dm_os_workers W WITH(nolock)
116+
ON W.worker_address = T.worker_address
117+
WHERE
118+
T.session_id = RS.session_id
119+
AND
120+
T.request_id = RS.request_id
121+
) W
122+
) RS
123+
) R
124+
CROSS JOIN
125+
(
126+
SELECT
127+
S_MediaCPU = AVG(last_worker_time/1000.00)
128+
,S_MediaTempo = AVG(last_elapsed_time/1000.00)
129+
,S_LastExecuted = MAX(last_execution_time)
130+
,S_QtdQueries = COUNT(*)
131+
FROM
132+
sys.dm_exec_query_Stats
133+
WHERE
134+
last_execution_time > DATEADD(SS,-ISNULL(@LastQueryTime,1),CURRENT_TIMESTAMP)
135+
) QS
136+
CROSS JOIN
137+
(
138+
SELECT
139+
IODisk_Pending = COUNT(CASE WHEN IOR.TYPE = 'disk' THEN IOR.IOID END)
140+
,IODisk_AvgPending = AVG(CASE WHEN IOR.TYPE = 'disk' THEN IOR.WAIT END)
141+
,IODisk_Oldest = MAX(CASE WHEN IOR.TYPE = 'disk' THEN IOR.WAIT END)
142+
,IODisk_Highs = COUNT(CASE WHEN IOR.TYPE = 'disk' AND IOR.IsHigh = 1 THEN IOR.IOID END)
143+
,IONet_Pending = COUNT(CASE WHEN IOR.TYPE = 'network' THEN IOR.IOID END)
144+
,IONet_Oldest = MAX(CASE WHEN IOR.TYPE = 'network' THEN IOR.WAIT END)
145+
,IONet_AvgPending = AVG(CASE WHEN IOR.TYPE = 'network' THEN IOR.WAIT END)
146+
,IONet_Highs = COUNT(CASE WHEN IOR.TYPE = 'network' AND IOR.IsHigh = 1 THEN IOR.IOID END)
147+
FROM
148+
(
149+
SELECT
150+
IOR.*
151+
,IOR.io_completion_request_address as IOID
152+
,IOR.io_pending_ms_ticks as WAIT
153+
,IOR.io_type as TYPE
154+
,CASE
155+
WHEN IOR.io_pending_ms_ticks > @highDisk THEN 1
156+
WHEN IOR.io_pending_ms_ticks > @highNet THEN 1
157+
ELSE 0
158+
END IsHigh
159+
FROM
160+
sys.dm_io_pending_io_requests IOR
161+
) IOR
162+
) IOS
163+
CROSS JOIN
164+
(
165+
SELECT
166+
SUM(work_queue_count) as PendingTasks
167+
,SUM(runnable_tasks_count) as RunnableTasks
168+
FROM
169+
sys.dm_os_schedulers S
170+
WHERE
171+
S.status = 'VISIBLE ONLINE'
172+
) SDLS
173+
CROSS JOIN
174+
(
175+
SELECT
176+
COUNT(CASE WHEN W.task_address IS NOT NULL THEN W.worker_address END) as BusyWorkers
177+
FROM
178+
sys.dm_os_workers W
179+
) WS
180+
CROSS JOIN
181+
(
182+
SELECT
183+
SI.max_workers_count as MaxWorkersCount
184+
FROM
185+
sys.dm_os_sys_info SI
186+
) SI
187+
OPTION(RECOMPILE) --> Avoids caching of this query

0 commit comments

Comments
 (0)