STATISTICS IO and Time - Query Optimization in SQL Server
STATISTICS IO and STATISTICS TIME are used to measure the performance of a query in SQL Server.
STATISTICS IO shows the amount of I/O performed by SQL Server during the execution of a query. It can help identify which tables and indexes are being read the most and whether the query is performing optimally.
STATISTICS TIME displays the amount of time taken by the CPU to execute the query and the total time taken for the query to complete. It can help identify queries that are taking a long time to execute.
To use STATISTICS IO and STATISTICS TIME, you can turn on the options using the following command:
SET STATISTICS IO ON
SET STATISTICS TIME ON
Then, run your query as usual:
SELECT * FROM MyTable WHERE Column1 = 'Value1'
After the query has finished executing, the query execution plan will be displayed in the results pane. Along with the execution plan, you will also see the statistics for I/O and time.
Here's an example output of STATISTICS IO:
Table 'MyTable'. Scan count 1, logical reads 10, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
This output shows that the query scanned the table once and performed 10 logical reads. Logical reads represent the number of pages that SQL Server had to read from memory to satisfy the query. Physical reads, on the other hand, represent the number of pages that had to be read from disk because they were not in memory.
Here's an example output of STATISTICS TIME:
SQL Server Execution Times:
CPU time = 15 ms, elapsed time = 30 ms.
This output shows that the query took 15 milliseconds of CPU time to execute and 30 milliseconds of elapsed time. CPU time is the amount of time that the CPU spent executing the query, while elapsed time is the total time it took for the query to complete, including time spent waiting for I/O operations to complete.
You can also combine STATISTICS IO and STATISTICS TIME to get both sets of statistics at once:
Table 'MyTable'. Scan count 1, logical reads 10, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 15 ms, elapsed time = 30 ms.
In this example, the query scanned the table once and performed 10 logical reads. It took 15 milliseconds of CPU time to execute and 30 milliseconds of elapsed time.
These statistics can be very useful in identifying performance bottlenecks in queries and optimizing them for better performance.
Comments
Post a Comment