?
Solved

analyze query activity over time

Posted on 2012-04-12
5
Medium Priority
?
533 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 549 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 546 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

809 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