79250246

Date: 2024-12-04 07:52:29
Score: 1
Natty:
Report link

The second query runs faster because it breaks the process in small steps using temp tables (#TBL1 and #TBL2) to avoids repeated calculating results.

In first query using DISTINCT, UNION ALL, and joining in one complex query makes SQL Server re-compute the results multiple times, that slows it down so by storing results in temp tables SQL Server can optimize and reuse them and leading to better performance.

Reasons:
  • No code block (0.5):
  • Low reputation (0.5):
Posted by: Harshini