0% found this document useful (0 votes)
649 views

Blockings: Select Text Status

The document contains SQL queries to summarize blocking sessions, review blocking details, identify top queries by CPU usage, and monitor database growth, CPU usage, and connections. Key sections include queries to show blocking sessions, blocking query text, top queries by CPU, database growth from error logs, database CPU usage, and database connections by login.

Uploaded by

kalyanjipandey
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
649 views

Blockings: Select Text Status

The document contains SQL queries to summarize blocking sessions, review blocking details, identify top queries by CPU usage, and monitor database growth, CPU usage, and connections. Key sections include queries to show blocking sessions, blocking query text, top queries by CPU, database growth from error logs, database CPU usage, and database connections by login.

Uploaded by

kalyanjipandey
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Blockings

Select 'Total user session:', count (*) from Sysprocesses where spid>51
Union all
Select 'Total active session:', count(*) from Sysprocesses where status<>'Sleeping'
Union all
Select 'Total blocked session:', count(*) from Sysprocesses where blocked<>0
------------------------------------------------------------------------------------------------------------------------

Current running Queries

SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
----------------------------------------------------------------

SELECT spid 'SPID',


blocked 'Blocked By',
sp.waittype 'Wait Type',
sp.open_tran 'Open Trans',
sp.status 'Status',
sp.waittime 'Wait Time',
cmd 'Command',
sl.name 'Login',
program_name'Program'
FROM master..sysprocesses AS sp
JOIN master..syslogins AS sl ON sp.sid = sl.sid
WHERE (blocked != 0
and blocked != spid)
OR spid IN (SELECT blocked FROM master..sysprocesses WHERE blocked != 0
and blocked != spid)
ORDER BY blocked, spid

Review details and document anything unusual


Select loginname, programname, hostname, blocked from Sysprocesses where status<>’Sleeping’

Blocking Query Text

SELECT
r.session_id,
r.blocking_session_id,
s.program_name,
s.host_name,
t.text
FROM
sys.dm_exec_requests r
INNER JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE
s.is_user_process = 1 AND
r.session_id = SPID

-- For 2000

DBCC INPUTBUFFER(SPID)

No of Concurrent Users DB wise


SELECT db_name(dbid) as DatabaseName, count(dbid) as NoOfConnections,
loginame as LoginName
FROM sys.sysprocesses
WHERE dbid > 0
GROUP BY dbid, loginame

SIZE of DB

select Name,
(convert(float,size)) * (8192.0/1048576) File_Size,
(convert(float,fileproperty(name,'SpaceUsed'))) * (8192.0/1048576)
MB_Used,
((convert(float,size)) * (8192.0/1048576) -
(convert(float,fileproperty(name,'SpaceUsed'))) * (8192.0/1048576))
MB_Free
from sysfiles
order by
fileproperty(name,'IsLogFile')

Query executing for Top 20 At present.


select top 20
st.objectid, st.dbid, total_worker_time/execution_count AS
AverageCPUTime,
CASE statement_end_offset
WHEN -1 THEN st.text
ELSE
SUBSTRING(st.text,statement_start_offset/2,statement_end_offset/2)
END AS StatementText
from sys.dm_exec_query_stats qs CROSS APPLY
sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY AverageCPUTime DESC

CPU % for Current Time

set nocount on declare @ts_now bigint

select @ts_now = cpu_ticks /( cpu_ticks / ms_ticks )

/*cpu_ticks / convert(float, cpu_ticks_in_ms)*/ from sys.dm_os_sys_info

select top 1 record_id,dateadd(ms, -1 * (@ts_now - [timestamp]),


GetDate()) as EventTime,
SQLProcessUtilization,SystemIdle,100 - SystemIdle -
SQLProcessUtilization as OtherProcessUtilization

from (select record.value('(./Record/@id)[1]', 'int') as


record_id,

record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)
[1]', 'int') as SystemIdle,

record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtiliz
ation)[1]', 'int') as SQLProcessUtilization,

timestamp from (select timestamp, convert(xml, record) as


record

from sys.dm_os_ring_buffers

where ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'


and record like '%<SystemHealth>%') as x

) as y order by record_id desc

DB Wise CPU

Use <DB Name>


select db_name(database_id) DBName,sum(cpu_time) total_cpu
from sys.dm_exec_requests group by db_name(database_id)

Get DB Growth

Declare @OldestLog datetime,


     @FirstLog int,
     @SearchText nvarchar(50),
     @DBName sysname
Declare @ErrorLog Table (LogID int identity(1, 1) not null primary key,
                              LogDate datetime null,
                              ProcessInfo nvarchar(100) null,
                              LogText nvarchar(max) null)
Declare @EnumLogs Table (ArchiveNum int not null primary key,
                              ArcDate Datetime not null,
                              LogFileSize bigint not null)
 
Set nocount On
Set @OldestLog = '2/20/2011'
Set @SearchText = N'pages dumped: '
Set @DBName = 'KomodoFCB_Prod_V6'
 
Insert Into @EnumLogs
Exec master..xp_enumerrorlogs
 
Select Top 1 @FirstLog = ArchiveNum
From @EnumLogs
Where ArcDate < @OldestLog
Order By ArcDate DESC
 
If @FirstLog Is Null
  Begin
     Select Top 1 @FirstLog = ArchiveNum
     From @EnumLogs
     Order By ArchiveNum DESC
  End
 
While @FirstLog >= 0
  Begin
     Insert Into @ErrorLog (LogDate, ProcessInfo, LogText)
     Exec master..xp_readerrorlog @FirstLog
     Set @FirstLog = @FirstLog - 1
  End
 
Select Convert(varchar, LogDate, 101) As BUPDate,
Cast(Cast((Cast(RTrim(LTrim(SubString(LogText, CharIndex(@SearchText, LogText) +
Len(@SearchText), CharIndex(',', LogText, CharIndex(@SearchText, LogText)) -
CharIndex(@SearchText, LogText) - Len(@SearchText)))) as BigInt) * 8.0)/1024 As Decimal(9, 2)) As
varchar) + ' MB' As BUPSize
From @ErrorLog
Where CharIndex('Backup', ProcessInfo) > 0
And CharIndex('Database backed up. Database: ' + @DBName, LogText) > 0
Order By LogDate Asc
 
Set nocount Off

Get who is the owner of DB

You might also like