Summarizing data in the DMV’s Part 3
Sunday, December 31st, 2006In part 2 of this series, we looked at applying the query pattern concept to the procedure cache. This allows us to aggregate “semi” historical data to help guide us in the best direction in our tuning efforts. Sometimes we would want to do something similar with DMV’s that contain real time data.
We will be looking at summarizing data from the DMV, sys.dm_os_memory_grants over time. In SQL 2000 this information was only available through the integer column of the DOP event class. You can still do this in 2005. It is not easy. If you have a large amount of data, set aside hours for analysis. Anything is possible but I can think of a way to automate this easily.
We can go into a WHILE loop with a WAITFOR and monitor sys.dm_os_memory_grants for a period of time to get a trend. This can be huge compared to just eyeballing this DMV. First, let’s eyeball it. Keep in mind, it may not even return any rows.
--Find queries that have granted memory or waiting on memory.
--An xml showplan is important so we can see the cause of large memory needs like hash or sorts.
select text, query_plan, granted_memory_kb, used_memory_kb, wait_time_ms
from sys.dm_exec_query_memory_grants mg
CROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) t
CROSS APPLY sys.dm_exec_query_plan(mg.plan_handle) q
Now let’s look at the query pattern method. This would be use full in optimizing the memory subsystem or in trying to resolve memory grant and resource semaphore waits.
--Now let's create the work table.
--Create work table
CREATE TABLE [dbo].[#mg_sig](
[sig] [nvarchar](max) COLLATE Latin1_General_CI_AI NULL,
[text] [nvarchar](max) COLLATE Latin1_General_CI_AI NULL,
[query_plan] [xml] NULL,
[granted_memory_kb] [bigint] NULL,
[used_memory_kb] [bigint] NULL,
[wait_time_ms] [bigint] NULL,
[csum] [bigint] NULL
)
--begin loop
--Arbitrary @ctr and delay. Adjust as you see fit.
declare @ctr int
select @ctr = 100
while @ctr > 0
begin
insert into #mg_sig (sig, text, query_plan, granted_memory_kb, used_memory_kb, wait_time_ms, csum)
select dbo.fn_SQLSigTSQL(text, 4000) as sig
, text
, query_plan
, granted_memory_kb
, used_memory_kb
, wait_time_ms
, checksum(dbo.fn_SQLSigTSQL(text, 4000)) as csum
from sys.dm_exec_query_memory_grants mg
CROSS APPLY sys.dm_exec_sql_text(mg.sql_handle) t
CROSS APPLY sys.dm_exec_query_plan(mg.plan_handle)
select @ctr = @ctr-1
waitfor delay '00:00:30'
end
--Now let's roll it up and include the xml plan
select dt1.sig, dt1.total_granted_memory_kb, dt1.total_wait_time_ms, convert(xml,dt2.query_plan)
from
(
select iq.sig
, iq.csum
, sum(iq.granted_memory_kb) as total_granted_memory_kb
, sum(iq.wait_time_ms) as total_wait_time_ms
from #mg_sig as iq
group by iq.sig, iq.csum
) as dt1
inner join
(
select s2.csum, convert(varchar(max),query_plan) as query_plan
from #mg_sig s2
group by csum, convert(varchar(max),query_plan)
) as dt2
on dt1.csum = dt2.csum
Note: This is a bug with the xml datatype that is supposed to be resolved in SP2 so sometimes you may get an error.
