We’re trying to optimize some of our T-SQL stored procedures to reduce tempdb contention, but I can’t figure out how non-table variables are stored by SQL server:
- What about simple data types like INT and DATETIME? It feels like they’d live in memory.
- What about VARCHARs/VARCHAR(MAX)? A regular VARCHAR could live in memory, but a VARCHAR(MAX) might need to use tempdb for its storage.
- Table variables are stored in tempdb. These I’m not really interested in though.
The MSDN article on tempdb doesn’t explain regular variables.
The Capacity Planning article for tempdb answers your questions:
The article is worth reading in its entirety, because it also covers a lot of the other uses for tempdb.
EDIT: If you’re curious how much memory in tempdb a specific session is using, you can run the following query:
Using this, it didn’t look like my
VARCHAR(MAX)variable was stored in tempdb until it reached around 1000 KB in size… but I’m sure that varies based on the memory that your server has available.