Professional Documents
Culture Documents
Join Search
MSSQLTips
By: Eric Blinn | Updated: 2020-09-30 | Comments | Related: More > Performance Tuning
Problem
I am working to improve the performance of a piece of SQL Server T-SQL code and I'm just not sure much the changes I am making are
helping or hurting. I need to be able to accurately track and report the performance improvement of my code changes.
Solution
The obvious answer is to look at the SQL Server query execution time, but that alone isn't always enough to determine that there is or
isn't an improvement – or if that new index is helping. If the query was already finishing in a second or 2 that just isn't enough precision to
determine if there is improvement. There are other things like caching and parallelism that can cause query execution time to be a
misleading statistic.
This tip will look at how to analyze query executions to determine how many system resources were used and how long the query took to
execute with far more precision than the query execution timer in SSMS so that a more definitive answer can be determined about
whether – and to what extent – query optimization was changed.
All of the demos in this tip will use the WideWorldImporters sample database which can be downloaded from here.
SELECT *
FROM [WideWorldImporters].[Sales].[Invoices]
WHERE InvoiceDate='2014-03-15';
Recommended Reading
How to find out how much CPU a SQL Server
process is really using
The answer is that SQL Server offers a method to get execution times measured at the millisecond level which is 1000x more precise
than the timer on the bottom right of the query results pane in SSMS. The method is called STATISTICS TIME and can be enabled in one
of 2 ways.
In SSMS the setting can be enabled on the session by right-clicking white space in the query pane and choosing query options. It's all
the way at the bottom of the context menu. When the menu comes up choose "Advanced" on the left then check the box next to SET
STATISTICS TIME.
The best part about this method is that it is not limited to SSMS connections. This feature can be enabled on SSMS or any SQL Server
session by running this simple command.
Now that the feature is enabled the query can be executed again and it will include additional output. The output of STATISTICS TIME
can be found on the messages tab in SSMS. To read this information in other applications the connection must be able to read the
InfoMessage event. It can be read in PowerShell by using the - verbose option of the invoke-sqlcmd cmdlet. The output from the
messages tab of SSMS is below.
Focus on the "SQL Server Execution Times:" section. The output shows that this query executed in 19 milliseconds. Running it
repeatedly showed numbers between 12 and 30 milliseconds on this laptop. With this level of precision measuring improvement will be
far easier.
This feature and the prior feature can be enabled at the same time using this query.
Enable the feature and then execute the same query for a 3rd time. Now there will be more output on the Messages tab.
enter business email Join
There will be one new row in the output for every table referenced in the query. Since this query only touches one table, there is only one
new row listed. It shows that the table 'Invoices' was queried and there is a lot of information on the row -- more than the screenshot even
includes. Focus should be placed on the "logical reads" section. This statistic shows the number of 8kb data pages that were read from
this table in total. The "physical reads" section shows how many of those reads were taken directly from disk. Since this reports a zero
that indicates all pages were found in the buffer pool and were read from memory.
Note that the execution time result remains since both features are enabled. The execution time output also includes the CPU time used
by the query. This is another great metric to watch when working to improve the query.
When combined, the CPU time, amount of data pages read (from disk or memory), and execution time measured to the millisecond
provide a wonderful baseline from which to compare future executions for performance improvement.
Running the sample query for the 4th time now includes a 3rd tab in the output. This table is labeled "Client Statistics". For the purposes
of this demo, the query was run a total of 3 more times after the client statistics were included.
The output is really great! It shows the execution time along with some useful statistics for each of the 3 new executions so that they can
be compared. The most useful line for this demo is the "Wait time on server replies" as this is the number of milliseconds that were
spent waiting for SQL Server to execute the query. Nothing was changed between executions so most of the values are the same or very
similar. As changes are made to the query or to the schema of the database the query can be executed again and the values compared
to see if there is an improvement or not.
Now that the index has been created the sample query can be run one final time. STATISTICS TIME, STATISTICS IO, and Client
Statistics should all still be enabled. For the purpose of this demo, the query was run 3 more times to better show the pattern.
The first values checked are those from STATISTICS TIME and STATISTICS IO. This output shows that after the index was created the
execution time dropped to below 1 ms from a value between 12 and 30 as seen above. The number of pages read dropped by over
99% from 6,558 to 2 and the CPU time dropped from 16ms to under 1ms. This proves that the index creation definitely helped the query
in a significant way.
Next, the output from Client Statistics should be reviewed. The screenshot below shows 6 executions total. 3 came before the index
was created and 3 after.
The output shows that the 3 executions before the index were created took 10, 7, and 4 milliseconds respectively. After the index was
created the execution times dropped to 4, 0, and 1 millisecond(s) each. This further confirms that the query is running much faster and
doesn't require the programmer write down or remember any previous execution times.
Next Steps
Check out this Performance Tuning Tutorial
Get more detail on STATISTICS TIME and IO
Add Comment