Non Sargable Predicate: Why the Plan Never Warns You

Non Sargable Predicate: Why the Plan Never Warns You

The query is fast in development and slow in production. The plan shows an Index Scan on a column that has a perfectly good index. No yellow triangle, no warning, no missing index hint. Nothing in the plan explains it, because the cause is a non sargable predicate, and that lives in the statement text.

What is a non sargable predicate in SQL Server and how do you find them? A non sargable predicate is a WHERE, ON or HAVING condition that stops SQL Server using an index seek, usually because a function wraps the column or a LIKE starts with a wildcard. The plan shows only a scan. Finding them means reading statement text, ranked by cost per day, which Database Health Monitor's Query Anti-Patterns report does.

Database Health Monitor has a report for exactly this gap, and it is the one place in the product that reads what your workload actually says rather than what the optimizer made of it. First, the mechanics.

Query Anti-Patterns is one of the reports in Database Health Monitor. It runs against your own servers, and it takes about a minute to have this same screen open on one of them.

Why the plan stays silent

Three statements, all of which compile without complaint:

  • WHERE YEAR(OrderDate) = 2025 wraps the column in a function, so the index on OrderDate cannot seek.
  • WHERE Surname LIKE '%smith' starts with a wildcard, so there is no sorted starting point to seek to.
  • WHERE (@Status IS NULL OR Status = @Status) gets one plan for every caller, whether they passed a value or not.

By the time a plan exists, the optimizer has already made its choice. The plan records the choice. It does not record the reason, so a scan caused by a function looks identical to a scan caused by a missing index. You can stare at the plan for an hour and learn nothing about how the query was written.

The check most people reach for first

Most of us sort the top queries by CPU, open the worst plan and look for warnings. It is a sensible habit and it misleads in two ways.

First, plan warnings cover spills, missing statistics and implicit conversions the plan can see. They do not cover a function on a column. Second, you fix one statement at a time and never learn that the same habit appears in forty others. The cost is spread across the application, so no single plan looks bad enough to justify the rewrite.

If you have already worked through wait based tuning, you know the value of asking which part of the cost matters. The same instinct applies to code. If scalar functions are your suspect, Scalar Function Performance: Read the Blocker Matrix First covers what inlining does and does not rescue.

Rank every non sargable predicate by cost per day

A pattern count is not the honest number. A habit in a statement that runs once a month is trivia. The same habit in a statement that runs 200,000 times a day is your week.

So the report weights every finding by cost per day: the statement's total CPU, reads or elapsed time divided by the age of its oldest plan, scaled to 24 hours. There is a floor of one hour on that age. Without it, a plan compiled a minute ago would read as a million executions a day. A pattern inside a procedure charges only the statement it sits in, not the whole procedure, so the ranking stays at statement level.

Statements come from the plan cache or from Query Store. Query Store needs SQL Server 2016 and looks back seven days, which includes plans that have already left the cache. The toolbar lets you read the Top 50, 200 or 1000 statements and decide whether cost means CPU, reads or elapsed time. That choice also decides which statements make the cut.

What the report puts on screen

Across the top is one bar per pattern, its length the summed cost per day, with the statement count printed on it. Red is worth stopping for. Amber is worth fixing. Slate is worth knowing.

That last point matters. The line under the title gives the share of cost in statements with at least one pattern, counted once. Quote that figure, not the sum of the bars.

The Together view turns the bars into a matrix of pattern against pattern. A catch all predicate with OPTION (RECOMPILE) is a known pair, and so is SELECT * with NOLOCK. The pair changes the advice, which is why the matrix is worth a look before you start rewriting.

The grid carries cost per day, runs per day, where the statement lives (procedure, trigger, ad hoc or prepared) and when it last ran. Select a row and the pane shows the text with each matched fragment highlighted. Below it, each pattern gets a two sentence explanation and, where one can be generated, the rewrite. YEAR(OrderDate) = 2025 becomes OrderDate >= '20250101' AND OrderDate < '20260101'. A catch all gets no mechanical rewrite. The pane says so and lists three real options instead.

Trusting a quiet result

The statement is tokenized before anything is matched. A NOLOCK inside a comment or a string is not a finding, and neither is a column named IsNoLockApproved. Where the text alone cannot decide, the pattern is left out. LIKE @p is not reported, for instance, because only the running parameter value knows whether it begins with a wildcard. The report would rather miss a pattern than invent one.

Encrypted modules are the other honest gap. They are counted with their cost and labeled not readable, never shown as clean. If the login cannot read sys.objects or sys.columns, the page tells you that scalar UDF and implicit conversion checks did not run, rather than reporting none.

What to do with the answer

Start at the top of the grid. Fix the red bars first, measuring each rewrite against the original with Open in Query Tuning Workbench. The report never runs anything itself. It reads and it suggests, and the change is yours to make and test.

If a statement you know is guilty is missing, it is outside the Top N, it has left the plan cache, or the function sits in the select list or on the literal side, where it does not block a seek. Raise the Top N or switch to Query Store. The Query Anti-Patterns documentation lists all eighteen patterns and the permissions each source needs.

A catch all or a function on a column often needs a better statistic alongside the rewrite, so check Index Statistics before you declare it done.

What to check on your own server

  • Run the report on your busiest database with cost set to CPU, then repeat with Reads and Elapsed and compare which statements move
  • Click the Function on column bar and read the generated rewrite in the pane for the most expensive statement
  • Open the Together view and look for catch all statements that also carry OPTION (RECOMPILE)
  • Check the line under the title for the share of cost sitting in statements with at least one pattern

Try Database Health Monitor Today

Query Anti-Patterns reads the text of your costliest statements and ranks the writing faults, such as functions on columns and catch all predicates, that no plan will ever flag. Database Health Monitor shows it on every instance you connect, in the time it takes to open the report.

Download Database Health Monitor and run the Query Anti-Patterns report against your own server. There is nothing to configure first, and you will know inside a few minutes whether it tells you something you did not already know.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To prove you are not a robot: *