SQL Server Performance Tuning

How We Made SQL Server Run 9 Times Faster

Updated
5 min read
Written by
Mark Varnas

Improvement after tuning, Stored Procedure #1

2x

DURATION

1x

CPU

6x

DISK

Improvement after tuning, Stored Procedure #2

5x

DURATION

6x

CPU

5x

DISK

Improvement after tuning, Stored Procedure #3

2x

DURATION

2x

CPU

Overall improvements for the other stored procedures in the SQL Server

2x

DURATION (MS)

3x

CPU (MS)

2x

DISK (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

2x

STORE PROCEDURE #1

5x

STORE PROCEDURE #2

2x

STORE 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

2x

DURATION

6x

DISK

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

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

5x

DURATION

6x

CPU

5x

DISK

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 (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

2x

DURATION

2x

CPU

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:

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.

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.

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