您可以获得现在正在执行的所有查询。
USE master;
SELECT st.text,
r.session_id,
r.status,
r.command,
r.cpu_time,
r.total_elapsed_time
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS st
您可以获得所有 50 个最后执行的查询
SELECT TOP 50
deqs.last_execution_time AS [Time],
dest.text AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC
我建议您使用 SQL Server Profiler,分析您的数据库并将结果保存在数据库中的一个表中,然后选择所有命令并按此分组进行分析。
Select *
From
(
Select
Convert(VarChar(Max) , PT.TextData ) As ExecutedCommand ,
COUNT( RowNumber ) As CommandCount
From ProfilerTable As PT (nolock)
Where
( PT.TextData Is Not Null )
And
( PT.EndTime Is Not Null )
Group By Convert( VarChar( Max ) , PT.TextData )
) As SubQuery
Order By SubQuery.CommandCount Desc