Question

Am I safe to assume that where I have stored procedures using the tempdb to write a temporary table, I'd be better off switching these to table variables to get better performance?

Was it helpful?

Solution

Temp tables are better in performance. If you use a Table Variable and the Data in the Variable gets too big, the SQL Server converts the Variable automatically into a temp table.

It depends, like almost every Database related question, on what you try to do. So it is hard to answer without more information.

So my answer is, try it and have a look at the execution plan. Use the fastest way with the lowest costs.

OTHER TIPS

@Table can be faster as there is less "setup time" since the object is in memory only.

@Tables have a lot of catches though.

You can have a primary key on a @Table but thats about it. Other indexes Clustered NonClustered for combinations of columns are not possible.

Also if your table is going to contain any real data volumes (more then about 200 maybe 1000 rows) then accessing the table will be slower. Especially when you will probably not have a useful index on it.

#Tables are a pain in procs as they need to be dropped when debugging, They take longer to create. and they take longer to setup as you need to add indexs as a second step. But if you have lots of data then its #tables every time.

Even in cases where you have less then 100 rows of data in a table you may still want to use #Tables as you can create a usefull index on the table.

In summary i use @Tables most of the time for the ease when doing simple proc etc. But anything that need to perform should be a #Table.

@Tables have no statistics so the execution plan entails more guesswork. Hence the recommended upper limit of 1000-ish rows. #Tables have statistics but these can be cached between invocations. If your cardinalities differ significantly each time the SP runs you'd want to REBUILD and RECOMPILE each time. This is an overhead, of course, but one which must be balanced against the cost of a rubbish plan.

Both types will do IO to TempDB.

So no, @Tables are not a panacea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top