SQL Server Performance Tuning

How We Made T-SQL Queries Run 6x Faster

Updated
2 min read
Written by
Mark Varnas

Improvement after tuning

6x

DURATION

4x

CPU

3x

DISK

SQL Server tuning is crucial – it reduces CPU time, disk reads, and speeds up operations, making everything more efficient.

Problem

The slow stored procedure significantly hampered SQL Server performance.

Pre-tuning Metrics

1,881
Duration (ms)
1,025
CPU (ms)
9,849
Disk (number of reads)

Solution

Here is what we’ve done to make this query run faster:

  • Removed SQL function from WHERE clause (subquery)
  • Removed unused columns from temporary tables
  • Replaced two temporary tables with Common Table Expressions (CTE)

Before vs. After

The improvement was huge: the total number of disk reads was 3,157 instead of 9,849!

Disc (number of reads)

9,849
Before tuning
3,157
After tuning
Duration
(ms)
CPU
(ms)
Disk
(8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 18054765004535,5634,471
Run 280734633713
Run 31,17141251,03414
Run 43,0095631,76651610,9584,612
Run 53,6134322,68839031,3546,678
AVG1,8812951,0254539,8493,157

Below is the detailed breakdown by metric:

Before After Improvement
Duration (ms)*1,881295~6 ( or 638%)
CPU (ms)*1,025453~2 ( or 226%)
Disk (number of reads)*9,8493,157~3 ( or 312%)
*The numbers are an average of multiple T-SQL runs

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

That’s over 6x faster!

Duration (ms)

1,881
Before tuning
295
After tuning

SQL Server functions in the WHERE clause

For better performance, avoid using functions in the WHERE clause. Here is why:

When you use functions in the WHERE clause, it makes SQL Server scan the whole table or index instead of quickly finding what it needs with an index seek, assuming an applicable index exists.

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