Prompt Engineering SQL SQL Server

SQL Server Query Tuning — Plans, Sniffing, Clustering Keys

Fast for most customers, 30x slower for the largest two — the parameter sniffing signature. SQL Server tuning with actual plans, clustering-key economics, and CONVERT_IMPLICIT hunting.

Overview

SQL Server tuning has engine-specific realities that generic SQL advice misses entirely. This prompt carries them: demand actual execution plans with SET STATISTICS IO, TIME ON (estimated plans hide spills and estimate-vs-actual gaps); reason about the clustered index — every nonclustered index carries the clustering key, so a wide key taxes every index; check parameter sniffing when performance varies by parameter — the loaded scenario is exactly that signature, fast for most customers and 30x slower for the largest; and watch for CONVERT_IMPLICIT in the plan, where a VARCHAR/NVARCHAR mismatch turns seeks into scans. A high-execution Key Lookup gets flagged as the covering-index candidate it is.

How to use this resource

  1. Demand the actual plan

    SET STATISTICS IO, TIME ON plus the actual plan — estimates hide exactly what matters.

  2. Check the sniffing signature

    Performance varying by parameter value points at a plan compiled for unrepresentative values — evidence before OPTIMIZE FOR.

  3. Cost against the clustering key

    Every index recommendation accounts for what the clustered index makes it carry.

Why This Works

  • Engine-specific guidance replaces the advice that ignores how SQL Server actually works
  • The sniffing check turns a classic mystery into a testable hypothesis
  • Clustering-key economics keep index advice from being free-lunch thinking

Best for

  • .NET and SQL Server shops tuning production queries
  • Parameter-dependent performance mysteries
  • Plans that look fine estimated and terrible actual

Not for

  • PostgreSQL or MySQL tuning — platform modes exist because the realities differ
  • Investigating whether the database is the problem at all — that's the Debugging Prompt Generator

Use cases

  • Diagnosing queries fast for some parameters and slow for others
  • Reading actual plans for spills and key lookups
  • Costing nonclustered indexes against a wide clustering key

FAQ

Why does this prompt insist on the actual execution plan instead of the estimated one?

Estimated plans hide the two things this tuning depends on. The DATABASE CONTEXT requires the Actual Execution Plan with SET STATISTICS IO, TIME ON, since estimates conceal spills and estimate-vs-actual gaps. Where no plan is pasted, the prompt refuses to invent one and instead outputs the exact capture command, marking every plan-dependent conclusion as pending that evidence.

Is this SQL prompt safe to use on PostgreSQL or MySQL queries?

It's SQL Server-specific and will misfire elsewhere. The engine-specific rules — CONVERT_IMPLICIT hunting for VARCHAR/NVARCHAR mismatches, clustering-key economics where every nonclustered index carries the clustering key, and OPTIMIZE FOR/RECOMPILE for parameter sniffing — only hold on SQL Server. notFor points Postgres and MySQL to their own platform modes because the realities differ.

My query is fast for most customers but 30x slower for the biggest two — what does this prompt point at?

That signature points straight at parameter sniffing. The prompt flags performance that varies by parameter as a plan compiled for unrepresentative values, and the loaded scenario — same query, same plan cache, 30x slower for the two largest customers — is exactly that pattern. It still gates OPTIMIZE FOR or RECOMPILE behind evidence from the actual plan rather than applying them blind.

More resources from SQL Optimization Prompt

Resources that pair well

Related tools

Tip: Save time by exploring related resources and tools that integrate with this resource.