What is WorkTable in SQL Server?
SET STATISTICS IO ON
FROM Production.Product p
CROSS JOIN Production.Product p1
If you run above query, there is good chance that it will give you following details in the message windows.
Table 'Product'. Scan count
2, logical reads 30, physical reads 1, read-ahead reads 17, lob logical reads
0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 1, logical reads 8110, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
You will notice in the message there is a table called worktable. However, if you see the original table there is no worktable. This is where the question has originated.
The question in another word is: Why does statistics show the worktable where it is not there in the original query?
The relational engine may need to build a worktable to perform a logical operation specified in an SQL statement. Worktables are internal tables that are used to hold intermediate results. Worktables are generated for certain GROUP BY, ORDER BY, or UNION queries. For example, if an ORDER BY clause references columns that are not covered by any indexes, the relational engine may need to generate a worktable to sort the result set into the order requested. Worktables are also sometimes used as spools that temporarily hold the result of executing a part of a query plan. Worktables are built in tempdb and are dropped automatically when they are no longer needed.