SQL Server Performance Tuning

How We Made T-SQL Queries Run 354x Faster

Updated
3 min read
Written by
Mark Varnas

Improvement after tuning

43x

DURATION

144x

CPU

168x

DISK

Reading from disk is the slowest operation SQL Server does.

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

Problem

The stored procedure ran very frequently (multiple times per minute) and consumed a lot of server resources (CPU, Disk reads and Duration).

Pre-tuning Metrics

44,177
Duration (ms)
64,829
CPU (ms)
686,140
Disk (number of reads)

Solution

We added certain indexes to improve the performance.

Before vs. After

There was a dramatic improvement: the overall duration of the procedure plummeted from 44,177 ms to only 1,037 ms!

Here’s a comparison highlighting the gains in efficiency:

We took 5 separate runs to analyze query performance before and after the fix:

Duration (ms)CPU (ms)Disk (8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 137,06139164,446281932,2043,687
Run 244,00221866,730203633,6202,658
Run 344,5842,81166,403765617,2824,395
Run 442,81982162,434515617,1623,613
Run 552,42294664,134484630,4336,067
AVG44,1771,03764,829449686,1404,084

As you can see, we successfully cut down on the average number of disk reads:

Disk, number of reads (average)

686,140
Before tuning
4,084
After tuning

Before TunningAfter TunningImprovement (%)Improvement (x)
Duration (ms)*44,1771,0374,26043
CPU (ms)*64,82944914,439144
Disk (number of operations)*686,1404,08416,801168
*The numbers Before/After are an average of 5 separate procedure runs

Overall, we observed a staggering improvement, making the query execution 354 times faster!

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 the available RAM isn’t sufficient to hold all data in memory (a common scenario), SQL Server must resort to disk reads, which are the slowest operations it can perform.
  • 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), in any case.

Tuning to lower CPU/RAM usage is also beneficial.

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

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.

Managed SQL Server services, consulting, and emergency support from expert DBAs to improve performance, predictability, and cost.

Get started with Red9 today.

Contact us

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