| |
 |
|
Display SQL Server RAM memory buffer
cache
SQL Server Tips by Burleson Consulting |
This is one of the many SQL
Server scripts to display SQL Server RAM memory from the book "High
Performance SQL Server DBA".SQL Server has
offered dynamic memory management since version 7.0. This means the DBA can
turn loose the reins and let SQL Server determine workload demands and adjust
memory accordingly, with one eye always on overall server memory utilization, in
order to keep performance high. The basic memory regions SQL Server uses are
the database or buffer cache , the plan cache , referred to in older versions as
the procedure cache , and workspace memory .
The database cache holds 8KB pages that contain database
information. SQL Server attempts to eliminate seldom-used pages from the
database cache so room is left for often-referenced data.
As has already been mentioned, the plan cache holds compiled
and executable plans for both code objects (procedures, etc.) and ad-hoc
queries.
Workspace memory is sometimes required for database requests
that require hashing or sorting operations. There are also miscellaneous areas
of memory used for locking and such.
Before looking at the various memory efficiency ratios, it
is a good idea to understand how much memory SQL Server has allocated across the
various memory regions. The up_memory_status
procedure below works for SQL Server 2000 and 2005 and gives a quick overview of
all primary memory allotments:
IF
OBJECT_ID('up_memory_status') IS NOT NULL
BEGIN
DROP PROCEDURE up_memory_status
IF OBJECT_ID('up_memory_status') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE up_memory_status >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE up_memory_status >>>'
END
go
create procedure up_memory_status
AS
set nocount on
declare @total_memory_kb float,
@low int,
@proc_cache_kb float,
@buffer_cache_used_kb float,
@proc_cache_used_kb float,
@buffer_cache_free_kb float
select
@total_memory_kb = cntr_value
from
master..sysperfinfo
where
object_name like '%Memory Manager%' and
counter_name = 'Total Server Memory (KB)'
select
@low = low
from
master..spt_values
where
number = 1 and
type = 'E'
select
@proc_cache_kb = (cntr_value * @low) / 1024
from
master..sysperfinfo
where
object_name like '%Buffer Manager%' and
counter_name = 'Procedure cache pages'
select
@buffer_cache_free_kb = (cntr_value * @low) / 1024
from
master..sysperfinfo
where
object_name like '%Buffer Manager%' and
counter_name = 'Free pages'
create table #proccache (
label varchar(50),
counter float)
insert into #proccache
exec ('DBCC MEMORYSTATUS')
select
@proc_cache_used_kb = (isnull(counter,0) * @low) / 1024
from
#proccache
See
code depot for full script
@buffer_cache_used_kb = @total_memory_kb -
@proc_cache_kb -
@buffer_cache_free_kb -
(select
sum(cntr_value)
from
master..sysperfinfo
where
object_name like '%Memory Manager%' and
counter_name in ('Connection Memory (KB)','Granted Workspace
Memory (KB)',
'Lock Memory (KB)','Optimizer Memory (KB)',
'SQL Cache Memory (KB)'))
drop table #proccache
select
total_sql_server_memory_kb =
@total_memory_kb,
buffer_cache_kb =
@buffer_cache_used_kb + @buffer_cache_free_kb,
procedure_cache_kb =
@proc_cache_kb,
buffer_cache_used_kb =
@buffer_cache_used_kb,
procedure_cache_used_kb =
@proc_cache_used_kb,
total_cache_used_kb =
@buffer_cache_used_kb + @proc_cache_used_kb,
percent_procedure_cache_used =
convert(decimal(5,2),100 * @proc_cache_used_kb / @proc_cache_kb),
percent_buffer_cache_used =
convert(decimal(5,2),100 * (@buffer_cache_used_kb) /
(@buffer_cache_used_kb + @buffer_cache_free_kb))
-- show miscellaneous memory regions
select
counter_name,
cntr_value
from
master..sysperfinfo
where
object_name like '%Memory Manager%' and
counter_name in ('Connection Memory (KB)','Granted Workspace Memory
(KB)',
'Lock Memory (KB)','Optimizer Memory (KB)',
'SQL Cache Memory (KB)')
order by
1
go
IF OBJECT_ID('up_memory_status') IS NOT NULL
PRINT '<<< CREATED PROCEDURE up_memory_status >>>'
ELSE
PRINT '<<< FAILED CREATING PROCEDURE up_memory_status >>>'
go
Once the amount of memory SQL Server has allocated for use
has been revealed, the DBA can then move on to looking at various memory
efficiency ratios. The up_ratios_memory
procedure should work on all versions of SQL Server. It returns the buffer cache
, procedure plan, ad-hoc SQL, and log cache hit ratios:
IF
OBJECT_ID('up_ratio_memory') IS NOT NULL
BEGIN
DROP PROCEDURE up_ratio_memory
IF OBJECT_ID('up_ratio_memory') IS NOT NULL
PRINT '<<< FAILED DROPPING PROCEDURE up_ratio_memory >>>'
ELSE
PRINT '<<< DROPPED PROCEDURE up_ratio_memory >>>'
END
go
create procedure up_ratio_memory
AS
set nocount on
-- buffer cache hit ratio
select
buffer_cache_hit_ratio =
convert(decimal(15,2),
(t1.cntr_value*1.0/t2.cntr_value*1.0 *100.0))
from
master.dbo.sysperfinfo t1,
master.dbo.sysperfinfo t2
where
t1.object_name = 'SQLServer:Buffer Manager' and
t2.object_name = 'SQLServer:Buffer Manager' and
See
code depot for full script
-- procedure plan hit ratio
select
procedure_plan_hit_ratio =
convert(decimal(15,2),(t1.cntr_value*1.0/t2.cntr_value*1.0)*100.0)
from
master.dbo.sysperfinfo t1,
master.dbo.sysperfinfo t2
where
t1.object_name = 'SQLServer:Cache Manager' and
t2.object_name = 'SQLServer:Cache Manager' and
t1.counter_name = 'Cache Hit Ratio' and
t2.counter_name = 'Cache Hit Ratio Base' and
t1.instance_name = t2.instance_name and
t2.cntr_value > 0 and
t1.instance_name in ('Procedure Plans')
-- ad hoc sql hit ratio
select
ad_hoc_sql_hit_ratio =
convert(decimal(15,2),(t1.cntr_value*1.0/t2.cntr_value*1.0)*100.0)
from
master.dbo.sysperfinfo t1,
master.dbo.sysperfinfo t2
where
t1.object_name = 'SQLServer:Cache Manager' and
t2.object_name = 'SQLServer:Cache Manager' and
t1.counter_name = 'Cache Hit Ratio' and
t2.counter_name = 'Cache Hit Ratio Base' and
t1.instance_name = t2.instance_name and
t2.cntr_value > 0 and
t1.instance_name in ('Adhoc Sql Plans')
-- log cache hit ratio
select
log_cache_hit_ratio =
isnull(convert(decimal(15,2),100 *
(sum(t1.cntr_value)*1.0/sum(t2.cntr_value)*1.0)),0)
from
master.dbo.sysperfinfo t1,
master.dbo.sysperfinfo t2
where
See
code depot for full script
go
IF OBJECT_ID('up_ratio_memory') IS NOT NULL
PRINT '<<< CREATED PROCEDURE up_ratio_memory >>>'
ELSE
PRINT '<<< FAILED CREATING PROCEDURE up_ratio_memory >>>'
go
To help ensure excellent performance, the buffer cache hit
ratio should be maintained in the neighborhood of 90% or higher. However, one
should be aware that every server has its own personality and might exhibit
excellent performance with below average readings for the cache hit ratio. One
should also be aware that excessive logical I/O activity can produce a very high
cache hit ratio while actually degrading overall database performance, so a high
buffer cache hit ratio is not the silver bullet for overall high performance in
SQL Server.
If the DBA is seeing low readings for the buffer cache hit
ratio, the Page Life Expectancy statistic should be checked. This statistic
indicates the length of time SQL Server estimates a page will remain in the
buffer cache. Obviously, pages served from memory result in much shorter
response times than pages that must be read from disk and then into the cache.
So, it is wise for often used data to be pinned in the buffer cache. The
page_life
query easily provides the DBA with
this measure:
This is one of the many SQL Server
scripts to display SQL Server RAM memory from the book "High
Performance SQL Server DBA".
|

|
|