SQL Server Performance Tuning

How Splitting One Subquery Made a Procedure 108x Faster

Updated October 29, 20253 min read

Server load

~65 GBless data read on every run

Every call made its caller wait 31 seconds. Splitting one subquery cut that to 287 ms.

Data read per run

Before64,957 MB (65 GB)
After141 MB

Source: 5 runs before and after. Data = 7,929,279 → 17,170 pages × 8 KB.

Reading from disk is the slowest operation SQL Server does.

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

The problem

The stored procedure was too slow and performed an excessive number of disk reads, as shown in the chart below:

The fix

Here's what we did to fix the issue:

  • Split a subquery into four parts using UNION
  • Replaced ISNULL function in WHERE clause

That’s it.

Why does disk improvement matter for stored procedure speed?

It's straightforward: the less often you access the disk, the more disk capacity remains available.

It works just like a highway. Imagine a 3-lane highway with just 5 cars driving on it every minute.

What if you add 50 cars? The speed remains the same because 55 cars don’t overload that highway.

What if you add another 500 or 5,000? Now you are starting a slowdown in traffic.

They all still get home. But not at the same speed anymore.

It's the same deal with SQL Servers. That's why it's important to speed-tune the most critical resources.

The fewer hits to the storage, the more capacity available.

During this tuning, we achieved improved performance by dividing a subquery into four distinct parts through UNION and substituting the ISNULL function in the WHERE clause.

Under the hood

Before and after, every measurement

The improvement was huge: average duration dropped from 31,025 ms (31 seconds) to just 287 ms!

Below you will see a comparison of SQL procedure performance before and after tuning.

Microsoft SQL performance tuning chart

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

Duration (ms)CPU (ms)Disk
(8k page reads)
BeforeAfterBeforeAfterBeforeAfter
Run 174,23748073,3284698,189,92028,724
Run 221,59230621,4222977,877,02319,023
Run 318,81516018,6251567,851,2099,542
Run 422,21432721,9063287,877,04819,023
Run 518,26816518,0161567,851,1969,539
AVG31,02528730,6592817,929,27917,170

We managed to significantly reduce the total number of disk reads:

BeforeAfterImprovement
Duration, ms*31,025287108x
CPU, ms*30,659281109x
Disk, number of reads*7,929,27917,170462x

After our tuning, the stored procedure ran 108 times faster.

CPU time dropped 109 times and disk reads dropped 462 times.

As a result, the query runs 108 times faster.

Check Red9's SQL Server Services

SQL Server Consulting

Perfect for one-time projects like SQL migrations or upgrades, and short-term fixes such as performance issues or SQL remediation.

Discover More ➜

SQL Server Managed Services

Continuous SQL support, proactive monitoring, and expert DBA help with one predictable monthly fee.

Discover More ➜

Emergency SQL Support

Take the stress out of emergencies with immediate access to a SQL Server Sr. DBA 24x7x365

Discover More ➜

Explore All Services ➜