SQL Server Performance Tuning

How We Made T-SQL Queries 9 Times Faster

Updated
3 min read
Written by
Mark Varnas

Improvement after tuning

9x

DURATION

9x

CPU

22x

DISK

Reading from disk is the slowest operation SQL Server does.

Therefore, tuning for less disk “reads” is often the primary goal.

Problem

The client’s billing process was slow because the stored procedure took 34,256 ms on average to run.

Here’s a glance at the pre-tuning metrics:

  • Duration (ms): 34,256
  • CPU (ms): 33,968
  • Disk (number of read operations): 3,298,493

Solution

This stored procedure was slow due to the large number of disk reads it was performing.

We refactored the code to reduce the number of reads it needed to perform.

Instead of hauling millions of rows, we trimmed it down to just what was needed.

Before vs. After

The most interesting part…

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

Disk reads took a massive dive, plummeting from 3,298,493 down to just 147,424.

That’s more than a 22-fold decrease!

Disk (number of read operations)

3,298,493
Before tuning
147,424
After tuning

Here is the breakdown:

BeforeAfterImprovement
Duration (ms)*34,2563,8599x or 888%
CPU (ms)*33,9683,8549x or 881%
Disk (number of read operations)*3,298,493147,42422x or 2,237%
*The numbers Before/After are an average of 3 separate procedure runs

After our tuning, the stored procedure became significantly faster, showing an overall improvement of 888%!

That’s almost 9x faster!

That means that through refactoring, we’ve reduced the overall duration time from 34,256 ms to just 3,859 ms!

Duration (ms)

34,256
Before tuning
3,859
After tuning

Final thoughts

Most SQL Servers bottleneck is on Disk access (or disk “reads”).

It’s not CPU or RAM – which most customers often suspect first.

And that makes much sense. Here is why:

  • Inefficient queries scan (or read) much data.
  • Data read in is stored in RAM. As more data is read in, “older” data is pushed out from RAM.
  • If there isn’t enough RAM to keep ALL data in memory (which is often not possible), SQL Server has to read from disk – and that is the slowest operation SQL Server can do.
  • When the query can be tuned to read 10 rows vs. 10 million – less CPU and RAM automatically are necessary.

Since 95% of SQL Server bottlenecks are due to disk resources, cutting down on disk reads is the main goal.

To the end-user, nothing is more important than Speed (or Duration of the query).

Tuning to reduce CPU/RAM resources is helpful too.

When queries are tuned to need less CPU & RAM, it means that the same server now has more capacity.

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