DEV Community

LingamSuyambu
LingamSuyambu

Posted on AI-assisted

SQL Server Parameter Sniffing: Fast in SSMS, Slow in the App (6 Fixes)

Your stored procedure ran in 1 second yesterday. Today it takes 2 minutes. You copy the call into SSMS and it's fast again. Nothing was deployed.

Nine times out of ten, that's parameter sniffing.

What actually happens

When SQL Server compiles a stored procedure (or a parameterized query), it "sniffs" the parameter values of that first call and builds an execution plan that's ideal for those values. The plan goes into the plan cache and every later call reuses it.

That's normally a good thing: compiling is expensive. It goes wrong when your data is skewed:

CREATE OR ALTER PROCEDURE dbo.GetCustomerOrders
    @CustomerID INT
AS
BEGIN
    SET NOCOUNT ON;
    SELECT OrderID, OrderDate, TotalDue
    FROM dbo.Orders
    WHERE CustomerID = @CustomerID;
END;
GO

SET STATISTICS IO, TIME ON;
EXEC dbo.GetCustomerOrders @CustomerID = 7;   -- small customer: 5 rows
EXEC dbo.GetCustomerOrders @CustomerID = 42;  -- big customer: 500,000 rows, reuses the plan
Enter fullscreen mode Exit fullscreen mode

If customer 7 runs first, SQL Server caches an index seek + key lookup plan. Perfect for 5 rows. Then customer 42 reuses it and does 500,000 key lookups. That's your 2-minute query.

Why it's fast in SSMS

SSMS runs with SET ARITHABORT ON by default. Most .NET client libraries leave it OFF. ARITHABORT is part of the plan cache key, so your SSMS session compiles a brand new plan for the value you typed, while the app keeps reusing the bad one.

To test like the app:

SET ARITHABORT OFF;
EXEC dbo.GetCustomerOrders @CustomerID = 42;
Enter fullscreen mode Exit fullscreen mode

Prove it in under a minute

  1. Get the actual execution plan and compare estimated vs actual rows on the seek. Estimated 5, actual 500,000 = sniffing.
  2. Or ask Query Store which plans the procedure has used and how long each took:
SELECT p.plan_id, rs.count_executions, rs.avg_duration / 1000 AS avg_ms
FROM sys.query_store_query q
JOIN sys.query_store_plan p ON p.query_id = q.query_id
JOIN sys.query_store_runtime_stats rs ON rs.plan_id = p.plan_id
WHERE q.object_id = OBJECT_ID('dbo.GetCustomerOrders')
ORDER BY rs.avg_duration DESC;
Enter fullscreen mode Exit fullscreen mode

Two plans with wildly different durations for the same procedure is your answer.

6 fixes (and when to use each)

1. OPTION (RECOMPILE): compile a fresh plan on every call. Best for queries that run a few times a minute, not thousands of times a second (CPU cost).

... WHERE CustomerID = @CustomerID OPTION (RECOMPILE);
Enter fullscreen mode Exit fullscreen mode

2. OPTIMIZE FOR: tell the optimizer which value to plan for, or to use average density.

... OPTION (OPTIMIZE FOR (@CustomerID = 7));
... OPTION (OPTIMIZE FOR UNKNOWN);
Enter fullscreen mode Exit fullscreen mode

3. Force the good plan with Query Store: the fastest fix in production, with no code change.

EXEC sys.sp_query_store_force_plan   @query_id = 1234, @plan_id = 56;
EXEC sys.sp_query_store_unforce_plan @query_id = 1234, @plan_id = 56;
Enter fullscreen mode Exit fullscreen mode

4. Parameter Sensitive Plan (PSP) optimization: SQL Server 2022+ and Azure SQL can cache several plans for one query, one per range of parameter values.

ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 160;
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SENSITIVE_PLAN_OPTIMIZATION = ON;
Enter fullscreen mode Exit fullscreen mode

5. Split the logic: route big customers and small customers to two different procedures, so each gets its own plan.

6. Keep statistics fresh: stale stats make every estimate worse, sniffing or not.

Never do this in production

DBCC FREEPROCCACHE;  -- no arguments = every plan on the instance is thrown away
Enter fullscreen mode Exit fullscreen mode

Every query on the server recompiles at once and CPU spikes. If you must, clear one plan:

DBCC FREEPROCCACHE (<plan_handle>);
EXEC sp_recompile N'dbo.GetCustomerOrders';
Enter fullscreen mode Exit fullscreen mode

Watch the walkthrough

I made an 8-minute video that walks through the demo, the plans and every fix:

If this helped, I post practical SQL Server and Azure SQL videos on SQL & Cloud Lab.

Have you been hit by parameter sniffing in production? Which fix worked for you?

Top comments (0)