I want some Moore

Blog about stuff and things and stuff...
mostly about SQL server and .Net
posts - 161, comments - 1491, trackbacks - 33

My Links

SQLTeam.com Links

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer. I'm also a MCP and MCTS for SQL Server. 
Welcome to my blog.

Search this Blog
 

My Blog Feed via Email


Get your Google PageRank
Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

SQL Server 2005 profiling with Dynamic Management Views (DMV's)

DMV's are one of the most usefull features in SQL Server 2005.

There are 2 that I like more than others because they can give you information about
executed SQL statements without the use of SQL Profiler tool.
Of course they aren't a substitute for SQL profiler but rather an another tool you can use
in situations when you can't run SQL Profiler.
Those 2 DMV's are:
 - sys.dm_exec_query_stats
 - sys.dm_exec_sql_text
 
They provide much info about executed statements but i used them in this procedure for searching through
executed sql statemnts.
Since dm_exec_query_stats is based on cached query plans the row in it lasts until the plan is removed from cache.
So for better and more usefull data i put the results of each run into a separate log table.

this is the stored procedure:

create table StatementExecLog
(
    StatementText nvarchar(max),
    LastExecuted datetime,
    ElapsedTimeInSeconds int,
    ExecCountFromLastCompile int
)
go
create proc GetExecutedQueries
    @Filter varchar(8000),
    @DateFrom datetime = '19000101',
    @DateTo datetime = '99990101'    
as
select * from
(
SELECT    SUBSTRING(st.text, (qs.statement_start_offset/2)+1, 
        ((CASE qs.statement_end_offset
          WHEN -1 THEN DATALENGTH(st.text)
         ELSE qs.statement_end_offset
         END - qs.statement_start_offset)/2) + 1) AS StatementText,
        last_execution_time as LastExecuted,
        last_elapsed_time/1000000.0 as ElapsedTimeInSeconds,
        execution_count as ExecCountFromLastCompile
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
where last_execution_time between @DateFrom and @DateTo
) t
where StatementText like '%' + @Filter + '%'
order by LastExecuted desc
go
select * from person.address
go
-- for simple searches
exec GetExecutedQueries '.address'

-- for long term logging
insert into StatementExecLog
exec GetExecutedQueries '.address'

Print | posted on Thursday, September 21, 2006 10:00 AM

Feedback

# Bitacle Blog Search Archive - SQL Server 2005 profiling with Dynamic Management Views (DMV's)

[...]
DMV's are one of the most usefull features in SQL Server 2005. [...]
9/23/2006 4:56 AM | bitacle.org

# re: SQL Server 2005 profiling with Dynamic Management Views (DMV's)

I think your sample code above has a bug. SQL BOL says that 'last_elapsed_time' column is in microseconds. This means you need to divide by 1,000,000 and not 1,000 to derive the seconds.

Randy
10/20/2006 4:57 PM | Randy Minder

# re: SQL Server 2005 profiling with Dynamic Management Views (DMV's)

Thanx Randy for catching that.

I'm still so used to miliseconds from SS2k :)
10/20/2006 5:17 PM | Mladen

# re: SQL Server 2005 profiling with Dynamic Management Views (DMV's)

Come with small examples and then proceed with critical examples. Its hard to get the point...not satisfied.
3/24/2008 10:39 AM | Madhu

# re: SQL Server 2005 profiling with Dynamic Management Views (DMV's)

How do i eliminate the microseconds to seconds
10/8/2008 1:25 AM | Tutu

# re: SQL Server 2005 profiling with Dynamic Management Views (DMV's)

multiply them by 1.000.000??
10/8/2008 10:10 AM | Mladen

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 7 and type the answer here:

Powered by: