HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

how to improve performance by using or not using table variables

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
improveusingperformancehowvariablesnottable

Problem

I have a table variable:

DECLARE @to_process TABLE 
(
    [Id] [bigint] NOT NULL,
    [SequenceId] [bigint] NOT NULL,
...
)

INSERT INTO @to_process
   (  Id
    , SequenceId
...
   )
  SELECT
    TOP (@recordsToProcess) 
      Id
    , SequenceId
...


in my stored procedure. I have investigated that insert into it spends about 66% of total execution time.

How can I improve or optimize my code to speed up my sp execution?

ADDED:

Solution

First thought...

If you have sufficient data to take this much % of the batch, use a #temptable.

When the table variable is used later, it is always assumed to have one row: there are no statistics on this table variable. So if you have several 1000, subsequent plans won't be optimal.

Temporary tables have statistics (and indexes if required etc) and can perform better for larger datasets

Context

StackExchange Database Administrators Q#3968, answer score: 7

Revisions (0)

No revisions yet.