When working with SQL Server, developers often need to store temporary data for intermediate calculations. SQL Server provides two primary methods to handle this:
- Table
Variables (@table_variable)
- Temporary
Tables (#temp_table)
While both serve similar purposes, they differ significantly
in performance, scope, and use cases. This article explores their differences
with execution plans and real-world examples.
1. What is a Table Variable?
A Table Variable is a variable that holds data in a
table structure but with limited scope and functionality. It is stored
primarily in memory, though it can spill to disk when necessary.
Declaration and Usage
DECLARE @EmployeeTable TABLE (
ID INT PRIMARY
KEY,
Name VARCHAR(100),
Salary
DECIMAL(10,2)
);
INSERT INTO @EmployeeTable (ID, Name, Salary)
VALUES (1, 'John Doe', 5000), (2, 'Jane Smith', 6000);
SELECT * FROM @EmployeeTable;
Key Characteristics
- Scoped
to the batch or procedure where it is declared.
- No
statistics are maintained, leading to poor execution plan
optimization.
- Cannot
be altered after declaration.
- Limited
indexing capabilities (only PRIMARY KEY and UNIQUE constraints are
allowed).
- Does
not participate in transactions (rollback has no effect).
2. What is a Temporary Table?
A Temporary Table is a table that exists in the tempdb
database and provides better performance for larger datasets due to indexing
and statistics support.
Declaration and Usage
CREATE TABLE #EmployeeTemp (
ID INT PRIMARY KEY
CLUSTERED,
Name VARCHAR(100),
Salary
DECIMAL(10,2)
);
INSERT INTO #EmployeeTemp (ID, Name, Salary)
VALUES (1, 'John Doe', 5000), (2, 'Jane Smith', 6000);
SELECT * FROM #EmployeeTemp;
Key Characteristics
- Stored
in tempdb and supports transactions.
- Supports
full indexing, including Clustered and Non-Clustered Indexes.
- Statistics
are generated, improving query performance.
- Can
be altered (ALTER TABLE) after creation.
- Scope
lasts until the session ends or explicitly dropped.
3. Key Differences Between Table Variables and Temporary
Tables
Feature |
Table Variable (@table_variable) |
Temporary Table (#temp_table) |
Scope |
Limited to the batch/procedure. |
Exists in tempdb until explicitly dropped or session ends. |
Transaction Logging |
Minimal logging, does not support rollback. |
Fully logged and supports rollback. |
Indexing |
Only PRIMARY KEY and UNIQUE constraints. |
Supports full Clustered and Non-Clustered Indexes. |
Statistics |
❌ No statistics, optimizer
assumes 1 row. |
✅ Maintains statistics,
improving performance. |
Recompilation |
Does not cause stored procedure recompilation. |
May cause recompilation. |
Performance |
Faster for small datasets (stored in memory). |
More efficient for larger datasets (stored in tempdb). |
Supports ALTER? |
❌ No ALTER TABLE support. |
✅ Can use ALTER TABLE to modify
structure. |
4. Execution Plan Comparison
Scenario:
We insert 10,000 records into both a Table
Variable and a Temporary Table, then perform a JOIN operation
and analyze the execution plan.
Using Table Variable
DECLARE @EmployeeTable TABLE (
ID INT PRIMARY
KEY,
Name VARCHAR(100),
Salary
DECIMAL(10,2)
);
INSERT INTO @EmployeeTable
SELECT TOP 10000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
'Employee ' +
CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR(10)),
5000
FROM master.dbo.spt_values;
SELECT * FROM @EmployeeTable;
Execution Plan Analysis:
- No statistics
are created, so SQL Server assumes 1 row exists.
- Uses
Nested Loops Join, which is inefficient for large datasets.
- No parallelism.
Using Temporary Table
CREATE TABLE #EmployeeTemp (
ID INT PRIMARY KEY
CLUSTERED,
Name VARCHAR(100),
Salary
DECIMAL(10,2)
);
INSERT INTO #EmployeeTemp
SELECT TOP 10000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
'Employee ' +
CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS VARCHAR(10)),
5000
FROM master.dbo.spt_values;
SELECT * FROM #EmployeeTemp;
Execution Plan Analysis:
- Statistics
are generated, so SQL Server correctly estimates 10,000 rows.
- Uses
Hash Join / Merge Join, which is more efficient.
- Can
leverage parallelism, improving performance.
5. When to Use Table Variables vs Temporary Tables
Scenario |
Best Choice |
Small datasets (<1,000 rows) |
Table Variable (@table_variable) |
Need rollback in a transaction |
Temporary Table (#temp_table) |
Need indexing beyond PRIMARY KEY |
Temporary Table (#temp_table) |
Need to modify table structure dynamically |
Temporary Table (#temp_table) |
Inside a stored procedure (avoiding recompilation) |
Table Variable (@table_variable) |
Conclusion
- Use
Table Variables when working with small datasets and avoiding
recompilation.
- Use
Temporary Tables when working with large datasets, transactions,
or complex indexing.
Understanding these differences will help optimize SQL
Server performance and ensure efficient query execution. 🚀
No comments:
Post a Comment