Hi @Benjamin Shaw ,
Agreed with other experts, your first query
SELECT U.UserID,
U.FirstName
FROM Users U
OUTER APPLY (SELECT TOP 1
Salary,
Currency,
Interest
FROM Salary S
WHERE S.UserID = U.UserID
WHERE S.PaymentDate = U.StartDate) X
is equal to below query since no column is from the OUTER APPLY part.
SELECT U.UserID,
U.FirstName
FROM Users U
The specific form of OUTER APPLY ( SELECT TOP ... ) is most likely to result in a correlated nested loops join in current versions of SQL Server, because the optimizer does not contain logic to transform this pattern to an equivalent JOIN.
Please refer below execution plan of my example:
In the second query, I added one column and it had extra nested loops, compute and table scan. So in the second query, the query cost was much larger.
In addition, in order to improve your query, you could have a try to create indexes and update statistics.
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.