🗄️ SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
A computed column index can only be used when a predicate’s expression matches the computed column’s own definition closely enough for the optimizer to recognize it – including matching data types exactly. Comparing the computed column against a parameter of a different type than what it resolves to forces an implicit conversion before the comparison runs, which breaks the expression match the optimizer relies on to substitute the indexed computed column. An ORM that sends every parameter as a string by default is a common, invisible source of exactly this mismatch, and the index that sys.indexes clearly shows as present can simply stop being chosen the moment a driver changes how it types its parameters.
🔎 The Problem
-- Computed column definition ALTER TABLE Orders ADD OrderCode AS CAST(OrderNumericId AS INT); CREATE INDEX IX_Orders_OrderCode ON Orders(OrderCode); -- ORM sends OrderCode as an NVARCHAR parameter by default: SELECT * FROM Orders WHERE OrderCode = @p0; -- @p0 nvarchar(4000) -- the implicit CONVERT(nvarchar, OrderCode) breaks the expression -- match -> IX_Orders_OrderCode is no longer considered by the optimizer
✅ Fix: Match the Computed Column’s Exact Resulting Type
- Compare the actual execution plan’s predicate against the computed column’s exact definition (queryable from
sys.computed_columns) to confirm the data types genuinely match, rather than assuming a query referencing the same underlying columns automatically matches the computed expression. - Explicitly cast the parameter or literal to the computed column’s exact resulting type in the query itself, instead of relying on an ORM’s default parameter typing to happen to match.
- Capture the parameter types a specific client library actually sends with an extended events session, since the type mismatch is often invisible in the application’s own types and only shows up at the wire protocol level.
🚨 Why This Is Dangerous
- A computed column index that “used to work” and silently isn’t chosen anymore, with no error and no obvious plan change in casual review, can mean a report that was fast for months starts timing out in production the moment a driver version bump changes its default parameter type.
A computed column index matches an expression, not just a column name – and an implicit cast is enough of a different expression to lose that match entirely.

