Solved

analyze query activity over time

Posted on 2012-04-12
5
512 Views
Last Modified: 2012-04-16
Looking for some ideas and/or approaches to analyze the queries, stored procedures etc that my database support department is running over time. My hope is to gain insight into the tables, rows, columns and stored procedures etc. being run to solve day to day data problems from our ticketing queue. For obvious reasons!!! this i believe will be helpful in targeting and eliminating root cause and thus reducing our load and customer facing issues that cause user frustration.

thanks,
dave
0
Comment
Question by:dthoms000
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 37838604
Hi Dave
What version of SQL Server are you using?
0
 
LVL 8

Accepted Solution

by:
venk_r earned 183 total points
ID: 37839606
1.create the table first  to store the data

CREATE TABLE [dbo].[longrunning](
      [session_id] [int] NULL,
      [start_time] [datetime] NULL,
      [status] [varchar](max) NULL,
      [cpu_time] [int] NULL,
      [Elapsed time(ms)] [int] NULL,
      [sql_statement] [nvarchar](max) NULL,
      [host_name] [varchar](100) NULL,
      [program_name] [varchar](100) NULL
) ON [PRIMARY]
 
2.Use the below sproc and set it up as job to run may be every 5 minutes or so.The query will record anything that goes beyond 2 secs. You can increase the time according to your need
 

CREATE procedure [dbo].[usp_longrunningqueries] --- Added Logic to ignore blocking by same SPID's which is identified as normal using parrellelism    
as    
begin    
 insert into longrunning(    
 session_id,  
 start_time,  
 status,  
 cpu_time,  
 [Elapsed time(ms)],  
 sql_statement,  
 host_name,  
 program_name)    
 SELECT  p.session_id, start_time, p.status,p.cpu_time,p.total_elapsed_time, --original_login_name,  
    (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 ,( (CASE WHEN statement_end_offset = -1  
    THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2)  
    ELSE statement_end_offset END)  - statement_start_offset) / 2+1))  AS sql_statement,host_name,program_name  --INTO longrunning  
 FROM sys.dm_exec_requests p  
 CROSS APPLY sys.dm_exec_sql_text(sql_handle)  s2 inner join sys.dm_exec_sessions s on s.session_id=p.session_id  
 AND p.total_elapsed_time > 2000--(2 sec)  

end
0
 

Author Comment

by:dthoms000
ID: 37840266
Nice idea venk_r... i'll try it and let you know thanks for taking the time to reply.


BCUNNEY... I am running 2008r2 on multiple servers supporting 100+ LARGE financial securities aggregation databases
0
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 182 total points
ID: 37841596
There are 3 'tracking' type areas of functionality in SQL Server 2008
1. Change Data Capture
http://blog.sqlauthority.com/2009/08/15/sql-server-introduction-to-change-data-capture-cdc-in-sql-server-2008/

2. Change Tracking
http://www.mssqltips.com/sqlservertip/1819/using-change-tracking-in-sql-server-2008/

3. SQL Server Audit
http://www.bradmcgehee.com/2010/03/an-introduction-to-sql-server-2008-audit/

These may be worth exploring as they may give some of the monitoring that you need to build up a picture of what users are doing.

I looked into SQL Audit myself a couple of months ago as a possibility for monitoring activity on a 'development' version of a Data Warehouse.
It seemed to be reasonably quick and straightforward to switch on and it gave nice 'log' type information which could be viewed just like any other SQL Server log.
0
 

Author Closing Comment

by:dthoms000
ID: 37853252
great pointers thanks both of you
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now