When you have data modification statements
(like, INSERT,UPDATE,DELETE) against a table variable,
The SQL Server will not use a parallel plan, it will use a serial plan.
This can affect the performance, so keep it in mind.
Although, there is a workaround:
INSERT INTO @Table
EXEC('SELECT .. FROM ...')
Additionally,
Until SQL Server 2019 which has "Table variable deferred compilation" feature,
the estimated number of rows of table variables was always 1.
Why? When a query/SP/batch is compiled and optimized, the table variable is empty.
The SQL Server will not use a parallel plan, it will use a serial plan.
This can affect the performance, so keep it in mind.
Although, there is a workaround:
INSERT INTO @Table
EXEC('SELECT .. FROM ...')
Additionally,
Until SQL Server 2019 which has "Table variable deferred compilation" feature,
the estimated number of rows of table variables was always 1.
Why? When a query/SP/batch is compiled and optimized, the table variable is empty.
If you wanted to have proper estimations then you need to use
OPTION(RECOMPILE) hint or use trace flag 2453,
so the optimize will use real row count.
Note, OPTION(RECOMPILE) hint will not always work when
you join the table variable to a table and have 1 to many relationship.
See https://www.sqlservercentral.com/blogs/should-i-use-optionrecompile for more details.
Also, the situation is different when you have some filters on your table variable.
Because there are no statistics on the table variables,
SQL Server will use the following rules for the estimation of rows:
- When there is equal filter the estimates will be 10 % out of total rows,
- When there are inequality filters, like <, >, <> the estimates will be 30 % out of total rows
- When there is an equal filter on a bit field the estimates will be 50 % out of total rows
Source articles:
https://www.brentozar.com/archive/2017/02/using-trace-flag-2453-improve-table-variable-performance/
https://support.microsoft.com/en-us/help/2952444/fix-poor-performance-when-you-use-table-variables-in-sql-server-2012-o
- When there is equal filter the estimates will be 10 % out of total rows,
- When there are inequality filters, like <, >, <> the estimates will be 30 % out of total rows
- When there is an equal filter on a bit field the estimates will be 50 % out of total rows
Source articles:
https://www.brentozar.com/archive/2017/02/using-trace-flag-2453-improve-table-variable-performance/
https://support.microsoft.com/en-us/help/2952444/fix-poor-performance-when-you-use-table-variables-in-sql-server-2012-o
No comments:
Post a Comment