🗄️ Fix a Table-Valued Function Forcing a Row Scan
A multi-statement table-valued function’s body is opaque to SQL Server’s optimizer – it can’t see the SELECT and INSERT statements inside the function, so for cardinality estimation purposes it falls back to a fixed, low guess for how many rows the function will return, regardless of what the function actually does or how much data is behind it. That bad guess is exactly what feeds a nested loops join against the function, and a query that joins a large outer table against this kind of function can get priced as if every call returns a handful of rows, when in practice each call returns hundreds – turning what should be a simple set-based join into a plan shaped for a workload many times smaller than the real one.
🔎 The Problem
CREATE FUNCTION dbo.GetOrdersForRegion(@RegionId INT)
RETURNS @Result TABLE (OrderId INT, Total DECIMAL(10,2))
AS
BEGIN
INSERT INTO @Result
SELECT OrderId, Total FROM dbo.Orders WHERE RegionId = @RegionId;
RETURN;
END
GO
SELECT c.CustomerName, o.OrderId, o.Total
FROM dbo.Customers c
CROSS APPLY dbo.GetOrdersForRegion(c.RegionId) o;
-- Actual execution plan:
-- Nested Loops (outer: Customers, ~50,000 rows)
-- -> Table-Valued Function [GetOrdersForRegion]
-- Estimated number of rows: 1 <- fixed MSTVF estimate
-- Actual number of rows: 340 (average, per call)
-- The optimizer priced this as 50,000 nested-loop iterations each
-- expected to touch ~1 row - the real workload is 50,000 iterations
-- each touching ~340 rows, over 17 million actual row accesses the
-- plan was never shaped to handle efficiently
✅ Fix: Make the Function Inline
- Convert the function to an inline table-valued function, a single RETURN (SELECT …) with no BEGIN/END body, wherever the logic allows it – SQL Server expands an inline TVF directly into the surrounding query like a parameterized view, letting the optimizer estimate its actual row count instead of guessing.
- On a recent compatibility level, check whether interleaved execution for multi-statement TVFs is active for this query – it lets the optimizer pause mid-compilation, actually run the function once, and use its real row count for the rest of the plan, which can remove this exact problem without rewriting the function at all.
- If the function’s logic genuinely needs the procedural, multi-statement form, consider replacing the cross apply with a plain join against a view or inline TVF that captures the same filtering logic in a single settable, even if it means duplicating some logic rather than reusing the existing function.
🚨 Why This Is Dangerous
A query that joins against a multi-statement table-valued function can look completely reasonable in the T-SQL and still generate a catastrophically bad plan purely because of a fixed cardinality guess the optimizer has no way to improve on its own – and because the query itself hasn’t changed, this kind of regression often only shows up once the data behind the function grows past whatever size made the bad estimate harmless.
The optimizer doesn’t refuse to estimate a multi-statement function’s row count – it was simply never given a way to look inside and find out.



