SELECT DB_NAME(st.dbid) DBName
,OBJECT_SCHEMA_NAME(objectid,st.dbid) SchemaName
,OBJECT_NAME(objectid,st.dbid) StoredProcedure
,max(cp.usecounts) execution_count
,sum(qs.total_elapsed_time) total_elapsed_time
,sum(qs.total_elapsed_time) / max(cp.usecounts) avg_elapsed_time
FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
join sys.dm_exec_cached_plans cp on qs.plan_handle = cp.plan_handle
where cp.objtype = 'proc'
group by DB_NAME(st.dbid),OBJECT_SCHEMA_NAME(objectid,st.dbid), OBJECT_NAME(objectid,st.dbid)
order by sum(qs.total_elapsed_time) desc
我在查询之上运行,但无法找到在数据库中创建的所有SP列表详细信息。 它只显示较少的SP。
请帮帮我。 等待你的宝贵回复。
答案 0 :(得分:1)
你可以尝试这样的事情,这个事情要复杂得多,但事实并非每个存储过程都会有统计数据:
CREATE TABLE #x(
database_id INT,
DatabaseName SYSNAME,
SchemaName SYSNAME,
ProcedureName SYSNAME,
[object_id] INT);
DECLARE @sql NVARCHAR(MAX) = '';
SELECT
@sql = @sql + N'INSERT INTO #x SELECT ' + CONVERT(VARCHAR(50), d.database_id) + ', ''' + name + ''', s.name, p.name, p.[object_id]
FROM ' + QUOTENAME(d.name) + '.sys.schemas AS s
INNER JOIN ' + QUOTENAME(d.name) + '.sys.procedures AS p ON p.schema_id = s.schema_id;' FROM sys.databases d WHERE d.database_id > 4;
EXEC sp_executesql @sql;
WITH PlanData AS (
SELECT
st.[dbid] AS database_id,
st.objectid AS [object_id],
DB_NAME(st.[dbid]) AS DBName,
OBJECT_SCHEMA_NAME(st.objectid, st.[dbid]) AS SchemaName,
OBJECT_NAME(st.objectid, st.[dbid]) AS StoredProcedure,
MAX(cp.usecounts) AS execution_count,
SUM(qs.total_elapsed_time) AS total_elapsed_time,
SUM(qs.total_elapsed_time) / MAX(cp.usecounts) AS avg_elapsed_time
FROM
sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle) st
LEFT JOIN sys.dm_exec_cached_plans cp on cp.plan_handle = qs.plan_handle
WHERE
cp.objtype = 'PROC'
GROUP BY
st.[dbid],
st.objectid,
DB_NAME(st.[dbid]),
OBJECT_SCHEMA_NAME(st.objectid, st.[dbid]),
OBJECT_NAME(st.objectid, st.[dbid]))
SELECT
x.DatabaseName,
x.SchemaName,
x.ProcedureName,
pd.execution_count,
pd.total_elapsed_time,
pd.avg_elapsed_time
FROM
#x x
LEFT JOIN PlanData pd ON pd.database_id = x.database_id AND pd.[object_id] = x.[object_id];
DROP TABLE #x;
从MSDN关于sys.dm_exec_query_stats,“视图在缓存计划中每个查询语句包含一行,并且行的生命周期与计划本身相关联。当从缓存中删除计划时,相应的行是从这种观点中消除了。“