SQL Server Performance Tuning

How We Made SQL Server Run 9 Times Faster

Updated October 29, 20257 min read

Written byMark Varnas

Improvement after tuning, Stored Procedure #1

2xDURATION
1xCPU
6xDISK

Improvement after tuning, Stored Procedure #2

5xDURATION
6xCPU
5xDISK

Improvement after tuning, Stored Procedure #3

2xDURATION
2xCPU

Overall improvements for the other stored procedures in the SQL Server

2xDURATION (MS)
3xCPU (MS)
2xDISK (NUMBER OF READS)

SQL Server tuning is crucial—it reduces CPU time, disk reads, and speeds up operations, making everything more efficient.

Problem

Slow stored procedures (SP) significantly affected the SQL Server's performance.

We identified multiple instances of SPs that were slow, and we are going to show 3 of them in detail below.

The improvements for these 3 stored procedures were the following:

Duration improvement after tuning

2xSTORE PROCEDURE #1
5xSTORE PROCEDURE #2
2xSTORE PROCEDURE #3

Before vs. After

Let's examine the gains from the tuning of each of the three stored procedures.

Stored procedure #1

SP name:[LoadArchitecturalItems_MK]

Improvement after tuning

2xDURATION
6xDISK

Here's the before-and-after snapshot that showcases the efficiency gains:

[LoadArchitecturalItems_MK] performance before and after tuning
Duration
(ms)
CPU
(ms)
Disk
(8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 112,8683,1142,4842,172131,40222,150
Run 27,0212,8721,7341,669125,26422,724
Run 35,7185,0583,2972,734243,71243,863
Run 43,3773,1082,6252,063124,20324,453
Run 54,7814,9253,1252,828243,69343,813
AVG6,7533,8152,6532,293173,65431,400
BeforeAfterImprovement
Duration (ms)*6,7533,8152x (or 177%)
CPU (ms)*2,6532,2931x (or 116%)
Disk (number of reads)*173,65431,4006x (or 553%)
*The numbers are an average of multiple T-SQL [LoadArchitecturalItems_MK] runs

After tuning the stored procedure, it started running 77% faster with almost 6 times fewer disk reads.

Stored procedure #2

SP name:[LoadProblems_MK]

Improvement after tuning

5xDURATION
6xCPU
5xDISK

Here's the before-and-after snapshot that showcases the efficiency gains:

LoadProblems_MK: before vs. after
Duration
(ms)
CPU
(ms)
Disk
(8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 16,5107896,06261042,9715,234
Run 21,6902551,65618812,4863,331
Run 314,4232,93712,5472,34493,78817,484
Run 49794209211727,8443,398
Run 523,5614,97911,9532,28193,70517,452
AVG9,4321,8766,6271,11950,1589,379
BeforeAfterImprovement
Duration (ms)*9,4321,876~5x (or 503%)
CPU (ms)*6,6271,119~6x (or 592%)
Disk (number of reads)*50,1589,379~5x (or 535%)
*The numbers are an average of multiple T-SQL [LoadProblems_MK] runs

After tuning the stored procedure, it started running 503% faster with over 5 times fewer disk reads.

Stored procedure #3

SP name:[LoadViolationMK_2]

Improvement after tuning

2xDURATION
2xCPU

To improve the overall performance of this SP, we did the following steps:

  • Added a filter for @ClientID in a CTE/Subquery.
  • Replaced two temp tables with variable tables.
  • Removed OPTION (RECOMPILE) query hint.

Here's the before-and-after snapshot that showcases the efficiency gains:

LoadViolationMK_2: before vs. after
Duration
(ms)
CPU
(ms)
Disk
(8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 116,9988,24816,3907,156512,942570,813
Run 210,7807,91810,3917,359489,224489,490
Run 333,4938,07632,4537,766583,396530,933
Run 413,1008,12212,7977,562496,772740,167
Run 513,3957,42212,9537,219497,841487,108
AVG17,5537,95716,9967,412516,035563,702
BeforeAfterImprovement
Duration (ms)*17,5537,957~2x (or 221%)
CPU (ms)*16,9967,412~2x (or 229%)
Disk (number of reads)*516,035563,702-
*The numbers are an average of multiple T-SQL [LoadViolationMK_2] runs

After tuning the stored procedure, it started running ~2x faster!

Multiple indexing changes

We also tuned a bunch of other stored procedures.

The overall duration improvement concluded almost 18,954%. That is almost 190x!

Check out this table below that highlights some serious performance improvements for stored procedures.

Multiple indexing changes: before vs. after

Let's take a closer look at this table:

  • avgCPU #1 column shows – CPU cycles needed to run this stored proc before tuning.
  • avgCPU #2 column shows – CPU cycles needed to run this stored proc after tuning.
  • Delta avgCPU column shows – the difference between before tuning vs. after. If the number is negative, then the speed improved.
  • avgReads #1 column shows – Storage read operations needed to run this stored proc before tuning.
  • avgReads #2 column shows – Storage read operations to run this stored proc after tuning.
  • Delta avgReads – the difference between before vs. after. If the number is negative, then the speed improved.
  • avgDuration #1 - shows duration with old indexes before IX changes.
  • avgDuration #2 - shows duration after new indexing is deployed.
  • The last column “Delta avgDuration” is the one that matters the most. It shows if stored procs are slower or faster.

Final thoughts

Such dramatic enhancements serve as a potent reminder of the power of SQL tuning.

Even minor adjustments can unleash significant performance gains, ensuring our systems run smoother, faster, and more efficiently.

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