SHARE
Facebook X Pinterest WhatsApp

Improving the Performance of a Table Variable using Optimizer Hint RECOMPILE

Written By
thumbnail
Gregory Larsen
Gregory Larsen
Apr 1, 2021

Table Variables have been known to run slowly when the table variable contains lots of rows. This is because when a batch is compiled the table variable has yet to be populated with any rows, and therefore the optimizer uses an estimated row count of 1 for table variables. This occurs because table variables don’t have statistics. Having an estimated row count of 1 works well for operators that have small row counts, like a NESTED LOOP operator for a join operation. Operators that perform well for small numbers of rows do not always scale when large numbers of rows are involved. Therefore, to improve the performance of your code that use table variables that contain lots of rows, you should consider adding the optimizer hint RECOMPILE as shown below:

SELECT O.[Order Key], TV.[City Key]
FROM Fact.[Order] as O JOIN @MyCities as TV
ON O.[City Key] = TV.[City Key]
OPTION (RECOMPILE)

By adding the optimizer hint RECOMPILE, a statement that uses a table variable will be recompiled after the temporary table has been populated. This will allow the optimizer to know how many rows are in the table variable. Once the optimizer has a better guess at the actual number of rows in a table variable, then it has a much better chance of picking an operator that works well for larger record sets, like a Hash Match for a join operation.  

If you find your Table Variable queries are not performing well then you might consider adding the optimizer hint RECOMPILE to see if your query starts performing better. Keep in mind with version 15 of SQL Server, Microsoft introduced a feature called “Table Variable Deferred Compilation” that solves the problem mentioned here, without the use of option RECOMPILE.

Recommended for you...

SQL SORT BY Statement
Ronnie Payne
Mar 21, 2023
Working with NULL Values in SQL
Ronnie Payne
Mar 9, 2023
Working with SQL AND, OR, and NOT Operators
Ronnie Payne
Feb 21, 2023
How to Use Databases With Python
technologyadv
Feb 10, 2023
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.