SQL Server Performance Tuning

How We Made T-SQL Queries 8 Times Faster

Updated
2 min read
Written by
Mark Varnas

Improvement after tuning

8x

DURATION

5x

CPU

17x

DISK

Reading from disk is the slowest operation SQL Server does.

Therefore, tuning for less disk “reads” is often the primary goal.

Problem

The slow stored procedure significantly hampered SQL Server performance.

Pre-tuning Metrics

180
Duration (ms)
117
CPU (ms)
21,737
Disk (number of reads)

Solution

So here’s what we did to improve the query performance:

  • Changed cursor type to FAST_FORWARD
  • Replaced temporary #table with variable @table
  • Added index IX_groupID_IsDeleted on dbo.Violations

Before vs. After

The most interesting part…

The improvement was significant: the total number of disk reads decreased to just 1,271 from 21,737, resulting in a 5x reduction in CPU time!

The green block below shows SQL performance numbers after we made some changes in the database to improve slow SQL calls.

We took 5 separate runs to analyze query performance before and after the fix:

Duration (ms)CPU (ms)Disk
(8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 1218111711623,26534
Run 27118791517,6081,073
Run 330141101623,040100
Run 42081915623,841773
Run 5106671094720,9354,376
AVG180231252421,7371,271

Disk (average number of reads per run)

21,737
Before tuning
1271
After tuning
BeforeAfterImprovement
Duration (ms)*18023~8x (or 783%)
CPU (ms)*12524~5x (or 520%)
Disk (number of reads)*21,7371,271~17x (or 1,710%)
*The numbers are an avg of multiple T-SQL [LoadColorBitmapsForGroup] runs.

Duration (ms)

180
Before tuning
23
After tuning

After our tuning, the stored procedure became significantly faster, showing an overall improvement of 783%!

That’s 8x faster!

Final thoughts

If possible, avoid cursors!

I still see a lot of people using cursors when they shouldn’t.

There are very few situations where a set-based query cannot replace a cursor logic.

For these, FAST FORWARD cursor is an excellent place to start.

Cursor options like FORWARD_ONLY, STATIC, and KEYSET can all produce parallel plans and reduce performance impacts too.

Speak with a SQL Expert

In just 30 minutes, we will show you how we can eliminate your SQL Server headaches and provide 
operational peace of mind

Article by
Mark Varnas
Founder | CEO | SQL Veteran
Hey, I'm Mark, one of the guys behind Red9. I make a living performance tuning SQL Servers and making them more stable.

Discover More

SQL Server Health Check SQL Server Migrations & Upgrades SQL Server Performance Tuning SQL Server Security SQL Server Tips

Discover what clients are saying about Red9

Red9 has incredible expertise both in SQL migration and performance tuning.

The biggest benefit has been performance gains and tuning associated with migrating to AWS and a newer version of SQL Server with Always On clustering. Red9 was integral to this process. The deep knowledge of MSSQL and combined experience of Red9 have been a huge asset during a difficult migration. Red9 found inefficient indexes and performance bottlenecks that improved latency by over 400%.

Rich Staats 5 stars
Rich Staats
Cloud Engineer
MetalToad

Always willing to go an extra mile

Working with Red9 DBAs has been a pleasure. They are great team players and have an expert knowledge of SQL Server database administration. And are always willing to go the extra mile to get the project done.
5 stars
Evelyn A.
Sr. Database Administrator

Boosts server health and efficiency for enhanced customer satisfaction

Since adding Red9 to the reporting and DataWarehousing team, Red9 has done a good job coming up to speed on our environments and helping ensure we continue to meet our customer's needs. Red9 has taken ownership of our servers ensuring they remain healthy by monitoring and tuning inefficient queries.
5 stars
Andrew F.
Datawarehousing Manager
See more testimonials