Sample Header Ad - 728x90

In-Memory Table Type with Primary Key causing Eager Index Spool

0 votes
1 answer
73 views
I have defined an In-Memory Table Type to hold a sequence of unique integers for the purpose of passing them between stored procedures. The type has the following definition: CREATE TYPE [dbo].[IntegerUniqueList] AS TABLE( [IntegerValue] [int] NOT NULL, PRIMARY KEY NONCLUSTERED ( [IntegerValue] ASC ) ) WITH ( MEMORY_OPTIMIZED = ON ) In complex queries that use it, even though an index seek seems the obvious choice, the optimizer is choosing to include an Index Spool. This is defeating the point of having the data in memory. This can be replicated in AdventureWorks2019 with the following: DECLARE @IDs dbo.IntegerUniqueList INSERT @IDs (IntegerValue) SELECT p.BusinessEntityID FROM Person.BusinessEntity p SELECT p.BusinessEntityID FROM Person.Person p WHERE (p.BusinessEntityID = 1) OR ( ( (0 = 1) OR EXISTS ( SELECT 1 FROM Person.BusinessEntity be JOIN Person.BusinessEntityAddress bea ON (bea.BusinessEntityID = be.BusinessEntityID) JOIN Person.Address a ON (a.AddressID = bea.AddressID) WHERE (bea.AddressTypeID = 2) AND (be.BusinessEntityID = p.BusinessEntityID) AND (a.AddressLine1 LIKE N'%1%') ) ) AND EXISTS ( SELECT 1 FROM @IDs id WHERE (id.IntegerValue = p.BusinessEntityID) ) ) Here is the plan produced: https://www.brentozar.com/pastetheplan/?id=SkFipTYTR If I remove the in-memory aspect, or change the NONCLUSTERED Primary Key to use a HASH index, the spool goes away. Given that in-memory *should* give better performance and I can't predict how many rows will be put in the variables to predict a reasonable BUCKET_COUNT, neither of those solutions is ideal. What's going on here and are there any other options to prevent the spool?
Asked by Dan Def (165 rep)
Sep 19, 2024, 04:26 PM
Last activity: Sep 19, 2024, 04:35 PM