Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Identifying long running queries SQL server Expand / Collapse
Author
Message
Posted Thursday, January 30, 2014 5:01 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 5:23 PM
Points: 4,049, Visits: 9,214
Reviving a thread twice


Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1536598
Posted Wednesday, February 12, 2014 5:44 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 5:24 AM
Points: 828, Visits: 1,439
James,

We are getting nulls for object_name and database_name

Can you please advise on this
Post #1540672
Posted Wednesday, February 12, 2014 6:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:26 AM
Points: 14,205, Visits: 28,536
A NULL for the Object name would just imply that you're seeing ad hoc or prepared queries that don't have an object because they're not being called by a stored procedure. Instead it's just a query batch being passed in. That just makes sense. You can read more about it in the documentation in the Books Online. The db_id is null because a sql_handle can't uniquely identify which database a query came from, so they don't store that information. Read about it in this Connect item.

In short, you're seeing normal behavior.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1540687
Posted Wednesday, February 12, 2014 8:04 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 5:24 AM
Points: 828, Visits: 1,439
hi grant,

I tried with the below query using the reference

http://msdn.microsoft.com/en-us/library/ms189472.aspx


SELECT qs.plan_handle, pvt.sql_handle,pvt.dbid,
(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,
qs.execution_count AS [Execution Count],
qs.total_elapsed_time/(1000*1000) as [Total Time In Secs],
qs.total_elapsed_time/1000 as [Total Time In Millisecs],
qs.total_elapsed_time / (1000 * qs.execution_count) as [Avg Resp Time in Millisecs],
qs.max_elapsed_time / (1000) as [Max Resp Time in Millisecs],
qs.min_elapsed_time / (1000) as [Min Resp Time in Millisecs],
qs.total_elapsed_time / qs.execution_count / 1000000.0 AS average_seconds,
qs.total_elapsed_time / 1000000.0 AS total_seconds,
qs.last_execution_time [Last Executed At]
FROM (
SELECT plan_handle, epa.attribute, epa.value
FROM sys.dm_exec_cached_plans
OUTER APPLY sys.dm_exec_plan_attributes(plan_handle) AS epa
WHERE cacheobjtype = 'Compiled Plan') AS ecpa
PIVOT (MAX(ecpa.value) FOR ecpa.attribute IN ("dbid", "sql_handle")) AS pvt
inner join sys.dm_exec_query_stats AS qs on qs.sql_handle =pvt.sql_handle
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS s2
where pvt.dbid =DB_ID()
GO


But sometime, some queries are appearing for two different databases
Post #1540755
Posted Wednesday, February 12, 2014 8:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:26 AM
Points: 14,205, Visits: 28,536
It's possible the query is being run from more than one database.

The real problem is, this is an imprecise mechanism for identifying long running queries. It's completely dependent on the queries being in cache. They can age out of cache, or never go there if the query has a RECOMPILE hint. If you really, really have to have perfect measures of exactly which queries runs on exactly which database, this is not the way to go about it. Instead, capture query metrics using trace events.


----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1540799
Posted Thursday, February 13, 2014 1:15 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, December 16, 2014 5:24 AM
Points: 828, Visits: 1,439
Thanks grant
Post #1541055
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse