views:

211

answers:

3
+1  Q: 

How tempDB works?

I am trying to understand the tempDB and following are the doubts popping in my mind.

  1. What is the lifetime of data in tempDB? Say a query is doing some Order By and uses tempDB for performing that. After this query finishes, someone else also executes a query which utilizes the tempDB. Will the second query find records written by first query in the tempDB or will they be deleted?
  2. Are there any visible tables created inside the tempDB by the Sql Engine? How can I know which temporary table is created because of this query? Is there any naming convention followed by the Sql engine for naming these temporary tables?

I am new to tempDB so please pardon me for asking such silly (if at all) questions :-)

It will be very nice if someone can point me to a good resource which can help me learn about tempDB.

+1  A: 
  1. The temporary tables created in TempDB are dropped when the query is completed.

  2. I'm not sure on this (I would have to try it), but I think theoretically ALL tables created in TempDB are visible, although only the user that created the table has permission to access it.

Maximilian Mayerl
And is there any naming convention followed for the temp tables by the sql engine.
peakit
They are not dropped when the query is completed but when the connection is dropped or if the table itself is manually dropped.
HLGEM
I don't know, sorry. As far as I know, manually created temp tables (in the style #name) at least begin with this "#name" and then have an underscore and some numbers at the end. something like #name_0123456789. I don't know if this numbers have some special meaning though. AS for tables created for SORT, UNION etc., I have no idea.
Maximilian Mayerl
@HLGEM: Manually created temp table, yes. But tables auto-generated for sorting, joining etc. are dropped when the query is completed.
Maximilian Mayerl
Just to clarify, issuing DROP TABLE #foo (or dropping the connection, or exiting the scope) does not remove the table from tempdb right away. Yes you can no longer access it since your connection is either gone or because your scope understands that you already issued a DROP, but the resources are still being used. They are placed on a queue for destruction by a background process.
Aaron Bertrand
I actually uncovered a bug, inadvertently, in SQL Server 2005, which has since been fixed, where the destruction rate was not (tempdb resource usage was forcing us to reboot a particular production cluster as often as once a week until they issued us a private hotfix).
Aaron Bertrand
+1  A: 

Temp table is stored in tempdb until the connection is dropped (or inthe case of a gloabal temp tables uwhen the last connection using it is dropped). YOu can also (and it isa a good proctice to do so) manually drop the table when you are finished unig it with a drop table statement.

No others cannot see your temp tables if they are local temp tables (They can see and use global temp tables) MUltiple people can run commands which use the same temp table name but they willnot be overlapping in a local temp table and so you can havea a table named #test and so can 10,000 other users but each one has its own structure and data.

You don't want to generally look up temp tables in tempdb. It is possible to chcek for existance, but that is the only time I have ever referenced tempdb directly. Simply use your temp table name.Example below of checking for exsistance

  IF OBJECT_ID('TempDB.dbo.#DuplicateAssignments') IS NOT NULL 
  BEGIN 
  DROP TABLE #DuplicateAssignments 
  END

You name temp tables by prefacing the name with # (for laocal tables the ones you would use 999.9% of the time) and ## for gloabal temp tables, then the rest of the name you want.

HLGEM
All temp tables are in 'tempDB' database (?) then what do you mean by global and local temp tables.. please explain
peakit
@peakit - Local temporary tables are specific to a given connection. Global temporary tables are visible to all connections.
CraigTP
I saw that someone is doing: IF object_id(N'tempdb..#MyTempTable', N'U') IS NOT NULL DROP TABLE #MyTempTableCould you please explain what is this 'N' in the first and second argument of objectId method doing?
peakit
N'means they are sending unicode charcters
HLGEM
Thanks HLGEM.. this may also interest you.. http://stackoverflow.com/questions/1596203/local-vs-global-temp-tables-when-to-use-what
peakit
+1  A: 

There's a few MSDN articles that are probably the best source of information on the tempDB database in SQL Server.

tempdb Database

The tempdb system database is a global resource that is available to all users connected to the instance of SQL Server and is used to hold the following:

  • Temporary user objects that are explicitly created, such as: global or local temporary tables, temporary stored procedures, table variables, or cursors.
  • Internal objects that are created by the SQL Server Database Engine, for example, work tables to store intermediate results for spools or sorting.
  • Row versions that are generated by data modification transactions in a database that uses read-committed using row versioning isolation or snapshot isolation transactions.
  • Row versions that are generated by data modification transactions for features, such as: online index operations, Multiple Active Result Sets (MARS), and AFTER triggers.

Operations within tempdb are minimally logged. This enables transactions to be rolled back. tempdb is re-created every time SQL Server is started so that the system always starts with a clean copy of the database. Temporary tables and stored procedures are dropped automatically on disconnect, and no connections are active when the system is shut down. Therefore, there is never anything in tempdb to be saved from one session of SQL Server to another. Backup and restore operations are not allowed on tempdb.

There's also tempdb and Index Creation, this blog post along with Working with tempdb in SQL Server 2005 which states:

The SQL Server system database, tempdb, has undergone a number of changes in SQL Server 2005. There are new tempdb usages and internal optimizations in SQL Server 2005; tempdb architecture is mostly unchanged since SQL Server 2000.

The tempdb system database is very similar to a user database. The main difference is that data in tempdb does not persist after SQL Server shuts down.

CraigTP