26 performance tuning questions and solutions, difference between temp table and table variable in sql server 2008, difference table variable and temp table, Differences between SQL Server temporary tables and table variable, How to tune SQL queries, Interview questions for SQL Server Performance Tuning, Looking for SQL Optimization Interview Questions, performance sql server, Performance tips for faster SQL queries, Performance Tuning, Performance Tuning for SQL Server, Query Optimization, Query Performance Tuning, SQL Complex Queries, SQL Optimization Interview Questions, sql performance, sql performance and tuning, sql performance explained pdf, sql performance tips, SQL Performance Tuning, sql performance tuning and optimization, sql performance tuning interview questions, sql performance tuning tips, SQL Query Optimizer, SQL Query Tuning or Query Optimization, SQL SERVER – Difference TempTable and Table Variable, SQL SERVER Interview questions, SQL server optimization interview questions and answers, sql server performance query, sql server performance slow, SQL Server Performance Tuning, SQL Server Performance Tuning Tips, SQL SERVER Tips, SQL Tuning Overview, table variable statistics, Table Variable V/S Temporary Table, table variable vs temp table, table variable vs temp table in sql server, table variable vs temp table in sql server 2008, table variable vs temp table in sql server performance, temp table and table variable, temp table and table variable difference, temp table and table variable performance, Temporary Tables vs. Table Variables and Their Effect on SQL performance, Tips for SQL Database Tuning and Performance, Top 10 performance tuning tips for relational databases, What's the difference between a temp table and table variable, Yet Another Temp Tables Vs Table Variables Article
Which one is better | Temp Table or Table Variable?
Download PDF – Which one is better | Temp Table or Table Variable?
Whether to use temp tables or table variables is an old interesting question since they were first introduced. In this post we will touch-base the differences between the table variable and Temp Tables.
Let’s check out the differences below then we will see which is better for performance.
|Detail||Temp Tables||Table Variables|
|Storage Location||Stored in TempDB||Stored in TempDB|
|Indexes||Yes. Temp table allows both clustered and non-clustered indexes||1 PK/UK Only|
|Types||Yes – Local and Global||No|
|Log file writing||Yes||Yes|
|Insert with Exec||Yes||Yes|
|Statistics||Yes||No(1 Row always)|
Temp tables are like normal SQL tables that are defined and stored in TempDB. The only difference between Temp table and a physical table is that temp table doesn’t allow foreign keys.
You can view the temp tables currently defined via SSMS by going to TempDB and Expanding Temp Tables.
As per my experience temp tables are better than table variables. The problem with the table variables is that query optimizer will generate bad/unpredictable query plans as they don’t have statistics on them.
If you check the estimated number of rows then it will always be 1.
DECLARE @tab AS TABLE ( ID INT ) INSERT INTO @tab SELECT TOP 100 [CustomerID] FROM [dbo].[CustomerInfo] SELECT ID FROM @tab Output see below image -----------
As a general rule of thumb across many SQL SERVER communities is that if you have 1000 rows or less then go for table variable otherwise go for temp table.
Well we can influence the query optimizer. One of the ways is given below-
DECLARE @x AS INT = 100 DECLARE @tab AS TABLE ( ID INT ) INSERT INTO @tab SELECT TOP (@x) [CustomerID] FROM [dbo].[CustomerInfo] Output -----------
Table variables do not qualifying for parallelism that’s why they are better suited for small amounts of data.
Table Variables can write to the disk if threshold goes over a certain number of records.
Table variables do write to the log file and they can have non clustered indexes if associated with a ‘NONCLUSTERED UNIQUE’ or ‘NONCLUSTERED PRIMARY KEY’ constraint.
Pingback: SQL Server – Table Variables & Estimated No of Rows | Enhance your SQL Server & MSBI Stack Skills