SQL Server Performance Tuning

How We Made T-SQL Queries Run 34,785x Faster

Mark Varnas
No comments

Improvement after tuning





Most SQL Servers bottleneck on Disk, which is number of disk operations required to pull all data necessary to process the the query.

When tuning, the goal is often to get SQL Server to read less data from the disk.


We identified a long-running query that was throttling CPU usage to 70% and concurrently executing across 12 threads.

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

  • Duration (query duration in milliseconds): 3,200,290
  • Disk (number of read operations required to get all data needed): 942,222,862

The query execution time was 53 minutes or 3,200,290 ms while making almost 1 million disk read operations.


  • After reviewing the query plan, we identified the culprit: a NOT IN operation invoking a VIEW.
  • The fix was straightforward: switching the NOT IN operation to a NOT EXISTS operation. See below.

Here is how we modified the original query to fix the problem:

Note: Performance of NOT IN vs. NOT EXISTS – performance vise is often massive. This is one of those examples.

Before vs. After

Small changes, huge difference? Totally!

We slashed a query’s run time from 3,200,290 ms down to 92 ms. No joke!

Below is a comparison of the number of disk reads before vs after tuning:

Disk (number of read operations)

Before tuning
After tuning
Duration (ms) 3,200,2909234,785x or 3,478,576%
Disk (number of reads)942,222,86230,31231,084x or 3,108,415%

Simple query rewrite reduced the duration of the stored procedure by a 34,785 times!

Duration – milliseconds

Before tuning
After tuning

Final Thoughts

A simple tweak of swapping the NOT IN operation for a NOT EXISTS operation resulted in a 34,785x improvement.

In SQL performance tuning, it’s often crucial to understand how the SQL engine retrieves data.

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. I channel my SQL into our SQL Managed Services, SQL Consulting and our internal database products.

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

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

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