?
Solved

analyze query activity over time

Posted on 2012-04-12
5
Medium Priority
?
530 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 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

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.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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