When creating temp tables within a stored procedure, is it good practice to drop them as soon as they are no longer required?

Maintaining temp tables uses up SQL Server resources, so the sooner you delete them after you are done using them, the better. For most SQL Servers, there is probably not much of a performance difference if you decide to delete them later than sooner, but on a very busy SQL Server that manages lots of temp tables, the difference might be noticeable.

Ideally, for best SQL Server performance, you should avoid using temp tables in the first place. Instead, consider using one of these “lower overhead” options instead of using a “higher overhead” temp table.

  • Rewrite your code so that the action you need completed can be done using a standard query or stored procedure.

  • Use a derived table.

  • Use the SQL Server 2000 or 2005 “table” datatype.

  • Consider using a correlated sub-query. 

  • Use a permanent table instead.

  • Use a UNION statement to mimic a temp table.


No comments yet... Be the first to leave a reply!