Foliotek Developer Blog

A Programmers Guide to Performance Tuning SQL Server Queries

First off – I am not a DBA. What follows is just some general tools and tips I’ve found to help write better queries, find queries that are performing poorly, and find some ideas on how you might improve performance.

Here’s the general procedure I use to find and fix the poorly performing “low hanging fruit” in an SQL Server driven database application.

  1. Do you know a query you want to improve? ?If so, skip to step 3.
  2. Use SQL Profiler to identify candidate queries 1. Fire up SQL Profiler. ?You can find it under ‘SQL ….->Performance Tools’ in your start menu, or you can open it in the ‘Tools” menu of SQL Management Studio.
  3. Start a new trace. ? Use the event selections tab to filter for queries you care about. 1. I usually check ‘Stored Procedures->RPC:Completed’ and ‘TSQL->SQL:BatchCompleted’
  4. I also use the column filters to limit to the ApplicationName/DatabaseName/LoginName/HostName I care about and the performance characteristics I’m looking for ?(a good place to start is >100 cpu, >50 duration or >5000 reads. ?Adjust as necessary to find the worst performing in each category)
  5. Run the trace until you see some queries you are interested in. ?Pause or stop it and select the query text from the detail window.
  6. Copy your identified query into SQL Management Studio. ?Right click to turn on ‘Execution Plan’ and ‘Client Statistics’ if they aren’t on already. ?Assuming you’ve selected a SELECT query that won’t change data, run it to get a feel for the amount of time you want to improve. ?Look at the execution plan and the client statistics to see what parts of the query cost most – you might identify ways to rewrite the query to improve performance. ? Common ways to improve are to shrink the amount of data you are returning by limiting columns returned, etc.
  7. If you can’t find a way to improve performance with a rewrite (or want to improve it even more) you can often improve performance by adding SQL Statistics or Indexes. ?You might be able to guess appropriate indexes based on the execution ?plan/client statistics – but it’s just as easy to let SQL identify candidates for you with SQL Tuning Advisor. 1. Highlight the query, right-click, and select Analyze Query in Database Engine Tuning Advisor
  8. Click the play button ‘start analysis’
  9. Look over the recommended statistics and indexes, especially the size column. ?Indexes are a size/speed tradeoff – you want to make sure you are getting enough benefit out of the extra index space. ?Statistics generally have a lower impact to both speed and storage space (they are stored in a blob in an internal table). ?NOTE: I’ve generally found the ‘Estimated improvement’ number useless. ?I’ve applied an estimated 2% improvement that realized a 2x speed benefit.
  10. Uncheck any you don’t want, and then click ‘Save Recommendations’ to create a .sql file you can apply to the server to create the indexes and statistics.