For The Last Time, It Doesn't Matter How Many Rows You're Putting In A Table Variable

Опубликовано: 19 Март 2026
на канале: Erik Darling (Erik Darling Data)
2,070
136

In this video, I dive into the nuances of using table variables in SQL Server, challenging a common piece of Microsoft advice that suggests they are fine as long as there are fewer than 150 rows. I argue that the number of rows is irrelevant; what truly matters is how you use the table variable later on. I walk through scenarios where table variables can lead to poor query plans and execution times, especially when joining them with larger tables. By comparing table variables to temporary tables, I highlight why temp tables are generally a better choice for complex operations due to their ability to generate accurate cardinality estimates. The video also delves into the technical details of how SQL Server handles statistics on these objects, emphasizing that table variables lack this crucial information, leading to suboptimal query plans.

CHAPTERS

00:00:00 - Introduction
00:00:56 - Common Rejections
00:01:20 - When Not to Use Table Variables
00:01:55 - Relational Activity Considerations
00:03:16 - Better Alternatives
00:04:00 - Conclusion
00:04:12 - Example Scenario
00:04:24 - Table Variable Setup
00:05:02 - User Data Explanation
00:05:43 - Cardinality Estimation Issues
00:06:51 - Performance Comparison
00:07:16 - Query Plan Analysis
00:08:03 - Operator Details
00:09:00 - Temp Table Variables
00:10:10 - Incorrect Estimates
00:11:06 - Parallelism Example
00:12:01 - Trace Flags and Statistics
00:14:12 - Temp Table Benefits
00:15:44 - Usage Considerations
00:17:01 - Gym Analogy
00:18:31 - Final Thoughts

━━━━━━━━━━━━━━━━━━━━━━━━━━
📚 TRAINING & COURSES
━━━━━━━━━━━━━━━━━━━━━━━━━━
Get AI-Ready With Erik
https://training.erikdarling.com/get-...

SQL Server Performance Engineering Course
https://training.erikdarling.com/sql-...

Learn T-SQL with Erik
https://training.erikdarling.com/lear...

Everything Bundle:
https://training.erikdarling.com/?cou...

━━━━━━━━━━━━━━━━━━━━━━━━━━
🛠️ CONSULTING & SERVICES
━━━━━━━━━━━━━━━━━━━━━━━━━━
Need SQL Server performance help?
https://training.erikdarling.com/sqlc...

━━━━━━━━━━━━━━━━━━━━━━━━━━
💬 CONNECT
━━━━━━━━━━━━━━━━━━━━━━━━━━
Ask questions at Office Hours
https://erikdarling.com/officehours/

Become a channel member
   / @erikdarlingdata  
━━━━━━━━━━━━━━━━━━━━━━━━━━