Contact us
Insights

Testing parameter-sensitive queries

Fast for one customer, painfully slow for another? How to spot a parameter-sensitive query, test it, and choose a safe fix.

The problem

Fast yesterday, slow today, same query

SQL Server builds a plan for a query the first time it runs and reuses it. The plan suits the values used that first time. If those were for a small customer with ten orders, the plan may be terrible for your largest customer with fifty thousand. The query is fine one day and slow the next, depending on who ran it first after a restart or plan change.

Signs

Signs of a parameter-sensitive query

Speed depends on the value

The same report is quick for some customers, stores, or dates and slow for others.

It fixes itself, then returns

Performance changes after a restart, a statistics update, or an index rebuild.

Several plans in Query Store

Query Store shows the same query with very different plans and durations.

Skewed data

A few values account for a large share of rows, such as one big customer or one main warehouse.

Testing

How to test a sensitive query

Step 1 of 4

  1. The smallest, a typical, and the largest value for each parameter.

  2. Compare duration, reads, and the plan chosen for each value on a test copy.

  3. See how badly each value does with a plan built for another.

  4. Retest every value after the fix, not just the one that was slow.

Choose the gentlest fix that works

Options range from recompiling the query each time it runs, to an optimisation hint for typical values, to splitting the query for large and small cases, to a better index that suits every value. For example: a recompile hint costs a little CPU on every run, which is fine for a report run a few times a day and not for a query run thousands of times a minute.

Reports fast for some, slow for others?

Tell us which reports or screens vary and for which values. A few lines are enough.

Tell us what’s not working