What an optimiser timeout in a SQL Server plan really means (a capped search, not a wall-clock failure), when it matters, and how to make the query easier to optimise.
The optimiser doesn't search for the best plan forever: each search phase gets a budget of optimisation work, scaled to how expensive the query looks. Time Out means it used that budget before finishing exploration and returned the best plan found so far. It isn't an error, and the plan may be fine — so I first check actual performance. If the plan is poor or unstable, the cause is usually too many alternatives: many joins, views over views, complex OR logic, or missing indexes that leave only expensive options. I simplify the query, split it with a temp table, remove redundant joins, make foreign keys trusted so join elimination works, and give it good indexes. Hints and plan forcing are last resorts.
What it means. After parsing and binding, cost-based optimisation runs in phases: a trivial plan check, then search 0 (transaction processing), search 1 (quick plan) and search 2 (full optimisation). Each phase applies transformation rules to explore alternatives and stops when it finds a plan cheap enough for the query's cost, or when its budget runs out. The budget is a count of optimisation tasks, set from the cost of the plans found so far — it is not a wall-clock timer, despite the name. When it runs out, the root operator (SELECT) shows Reason For Early Termination Of Statement Optimization: Time Out, and the best plan found is used.
Why it isn't automatically bad. Many timeout plans are perfectly good. The optimiser explores the most promising alternatives first. So I first check: is the query actually slow? Are estimates close to actuals? Is the plan stable between compilations?
When it is a problem.
CompileTime / CompileCPU in the plan) for a frequently recompiled query.What I do about it.
OR logic as UNION ALL of simpler queries.#temp table. The second query is smaller and gets fresh statistics on the temp table.WITH NOCHECK) constraints don't count.OPTION (FORCE ORDER) or join hints when you know the right order, or Query Store plan forcing to pin a known good plan — both need ongoing review.Example. An EF Core query over a view that joined six other views (about 30 tables once expanded) timed out in optimisation and flipped between a 200 ms plan and a 20 s plan after each statistics update. Querying the two base tables it actually needed, plus a trusted FK so a lookup join was eliminated, gave a plan that finished optimisation (Good Enough Plan Found) and ran in 90 ms consistently.
Checks practical handling of complex queries that exhaust the optimiser's search budget.
Green flags: explains the search phases and a cost-scaled budget; says the plan may still be fine and checks actuals first; mentions plan instability; reduces the search space with temp tables, flattening views, trusted FKs and indexes; uses hints only as a last resort.
Red flags: thinks it's a query execution timeout; wants more hardware; reaches for hints or trace flags first; doesn't know what join elimination needs.
Search phases. Before full optimisation, simplification rewrites the tree (predicate pushdown, contradiction detection, join elimination using trusted constraints). Then: trivial plan for queries with one obvious plan; search 0 for small OLTP-style queries (at least three tables, limited rules); search 1 with more rules and parallel plan consideration; search 2 with all rules. Each phase can end with a good enough plan (cost below the phase's threshold) or a timeout.
The budget. It is measured in optimisation tasks (rule applications) stored in the memo structure, and set from the estimated cost of the best plan so far: cheap queries get a small budget because spending more compile time than run time would be pointless. That is why it is called a time out but behaves deterministically for the same inputs.
Join count explosion. The number of possible join orders grows factorially; with 10+ tables the optimiser can explore only a fraction. Views and inline TVFs hide how many tables are really in the query — look at the expanded plan.
Other early termination reasons. Good Enough Plan Found (normal) and Memory Limit Exceeded (the memo grew too large — rarer and more serious). You can see them in the plan XML attribute StatementOptmEarlyAbortReason.
Finding them at scale. Search the plan cache or Query Store plans for StatementOptmEarlyAbortReason="TimeOut" and rank by CPU or duration so you fix the ones that matter.
-- Cached plans that hit an optimiser timeout, ranked by CPU
WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT TOP (20)
qs.total_worker_time / 1000 AS total_cpu_ms,
qs.execution_count,
SUBSTRING(st.text, 1, 200) AS query_start
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
WHERE qp.query_plan.exist('//StmtSimple[@StatementOptmEarlyAbortReason="TimeOut"]') = 1
ORDER BY qs.total_worker_time DESC;
-- Untrusted foreign keys block join elimination
SELECT name, OBJECT_NAME(parent_object_id) AS table_name
FROM sys.foreign_keys
WHERE is_not_trusted = 1;
-- Make one trusted again (validates existing rows)
ALTER TABLE dbo.OrderLines WITH CHECK CHECK CONSTRAINT FK_OrderLines_Orders;
-- Split a complex query with a selective intermediate result
SELECT o.OrderId, o.CustomerId
INTO #RecentOrders
FROM dbo.Orders AS o
WHERE o.CreatedAt >= DATEADD(day, -7, SYSUTCDATETIME())
AND o.Status = 'Open';
SELECT r.OrderId, c.Name, SUM(l.Amount) AS Total
FROM #RecentOrders AS r
JOIN dbo.Customers AS c ON c.CustomerId = r.CustomerId
JOIN dbo.OrderLines AS l ON l.OrderId = r.OrderId
GROUP BY r.OrderId, c.Name;
WITH NOCHECK and leaving foreign keys untrusted.FORCE ORDER or undocumented trace flags.The timeout flag tells me the query is hard to optimise, not that the plan is bad. I care when it comes with poor actual performance or plan instability. The durable fix is to make the query smaller and more honest: query the base tables you need rather than reusing a do-everything view, keep constraints trusted so the optimiser can reason, and support the access paths with indexes. With ORMs I look at the generated SQL — deep Include chains and navigation over views produce exactly these queries. Plan forcing can stabilise an incident, but it hides the complexity rather than removing it.
Last updated 23 Nov 2026.