sys.dm_exec_procedure_statsおよびsys.dm_exec_query_stats動的管理ビューを使用することもできます。それらの最初のものは、手順全体に関する情報を提供します。2番目は、プロシージャ内の各クエリを分割するために使用できます。以下に例を示します。
USE AdventureWorks;
GO
CREATE PROCEDURE dbo.Test
@NameLike nvarchar(50)
AS
BEGIN
SELECT
ProductCount = COUNT_BIG(*)
FROM Production.Product AS p
JOIN Production.TransactionHistory AS th ON
th.ProductID = p.ProductID
WHERE
p.Name LIKE @NameLike;
SELECT
pc.Name,
ProductCount = COUNT_BIG(*)
FROM Production.Product AS p
JOIN Production.ProductSubcategory AS ps ON
ps.ProductSubcategoryID = p.ProductSubcategoryID
JOIN Production.ProductCategory AS pc ON
pc.ProductCategoryID = ps.ProductCategoryID
WHERE
p.Name LIKE @NameLike
GROUP BY
pc.Name
ORDER BY
pc.Name;
END;
GO
EXECUTE dbo.Test @NameLike = N'A%';
EXECUTE dbo.Test @NameLike = N'F%';
手順の統計:
SELECT
deps.last_execution_time,
deps.last_worker_time,
deps.last_physical_reads,
deps.last_logical_writes,
deps.last_logical_reads,
deps.last_elapsed_time
FROM sys.dm_exec_procedure_stats AS deps
WHERE
deps.database_id = DB_ID()
AND deps.[object_id] = OBJECT_ID(N'dbo.Test', N'P');
プロシージャ内のクエリ:
SELECT
query.the_text,
deqs.last_execution_time,
deqs.last_worker_time,
deqs.last_physical_reads,
deqs.last_logical_writes,
deqs.last_logical_reads,
deqs.last_clr_time,
deqs.last_elapsed_time,
deqs.last_rows -- note: Only present from 2008 R2 onwards
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.[sql_handle]) AS dest
CROSS APPLY
(
VALUES
(
SUBSTRING
(
dest.[text],
deqs.statement_start_offset / 2 + 1,
(ISNULL(NULLIF(deqs.statement_end_offset, -1), DATALENGTH(dest.[text])) - deqs.statement_start_offset) / 2 + 1
)
)
) AS query (the_text)
WHERE
deqs.[sql_handle] IN
(
SELECT
deps.[sql_handle]
FROM sys.dm_exec_procedure_stats AS deps
WHERE
deps.database_id = DB_ID()
AND deps.[object_id] = OBJECT_ID(N'dbo.Test', N'P')
);
Duration
そしてCPU
結果列は推定ではなく実際です、そうですか?