Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a variable is declared in a T-SQL stored procedure, is it kept in memory or tempdb?

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.

like image 599
Evan M Avatar asked Mar 15 '12 21:03

Evan M


People also ask

Where are temp variables stored in SQL Server?

Both table variables and temp tables are stored in tempdb.

Does a table variable use tempdb?

Table variables are created in the tempdb database similar to temporary tables. If memory is available, both table variables and temporary tables are created and processed while in memory (data cache).

Where are variables stored in SQL?

It is stored in the tempdb system database. The storage for the table variable is also in the tempdb database. We can use temporary tables in explicit transactions as well. Table variables cannot be used in explicit transactions.

Is table variable stored in memory?

A very popular (and incorrect) understanding is that Table Variables are stored in only memory and they do not exist on the disk. This is the reason they are faster and efficient.


2 Answers

The Capacity Planning article for tempdb answers your questions:

The large object data types are varchar(max), nvarchar(max), varbinary(max) text, ntext, image, and xml. These types can be up to 2 GB in size and can be used as variables or parameters in stored procedures, user-defined functions, batches, or queries. Parameters and variables that are defined as a LOB data type use main memory as storage if the values are small. However, large values are stored in tempdb. When LOB variables and parameters are stored in tempdb, they are treated as internal objects. You can query the sys.dm_db_session_space_usage dynamic management view to report the pages allocated to internal objects for a given session.

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:

select * 
from sys.dm_db_session_space_usage 
where session_id = @@SPID

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.

like image 148
Michael Fredrickson Avatar answered Oct 31 '22 17:10

Michael Fredrickson


"Table variables are stored in tempdb. These I'm not really interested in though."

In general yes, table variables are stored in tempdb, but this could be changed with memory-optimized table variables.

Faster temp table and table variable by using memory optimization

D. Scenario: Table variable can be MEMORY_OPTIMIZED=ON

A traditional table variable represents a table in the tempdb database. For much faster performance you can memory-optimize your table variable.

The inline syntax does not support memory-optimization. So let us convert the inline syntax to the explicit syntax for the TYPE.

CREATE TYPE dbo.typeTableD AS TABLE  
(  
    Column1  INT   NOT NULL INDEX ix1,  
    Column2  CHAR(10)  
) WITH (MEMORY_OPTIMIZED = ON);

DECLARE @tvTableD dbo.typeTableD;
INSERT INTO @tvTableD (Column1) values (1), (2);  

SELECT * FROM @tbTableD;

A memory-optimized table variable does not reside in tempdb. Memory-optimization results in speed increases that are often 10 times faster or more.

like image 30
Lukasz Szozda Avatar answered Oct 31 '22 18:10

Lukasz Szozda