Solved

analyze query activity over time

Posted on 2012-04-12
5
517 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

679 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