SQL Server Performance Tuning

How We Made T-SQL Queries 8 Times Faster

Updated October 29, 20252 min read

Written byMark Varnas

Improvement after tuning

8xDURATION
5xCPU
17xDISK

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.

Improved SQL performance metrics

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 420819156-23,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.

Discover More

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 StaatsRich StaatsCloud EngineerMetalToad
See more testimonials