Table Variable vs Temp Table in SQL Server 2025: Measured, Not Assumed
Measured on SQL Server 2025: a table variable read thirty times the pages at one compatibility level, and six extra indexes made an insert eight times slower.
Every SQL Server estate inherits performance rules nobody can date. Never more than a hundred rows in a table variable. Always use a temp table. Never write SELECT star. Most of those rules started as a measurement, and somewhere between the conference talk and the team wiki the conditions fell away.
Two of the answers deserve recent evidence before you repeat them in a design review, because Pinal Dave measured both on SQL Server 2025 and published the numbers. The table variable vs temp table question is settled by a database setting, not by a row ceiling. And six extra indexes made a hundred thousand row insert eight times slower while the log grew about fifty five times as much.
Here are both measurements, five quicker verdicts on familiar rules, and a free Query Store script that catches plan regressions within 24 hours.
Table variable vs temp table: the compatibility level decides
The hundred row rule existed for a reason. Before SQL Server 2019 the optimizer assumed a table variable held one row, however many it actually held, and row estimates collapsed under real data. SQL Server 2019 added deferred compilation for table variables at compatibility level 150 and above: the statement compiles after the variable has been filled, so the real row count reaches the optimizer.
On 19 October 2026, Pinal Dave retested the premise on SQL Authority. Same procedure, same data: twenty thousand rows in the container joined to a four hundred thousand row table, run at two compatibility levels.
- At compatibility level 140, the table variable version read 60,035 pages.
- At level 170 it read 2,029 pages, exactly what the temp table version read.
About thirty times the reads, produced by a database setting, with no change to a line of SQL. Dave quotes logical reads rather than milliseconds, because single run timings disagreed with each other.
A temp table still wins some arguments. A table variable carries no column statistics, and its cached plan can be reused later for a very different row count. What the test retires is the flat ceiling as law: the decision follows the setting the database runs at, and plenty of production databases sit at level 140 because somebody upgraded the instance and never checked the database properties.
Check what your estate actually runs:
SELECT name, compatibility_level FROM sys.databases ORDER BY compatibility_level, name;
Where a database still runs level 140, the inherited rule holds, for the wrong reason: a setting nobody revisited.
What six extra indexes really cost on an insert
The write side folklore is vaguer. Everybody agrees indexes slow inserts down, "some". For a meeting you want a number, and there is one: one table, 100,000 rows in a single transaction, one load per configuration.
- No extra indexes: 414 milliseconds.
- Three extra indexes: 2,111 milliseconds.
- Six extra indexes: 3,159 milliseconds.
The log moved faster than the clock. Used log space per row measured 16.8 bytes with no extra indexes, 583.8 with three and 932.0 with six. On this load, six indexes ran about eight times slower and pushed the log needle about fifty five times as far.
Used log space is not the same as bytes written, because the engine reuses space as it goes: read it as the shape of the cost, not an invoice. And it is not an argument for stripping indexes in bulk: the same series notes an index which turns a nightly scan into a seek can pay for itself many times over. Both sides are measurable, and indexes only make sense when you price both sides, which is the argument behind The 8 Index Changes That Deliver Immediate Results.
Five more folklore checks, in fast forward
SELECT star. Against 100,000 matching rows, the two columns an index carried cost 491 logical reads. All six columns cost 9,339, nineteen times the reads. The plans explain it: the narrow version kept an Index Seek, the wide version abandoned the index and scanned the clustered index. The asterisk was not the expensive part. The columns were. An asterisk in a subquery nobody read from came back at 491 reads, so the rule is to know which columns your index carries and ask for no more than you will use.
ORDER BY inside a view. TOP 100 PERCENT plus ORDER BY compiles, looks sorted and is not. Five rows selected from exactly such a view came back in ascending clustered index order, and the plan held no sort operator: the ORDER BY was discarded at compile time. A view is a table expression with no order. Put the ordering in the query that needs it.
Heaps. A load once, read once staging heap is not a firing offence. Fifty thousand short rows occupied 230 pages in either container. Widened from 20 to 1,900 bytes, the heap reached 12,615 pages with 49,541 forwarded records against 24,082 clustered pages. Reading was where it hurt: 62,156 reads from the heap against 24,170 from the clustered table, two and a half times the reads on half the pages. Load it, read it, truncate it.
WHERE clause order. Rearranging three predicates to put the most selective first changed nothing: both orders produced the same plan hash and three logical reads. SQL is declarative, and nothing in the language lets you demand an evaluation order. Order predicates for whoever maintains the query.
Shrink after a big delete. TRUNCATEONLY did nothing while the free space sat mid file. A real shrink returned about 107 MB from a 200 MB file, but the outcome depends on the table: fragmentation stayed at 0.01 percent in one run and hit 99.99 percent on a second table carrying an extra index. Take the disk back when you need it, measure fragmentation afterwards, and leave the file alone when you do not.
Catch the regressions the rules never mention
A quieter failure is a query that ran well for months and turns slow overnight with nothing changed. Usually the plan changed, and parameter sniffing is the textbook example, covered in the parameter sniffing guide. The answer is Query Store, asked a specific question: which queries ran slower in the last 24 hours than in the seven days before?
Clive Roberts published a read-only script for exactly that on 15 October 2026, tested on SQL Server 2017 and SQL Server 2025. Run it in the database with Query Store on; it changes nothing. It flags queries whose recent average duration is at least twice the previous average and above 100 milliseconds, returning the worst 25 by the absolute duration increase.
-- Which queries got slower in the last 24 hours than in the 7 days before? -- Query Store must be on; read-only. SET NOCOUNT ON; WITH s AS ( SELECT p.query_id, rs.plan_id, AVG(CASE WHEN i.start_time >= DATEADD(HOUR, -24, SYSUTCDATETIME()) THEN rs.avg_duration END) AS recent_us, AVG(CASE WHEN i.start_time < DATEADD(HOUR, -24, SYSUTCDATETIME()) THEN rs.avg_duration END) AS before_us, SUM(rs.count_executions) AS executions FROM sys.query_store_runtime_stats AS rs JOIN sys.query_store_runtime_stats_interval AS i ON i.runtime_stats_interval_id = rs.runtime_stats_interval_id JOIN sys.query_store_plan AS p ON p.plan_id = rs.plan_id WHERE i.start_time >= DATEADD(DAY, -8, SYSUTCDATETIME()) GROUP BY p.query_id, rs.plan_id) SELECT TOP (25) s.query_id, s.plan_id, CONVERT(decimal(12,1), s.before_us / 1000) AS before_ms, CONVERT(decimal(12,1), s.recent_us / 1000) AS recent_ms, CONVERT(decimal(8,1), s.recent_us / NULLIF(s.before_us, 0)) AS times_slower, s.executions, qt.query_sql_text FROM s JOIN sys.query_store_query AS q ON q.query_id = s.query_id JOIN sys.query_store_query_text AS qt ON qt.query_text_id = q.query_text_id WHERE s.recent_us > 2 * s.before_us AND s.recent_us > 100000 ORDER BY s.recent_us - s.before_us DESC;
If the old plan is clearly better, sp_query_store_force_plan pins it back. Plan forcing freezes behaviour, so treat it as a stopgap while you find the underlying cause.
From folklore to measurement
None of this needs a laboratory. Most of Dave's answers took under ten minutes each to produce, and your test instance offers the same deal. Pick the inherited belief, write the test, record the number, then retire the rule or keep it with its conditions attached. That is the assess, fix and monitor discipline we describe in From Reactive to Proactive: Building a SQL Server Strategy, and it is the spirit of our health check. If you would rather hand the next batch of inherited rules to somebody who measures before advising, start at our services.
Need Expert SQL Help?
Our SQL optimization experts are ready to help you implement these strategies and optimize your database performance.
Schedule Free Consultation