SQL Server Performance Tuning

How We Made T-SQL Queries 8 Times Faster

Updated October 29, 20252 min read

T-SQL queries 8x faster. A paper plane streaks past a row of slow paper boats.

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 cursor logic.

For these, a 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.

Check Red9's SQL Server Services

SQL Server Consulting

Perfect for one-time projects like SQL migrations or upgrades, and short-term fixes such as performance issues or SQL remediation.

Discover More ➜

SQL Server Managed Services

Continuous SQL support, proactive monitoring, and expert DBA help with one predictable monthly fee.

Discover More ➜

Emergency SQL Support

Take the stress out of emergencies with immediate access to a SQL Server Sr. DBA 24x7x365

Discover More ➜

Explore All Services ➜