SQL Server Performance Tuning

How Splitting Four JOINs With UNION Made a Query 45x Faster

Updated October 29, 20252 min read

Finance operations

~105 GBless data read on every run

Every run of this receipts-repair procedure waited about 31 seconds (30,682 ms). Splitting one query with UNION cut that to 685 ms.

Data read per run

Before105,272 MB (105 GB)
After152 MB

Source: average of multiple runs. Data = 12,850,589 → 18,532 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 too many disk reads.

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

  • Duration (ms): 30,682
  • CPU (ms): 18,922
  • Disk (number of read operations): 12,850,589

The fix

This is what we did to fix the issue: a SELECT with four JOINS became two SELECTs using a UNION.

That’s it.

Final thoughts

Most SQL Server performance issues come from disk access, not the CPU or RAM as often thought.

The main problem is disk reads due to inefficient queries that fill up RAM and push the system to read from the disk - its slowest operation.

We boosted performance by turning a SELECT with four JOINS into two SELECTs with a UNION, simplifying queries to reduce system load and directly enhance speed.

This approach makes queries more efficient and decreases disk reads, thereby lightening the load on both CPU and RAM.

Cutting down on disk reads is key because it directly improves SQL Server's main performance issue, leading to faster query times.

Under the hood

Before and after, every measurement

The improvement was huge: the total number of disk reads was 18,532 instead of 12,850,589!

We can't paste the client code here, but here is what we can show you.

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

SQL procedure performance before and after tuning
Before After Improvement
Duration (ms)*30,68268545x
CPU (ms)*18,9221,24915x
Disk (number of read operations)*12,850,58918,532693x
*The numbers are an average of multiple T-SQL [RepairPostedReceiptsOnVoidedEntries] runs.

After our tuning, the stored procedure became significantly faster, with average duration dropping from 30,682 ms to 685 ms.

That’s ~45x 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 ➜