I have a very simple query that I am trying to optimize. issues_notes is a simple table, while meta_users is a table on a remote database server that I am querying via a view.
when I run the query without ordering it returns immediately, but when I add ORDER BY date it takes about 4 seconds to return. I had SQL Server show the execution plan, and it appears that the slowness is introduced in a Table Spool operation that happens only on the join. Is there a way to prevent this “optimization”?
Query:
SELECT
[issues_notes].[date],
[meta_users].[firstname],
[meta_users].[lastname],
[issues_notes].[note]
FROM
[issues_notes]
LEFT JOIN [issues_issue]
ON ([issues_notes].[issue_id] = [issues_issue].[id])
LEFT OUTER JOIN [meta_users]
ON ([issues_notes].[author_id] = [meta_users].[userid])
WHERE
([issues_issue].[issue_hash] = '%s' )
Execution Plan without Order By:

Execution Plan With Order By:

The Table Spool is a “Lazy Spool”. That means it remembers rows it has seen, but does not do any extra work. Because the rows coming in on the left of the join are now sorted, repeating rows with the same value will show up right after one another. The spool allows to just reuse them instead of hitting the remote server again.
The sort itself is a blocking operator. That means it will hold all rows before the sort and return them only after the sort is finished.
How many rows are we talking here? What is the total execution time of both queries?