Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of Table Spool in SQL Server Execution plan

I have a query that creates several temporary tables and then inserts data into them. From what I understand this is a potential cause of Table Spool. When I look at my execution plan the bulk of my processing is spent on Table Spool. Are there any good techniques for improving these types of performance problems? Would using a view or a CTE offer me any benefits over the temp tables?

I also noticed that when I mouse over each table spool the output list is from the same temporary table.

like image 911
Abe Miessler Avatar asked Dec 18 '09 22:12

Abe Miessler


People also ask

What is table Spool in SQL Server execution plan?

Spool is a physical operator that shows up in the execution plan. Query optimizer uses spools when it thinks that it's better to put data in the temp table rather than hitting a table again and again using seeks and scans. Check if below blog could help you.

What is table spool eager spool SQL Server?

"The role of the Eager Spool is to catch all the rows received from another operator and store these rows in TempDB. The word “eager” means that the operator will read ALL rows from the previously operator at one time. In other words, it will take the entire input, storing each row received."

What is a lazy spool SQL?

SQL Server Table Spool Operator (Lazy Spool) Te SQL Server Lazy Spool is used to build a temporary table on the TempDB and fill it in lazy manner. In other words, it fills the table by reading and storing the data only when individual rows are required by the parent operator.

What is hash match in SQL Server execution plan?

SQL Server Hash Match Aggregate operator is used to process the large tables that are not sorted using an index. It builds a hash table in the memory, calculates a hash value for each record, then scan all other records for that hash key.


1 Answers

Well, with the information you gave I can tell only that: the query optimizer has chosen the best possible plan. It uses table spools to speed up execution. Alternatives that don't use table spools would be even slower.

How about showing the query, the table(s) schema and cardinality, and the plan.

Update

I certainly understand if you cannot show us the query. But is really hard to guess why the spooling is preffered by the engine whithout knowing any specifics. I recommend you go over Craig Freedman's blog, he is an engineer in the query optimizer team and has explained a lot of the inner workings of SQL 2005/2008 optimizer. Here are some entries I could quickly find that touch the topic of spooling in one form or another:

  • Recursive CTEs
  • Ranking Functions: RANK, DENSE_RANK, and NTILE
  • More on TOP

SQL customer support team also has an interesting blog at http://blogs.msdn.com/psssql/
And 'sqltips' (the relational engine's team blog) has some tips, like Spool operators in query plan...

like image 191
Remus Rusanu Avatar answered Sep 18 '22 22:09

Remus Rusanu