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 a Table Variable Breaking a Parallel Query Plan

- 26.11.26 - ErcanOPAK

🗄️ SQL Server: Fix a Table Variable Breaking a Parallel Query Plan

A table variable carries no column-level statistics for the optimizer to use at all, unlike a temp table – SQL Server always falls back to a generic, far less accurate row-count assumption for a table variable regardless of how many rows it actually holds at execution time. Since parallelism is a cost-based decision driven heavily by each operator’s estimated row count, a badly underestimated table variable can convince the optimizer a plan is cheap enough to run serially even while it’s actually processing hundreds of thousands of real rows, producing a slow single-threaded plan for work that would otherwise easily qualify for, and benefit from, a parallel one.

🔎 The Problem

DECLARE @OrderIds TABLE (Id INT PRIMARY KEY);
INSERT INTO @OrderIds SELECT Id FROM Orders WHERE Status = 'Pending';
-- @OrderIds now holds 300,000 rows

SELECT o.* FROM Orders o
JOIN @OrderIds t ON o.Id = t.Id;
-- Execution plan: SERIAL, single thread
-- Estimated rows at the table variable scan: 1
-- Actual rows at the table variable scan: 300,000

✅ The Fix

  • Switch the table variable to a temp table (#OrderIds) whenever the row count is expected to be nontrivial, so the optimizer can gather and use real statistics on it instead of falling back to its generic fixed assumption.
  • If a table variable needs to be kept for a specific reason (its transaction-scope behavior, or avoiding recompiles in a frequently-called routine), add OPTION (RECOMPILE) to the query using it, letting the optimizer size the plan against the variable’s actual runtime row count instead of the compile-time guess.
  • When a query involving a table variable runs slower than expected, check the actual execution plan’s estimated-vs-actual row counts at that specific operator first, before investigating indexes or hardware – this exact mismatch is the first thing worth ruling out.

🚨 Why This Is Dangerous

Because the query still returns completely correct results, a performance regression caused purely by table-variable cardinality estimation can go undiagnosed for a long time, with the slowdown blamed on ‘the server getting busier’ or ‘the data growing’ rather than a specific, fixable optimizer behavior.

SQL Server doesn’t guess how many rows are in your table variable – it assumes, and that assumption doesn’t get more accurate just because your data did.

— Database Engineer

Related posts:

Missing WHERE on UPDATE — Disaster Prevention Trick

Missing WHERE on UPDATE — Disaster Prevention Trick

SQL Server: Fix an Identity Column Gap After a Failed Bulk Insert

SQL Server: Fix an Identity Column Gap After a Failed Bulk Insert

SQL Server Execution Plan Analysis: Find and Fix Slow Queries Like a Pro

SQL Server Execution Plan Analysis: Find and Fix Slow Queries Like a Pro

Views: 10
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

WordPress: Fix a Bloated Autoload Option Slowing Every Page Load
C#: Fix a SemaphoreSlim Leak From a Forgotten Release on Throw

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 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 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# 868 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