SQL Server Performance Tuning

How UNION ALL Made a Timing-Out Procedure 38x Faster

Updated October 29, 20253 min read

Server load

~74 sof CPU saved on every call

Users on the app’s front end waited 38 seconds and then hit a timeout. Now the screen loads in about a second.

CPU per call

Before75 s
After0.9 s

Source: STATISTICS IO and timings before and after. Data = 687,475 → 314 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 took 38 seconds (38,000 ms) to execute. It times out on the application frontend. It was performing multiple scans and reading too much data.

The fix

The stored procedure was rewritten.

Also, we added a second SELECT with a UNION ALL, this was necessary to divide the WHERE clause selectivity.

Note: Both versions of the stored procedure deliver the same info, but the order's different.

Before launching this change, just make sure the app doesn't mind the data order. It probably doesn't, since the app sorts the data itself.

Why does disk improvement matter for stored procedure speed?

The less you access the disk, the more disk capacity is left over.

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.

In this tuning session, better performance was achieved by overhauling a stored procedure and adding a second SELECT with a UNION ALL to divide the WHERE clause selectivity.

Under the hood

Before and after, every measurement

The improvement was huge: Duration of the procedure dropped from 38 seconds to just 1 second!

Here is the detailed breakdown.

Statistics IO output
Table reads: before vs. after
Disk, logical reads per table
BeforeAfter
Table 128,924123
Table 2363,931133
Table 3294,62058
Total per run687,475314

Disk reads took a massive dive, plummeting from 687,475 down to just 314.

That’s more than a 2,189-fold decrease!

BeforeAfterImprovement
Duration (ms)38,0001,000~38x
CPU (ms)75,000941~80x
Disk (number of reads)*687,475314~2,189x
*Sum of logical reads across the three tables in the STATISTICS IO output above

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

As a result - 38 times faster query!

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 ➜