Create Index on Table Variable

Creating an index on a table variable can be done implicitly within the declaration of the table variable by defining a primary key and creating unique constraints. The primary key will represent a clustered index, while the unique constraint a non clustered index.

[cc lang=”sql”]
DECLARE @Users TABLE
(
UserID INT PRIMARY KEY,
UserName varchar(50),
UNIQUE (UserName)
)
[/cc]

The drawback is that the indexes (or constraints) need to be unique. One potential way to circumvent this however, is to create a composite unique constraint:
[cc lang=”sql”]
DECLARE @Users TABLE
(
UserID INT PRIMARY KEY,
UserName varchar(50),
FirstName varchar(50),
UNIQUE (UserName,UserID)
)
[/cc]

You can also create the equivalent of a clustered index. To do so, just add the clustered reserved word.
[cc lang=”sql”]
DECLARE @Users TABLE
(
UserID INT PRIMARY KEY,
UserName varchar(50),
FirstName varchar(50),
UNIQUE CLUSTERED (UserName,UserID)
)
[/cc]

Generally, temp tables perform better in situations where an index is needed. The downfall to temp tables is that they will frequently cause recompilation. This was more of an issue with SQL 2000 when compilation was performed at the procedure level instead of the statement level. SQL 2005 and above perform compilation at the statement level so if only one statement utilizes a temp table then that statement is the only one that gets recompiled. Contrary to popular belief, table variables can and do write to disk.

14 comments
Tom Kaye 20 May 2015 at 5:49 am

Thanks Derek!!!!
To echo another comment… 5+ years later, your tip solved my performance issue! My procedure went from over 7 minutes to 5 seconds!

Derek Dieter 11 Jun 2015 at 4:28 am

Yay!!!! Good to hear Tom.

Teddy H 10 Dec 2015 at 8:45 pm

MIND BLOWN. Never heard of using indexes on table variables. Had a long running procedure that went from 8 1/2 minutes down to 1.3 seconds. LIFE SAVER!

Suelen APC 11 Mar 2015 at 7:29 pm

My query is much faster…

Thank you very much!

Derek W 15 May 2014 at 11:43 am

Thank you Derek ( From Derek W)
My query time went from 25 mins to 1min 22secs ! Four years after posting you are still helping people!

Anonymous 14 May 2012 at 12:04 pm

Hey Derek this is one of your x-coWorkers from resMAE I can see your hair hasnt grown back out yet (j/k ) anyway “pal” is good to see you.

Ed Graham 18 Jan 2012 at 6:16 am

Fantastic tip, particularly after I read on StackOverflow that it couldn’t be done. My query involving an inner join on a table without a PK went from 3m 23s to 17s!

Julianne 23 Sep 2011 at 11:07 am

Great Derek! Thanks for the tip:)

Julianne 23 Sep 2011 at 11:06 am

Greate Derek!

Daniel N 24 Aug 2011 at 12:28 am

Awesome tip, my query batch went from 7 minutes down to 12 seconds.

Adam 25 Jul 2011 at 1:53 am

Blimy, this optimised my stored procedure. It took it down from 6 mins to 22 seconds. Thank you.

George 02 Apr 2010 at 4:58 am

Thanks Derek. A unique composite constraint helped drop the execution time of my sproc by half.

Derek D. 02 Apr 2010 at 9:10 am

Great George that’s awesome. Nice to see when these things help.

Twitted by SSISBI 23 Nov 2009 at 10:39 pm

[…] This post was Twitted by SSISBI […]

Featured Articles

 Site Author

  • Thanks for visiting!
css.php