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

performance

The document contains SQL code for creating a temporary table named #PerformanceMon to store various performance metrics related to SQL Server. It includes commands to insert initial blank values, update the table with real-time performance data such as Buffer Cache Hit Ratio, Free Pages, RAM, SQL Memory, and other metrics. Finally, it selects the data from the table and drops the temporary table after use.

Uploaded by

Murali Pujari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

performance

The document contains SQL code for creating a temporary table named #PerformanceMon to store various performance metrics related to SQL Server. It includes commands to insert initial blank values, update the table with real-time performance data such as Buffer Cache Hit Ratio, Free Pages, RAM, SQL Memory, and other metrics. Finally, it selects the data from the table and drops the temporary table after use.

Uploaded by

Murali Pujari
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

create table #PerformanceMon

(Date datetime, CashHitRatio numeric(25,2), FreePages numeric(20),RAM_in_GB


numeric(25,2),SQLMemory_in_GB numeric(20,3),
Procedure_cache numeric(20,3),Single_used numeric(20,3), Page_Life_Expectancy
numeric(20), UserConnection numeric(20), DeadLocks numeric(10), Reads numeric(25),
Writes numeric(25), TotalSession numeric(10))

--Create table HitRatio


--create table HitRatio
--(Date datetime, CashHitRatio numeric(25,2), FreePages numeric(20), SQLMemory
numeric(20), UserConnection numeric(20), DeadLocks numeric(10), Reads numeric(25),
Writes numeric(25), TotalSession numeric(10))

--Insert blank value in #PerformanceMon Table


insert into #PerformanceMon (Date, CashHitRatio, FreePages ,RAM_in_GB ,
SQLMemory_in_GB ,Procedure_cache ,Single_used , Page_Life_Expectancy,UserConnection
, DeadLocks , Reads , Writes , TotalSession)
values(0,0,0,0,0,0,0,0,0,0,0,0,0)

set nocount on
--Date
update #PerformanceMon set Date= (SELECT GETDATE())

--Buffer Cahe Hit Ratio


declare @value numeric(25, 2), @basevalue numeric(25, 2)
declare @Cachehitratio numeric(25, 2)
set @Cachehitratio =0
SELECT @value = cntr_value FROM sys.dm_os_performance_counters (nolock) WHERE
counter_name = 'Buffer cache hit ratio'
SELECT @basevalue = cntr_value FROM sys.dm_os_performance_counters (nolock) WHERE
counter_name = 'Buffer cache hit ratio base'
set @Cachehitratio= convert(varchar(100), (@value / @basevalue) *100)
update #PerformanceMon set CashHitRatio= @Cachehitratio

--Free Pages
update #PerformanceMon set FreePages=
(select cntr_value FROM sys.dm_os_performance_counters (nolock) WHERE counter_name
Like '%Free pages%' and
object_name like '%Buffer Manager%')

--RAM
update #PerformanceMon set RAM_in_GB=
(select cast(physical_memory_in_bytes as numeric)/1024/1024/1024 RAM_in_MB from
sys.dm_os_sys_info(nolock))

--SQL Memory
update #PerformanceMon set SQLMemory_in_GB=
(select sum(cast (cntr_value as numeric))/1024/1024 from
sys.dm_os_performance_counters (nolock) where object_name like '%Memory Manager%'
and
counter_name ='Total Server Memory (KB)')

--procedure CAche Size


update #PerformanceMon set Procedure_cache=
(select cntr_value/128 Procedure_cache from sys.dm_os_performance_counters
where object_name like '%Plan%'
and instance_name like '%_Total%'
and counter_name like '%Cache Pages%')

-- Gives size of single used cache plans


update #PerformanceMon set Single_used=
(select sum(size_in_bytes)/1024/1024
FROM sys.dm_exec_cached_plans
where usecounts=1 and objtype in ('Adhoc','Prepared'))

--Page Life Expectancy

update #PerformanceMon set Page_Life_Expectancy=


(select cast(cntr_value as numeric) from sys.dm_os_performance_counters
where
object_name like '%Buffer Manager%' and
counter_name like '%Page life expectancy%'
)

--User Connections
update #PerformanceMon set UserConnection=
(select cntr_value from sys.dm_os_performance_counters (nolock) where
object_name like '%General Statistics%'
and counter_name ='User Connections')

--Deadlocks
update #PerformanceMon set DeadLocks=
(select cntr_value from sys.dm_os_performance_counters (nolock) where object_name
like '%Locks%'
and counter_name ='Number of Deadlocks/sec' and instance_name='Database')

--Read
Update #PerformanceMon set Reads=
(select cntr_value from sys.dm_os_performance_counters (nolock) where object_name
like '%Buffer Manager%'
and counter_name='Page reads/sec')

--Writes
Update #PerformanceMon set Writes=
(select cntr_value from sys.dm_os_performance_counters (nolock) where object_name
like '%Buffer Manager%'
and counter_name='Page writes/sec')

--TotalSession
Update #PerformanceMon set TotalSession=
(select count(*) from sys.sysprocesses)

--Insert values from #PerformanceMon table to HitRatio table


--insert into HitRatio select * from performancemon
select * from #PerformanceMon

drop table #PerformanceMon

You might also like