Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
SQL

SQL Server: Fix Parameter Sniffing on a Seasonal Reporting Query

- 30.11.26 - ErcanOPAK

🗄️ Fix Parameter Sniffing on a Seasonal Reporting Query

SQL Server caches a query plan based on the parameter values present the very first time it’s compiled, and a seasonal reporting query (think: ‘orders in the last N days’) that first compiles during a quiet period gets a plan optimized for a tiny row estimate — the same cached plan then gets reused during a high-volume season with a completely different actual row count, and a plan built for ‘small’ performs badly at ‘huge’ because the optimizer never got a chance to reconsider.

🔎 The Problem

CREATE PROCEDURE rpt.GetOrdersInRange @Days INT AS
BEGIN
  SELECT * FROM Orders WHERE OrderDate >= DATEADD(day, -@Days, GETDATE());
END
-- First compiled with @Days = 7 (quiet period, ~400 rows)
-- plan uses a Key Lookup per row - fine at 400 rows,
-- catastrophic when reused during a @Days = 90 holiday report (800k rows)

✅ Fix: Give the Optimizer a Plan That Fits the Actual Range Size

  • Add OPTION (RECOMPILE) to the specific statement if it’s run infrequently enough that recompiling every execution is cheaper than risking a badly-fitted cached plan.
  • For a query run often enough that recompiling every time is wasteful, use OPTION (OPTIMIZE FOR (@Days UNKNOWN)) to get a plan based on average distribution statistics instead of whatever happened to compile first.
  • Split genuinely different-shaped workloads (a ‘recent orders’ dashboard vs. a ‘full season’ report) into separate stored procedures so each gets its own cached plan tuned to its own typical row count.

The plan isn’t wrong for the data it was compiled against — it’s wrong for the data that showed up six months later.

— Database Engineer

Related posts:

SQL Server: Why NULL Values Break a Simple NOT IN Subquery

SQL Server: Why NULL Values Break a Simple NOT IN Subquery

SQL Window Functions — The Fastest Way to Do Ranking & Aggregates

SQL Window Functions — The Fastest Way to Do Ranking & Aggregates

How to get stored procedure parameters details in SQL

How to get stored procedure parameters details in SQL

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Asp.Net Core: Fix Razor Pages Page Filter Running Before Auth
C#: Fix a Tuple Deconstruction Hiding a Discarded Exception

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com