SQL Server Performance Tuning

How We Made 3 Slow Stored Procedures Up to 5x Faster

Updated October 29, 20255 min read

Data loads

~1.5 GBless data read per run of the two loads

Three load procedures were slowing the server. Two of them now read 5–6x less data on every run.

Data read per run

BeforeAfter

Load_ArchitecturalUnits_TGF

Before: 1,423 MB (1.4 GB)
After: 257 MB

Load_Issues_TGF

Before: 411 MB
After: 77 MB

Source: 5 runs before and after for each procedure.

SQL Server tuning reduces CPU time and disk reads, which speeds up operations.

The 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, before and after

BeforeAfter

Stored procedure #11.8x faster

Before: 6.8 sAfter: 3.8 s

Stored procedure #25.0x faster

Before: 9.4 sAfter: 1.9 s

Stored procedure #32.2x faster

Before: 17.6 sAfter: 8.0 s

Average of 5 measured runs per procedure. Same scale within each pair.

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.

Under the hood

Before and after, every measurement

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

Stored procedure #1

SP name: [Load_ArchitecturalUnits_TGF]

Stored procedure #1, average of 5 runs

BeforeAfter

Duration1.8x faster

Before: 6.8 sAfter: 3.8 s

CPU time1.2x less

Before: 2.7 sAfter: 2.3 s

Disk reads (8 KB pages)5.5x less

Before: 173,654 readsAfter: 31,400 reads

Averages from the run table below. Each pair of bars is on its own scale.

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

[Load_ArchitecturalUnits_TGF] 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
CPU (ms)*2,6532,293~1.2x
Disk (number of reads)*173,65431,4006x
*The numbers are an average of multiple T-SQL [Load_ArchitecturalUnits_TGF] runs

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

Stored procedure #2

SP name: [Load_Issues_TGF]

Stored procedure #2, average of 5 runs

BeforeAfter

Duration5.0x faster

Before: 9.4 sAfter: 1.9 s

CPU time5.9x less

Before: 6.6 sAfter: 1.1 s

Disk reads (8 KB pages)5.3x less

Before: 50,158 readsAfter: 9,379 reads

Averages from the run table below. Each pair of bars is on its own scale.

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

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
CPU (ms)*6,6271,119~6x
Disk (number of reads)*50,1589,379~5x
*The numbers are an average of multiple T-SQL [Load_Issues_TGF] runs

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

Stored procedure #3

SP name: [Load_Infractions_TGF_2]

Stored procedure #3, average of 5 runs

BeforeAfter

Duration2.2x faster

Before: 17.6 sAfter: 8.0 s

CPU time2.3x less

Before: 17.0 sAfter: 7.4 s

Disk reads (8 KB pages)about the same

Before: 516,035 readsAfter: 563,702 reads

Averages from the run table below. Each pair of bars is on its own scale.

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:

Load_Infractions_TGF_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
CPU (ms)*16,9967,412~2x
Disk (number of reads)*516,035563,702-
*The numbers are an average of multiple T-SQL [Load_Infractions_TGF_2] runs

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

Multiple indexing changes

We also tuned a bunch of other stored procedures.

Most of them got faster too. The table below shows each one, before and after.

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 shows 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.

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 ➜