Solved

analyze query activity over time

Posted on 2012-04-12
5
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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