SET STATISTICS TIME (Transact-SQL)

適用於:SQL ServerAzure SQL DatabaseAzure SQL 受控執行個體

顯示剖析、編譯和執行每個陳述式所需要的毫秒數。

Transact-SQL 語法慣例

Syntax

  
SET STATISTICS TIME { ON | OFF }  

注意

若要檢視 SQL Server 2014 (12.x) 和舊版的 Transact-SQL 語法,請參閱 舊版檔

備註

當 SET STATISTICS TIME 是 ON 時,會顯示陳述式的時間統計資料。 當它是 OFF 時,不會顯示時間統計資料。

SET STATISTICS TIME 的設定是在執行階段進行設定,而不是在剖析階段進行設定。

當您啟用 [輕量型共用] 設定選項時,會啟用 Fiber 模式,但在 Fiber 模式下,Microsoft SQL Server 無法提供準確的統計資料。

只有在使用 SET STATISTICS TIME ON 來執行查詢時,才會更新 sysprocesses 資料表的 cpu 資料行。 當 SET STATISTICS TIME 是 OFF 時,會傳回 0

ON 和 OFF 設定也會影響 SQL Server Management Studio 中目前活動的 [處理序資訊] 檢視中的 CPU 資料行。

權限

若要使用 SET STATISTICS TIME,使用者必須有執行 Transact-SQL 陳述式的適當權限。 不需要 SHOWPLAN 權限。

範例

這個範例會顯示伺服器的執行、剖析和編譯階段。

USE AdventureWorks2022;  
GO         
SET STATISTICS TIME ON;  
GO  
SELECT ProductID, StartDate, EndDate, StandardCost   
FROM Production.ProductCostHistory  
WHERE StandardCost < 500.00;  
GO  
SET STATISTICS TIME OFF;  
GO  

以下為結果集:

SQL Server parse and compile time:   
   CPU time = 0 ms, elapsed time = 1 ms.  
SQL Server parse and compile time:   
   CPU time = 0 ms, elapsed time = 1 ms.  
  
(269 row(s) affected)  
  
SQL Server Execution Times:  
   CPU time = 0 ms,  elapsed time = 2 ms.  
SQL Server parse and compile time:   
   CPU time = 0 ms, elapsed time = 1 ms.  
  

另請參閱

SET 陳述式 (Transact-SQL)
SET STATISTICS IO (Transact-SQL)