SQL Server: Performance Tuning (Understanding Set Statistics Time output)
In the last post we have discussed about Set Statistics IO and how it will help us in the performance tuning. In this post we will discuss about the Set Statistics Time which will give the statistics of time taken to execute a query.Let us start with a example.
USE AdventureWorks2008
GO
DBCC dropcleanbuffers
DBCC freeproccache
GO
SET STATISTICS TIME ON
GO
SELECT *
FROM Sales.SalesOrderHeader SOH INNER JOIN Sales.SalesOrderDetail SOD ON
SOH.SalesOrderID=SOD.SalesOrderID
WHERE ProductID BETWEEN 700
AND 800
GO
SELECT *
FROM Sales.SalesOrderHeader SOH INNER JOIN Sales.SalesOrderDetail SOD ON
SOH.SalesOrderID=SOD.SalesOrderID
WHERE ProductID BETWEEN 700
AND 800
There aretwo select statement in the example .The first one is executed after clearing the buffer. Let us look into the output.
SQL Server parse and Compile time : When we submit a query to SQL server to execute,it has to parse and compile for any syntax error and optimizer has to produce the optimal plan for the execution. SQL Server parse and Compile time refers to the time taken to complete this pre -execute steps.If you look into the output of second execution, the CPU time and elapsed time are 0 in the SQL Server parse and Compile time section. That shows that SQL server did not spend any time in parsing and compiling the query as the execution plan was readily available in the cache. CPU time refers to the actual time spend on CPU and elapsed time refers to the total time taken for the completion of the parse and compile. The difference between the CPU time and elapsed time might wait time in the queue to get the CPU cycle or it was waiting for the IO completion. This does not have much significance in performance tuning as the value will vary from execution to execution. If you are getting consistent value in this section, probably you will be running the procedure with recompile option.
SQL Server Execution Time: This refers to the time taken by SQL server to complete the execution of the compiled plan. CPU time refers to the actual time spend on CPU where as the elapsed time is the total time to complete the execution which includes signal wait time, wait time to complete the IO operation and time taken to transfer the output to the client.The CPU time can be used to baseline the performance tuning. This value will not vary much from execution to execution unless you modify the query or data. The load on the server will not impact much on this value. Please note that time shown is in milliseconds. The value of CPU time might vary from execution to execution for the same query with same data but it will be only in 100's which is only part of a second. The elapsed time will depend on many factor, like load on the server, IO load ,network bandwidth between server and client. So always use the CPU time as baseline while doing the performance tuning.