November 17, 2010 at 9:11 pm
I have query which is using 4 temporary tables for some internal caluclation.
My doubt here is should i use temporary tables or should i use physical tables keeping
in mind that this query is executed in every 10 seconds.
🙂
November 17, 2010 at 9:42 pm
virender.singh (11/17/2010)
I have query which is using 4 temporary tables for some internal caluclation.My doubt here is should i use temporary tables or should i use physical tables keeping
in mind that this query is executed in every 10 seconds.
🙂
I wouldn't use physical tables for such a thing especially if you have something other than the Simple Recovery Model active. Why do you think the use of temp tables here is a problem?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 17, 2010 at 10:50 pm
thanks for your quick reply jeff.
Although i am already using temp tables, but my boss:):-D suggested me use physical table instead and like you i was also surprised.
November 18, 2010 at 1:05 am
Temp tables are created in tempdb just like you create any other table in your user database. Only difference is it will be destroyed when u disconnect (Prefixed with #) or it will be destroyed when all connections using that object disconnects(prefixed with ##).
November 18, 2010 at 1:21 am
ps. (11/18/2010)
Only difference is it will be destroyed when u disconnect (Prefixed with #)
There are other differences internally around logging, caching when dropped and the like.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 18, 2010 at 1:23 am
virender.singh (11/17/2010)
but my boss:):-D suggested me use physical table instead
Ask him for his reasoning.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 18, 2010 at 1:30 am
GilaMonster (11/18/2010)
ps. (11/18/2010)
Only difference is it will be destroyed when u disconnect (Prefixed with #)There are other differences internally around logging, caching when dropped and the like.
Thanks Gail. Need to search for more info on that. I hope the book by Brent Ozar (sql server professional internals and troubleshooting) gives me some information. it has a chapter on tempdb. will get it in a day or two.
November 18, 2010 at 4:18 am
I know that temp tables are created in tempdb by default and automatically dropped on session close.
he was telling that there would be more logging, caching etc. in case of a temp table so try to avoid it if you are executing your query so frequently.
November 18, 2010 at 4:23 am
There's less logging for a temp table compared to a user table. Proof on my blog if you're interested.
The data caching's much the same, but even if it were more, caching is good, so more is better.
Temp tables aren't in tempdb by default, they are always in tempDB, that cannot be changed.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 18, 2010 at 7:57 pm
virender.singh (11/18/2010)
I know that temp tables are created in tempdb by default and automatically dropped on session close.he was telling that there would be more logging, caching etc. in case of a temp table so try to avoid it if you are executing your query so frequently.
Actually, that's not the best advice. IIRC (Gail, please correct me if I'm wrong), Temp Tables have space reserved and the definition is preserved for a bit if you don't explicity drop the Temp Table. A frequently running routine can reuse that instead of building new. In other words, there is sometimes a performance advantage to using Temp Tables.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 18, 2010 at 8:00 pm
ps. (11/18/2010)
Temp tables are created in tempdb just like you create any other table in your user database. Only difference is it will be destroyed when u disconnect (Prefixed with #) or it will be destroyed when all connections using that object disconnects(prefixed with ##).
I'd also like to remind everyone that they start out in memory (which usually makes it very fast) and only spill onto disk if it uses too much memory... just like a Table Variable does.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 18, 2010 at 10:48 pm
Jeff Moden (11/18/2010)
Actually, that's not the best advice. IIRC (Gail, please correct me if I'm wrong), Temp Tables have space reserved and the definition is preserved for a bit if you don't explicity drop the Temp Table.
True, even if you do explicitly drop the temp table.
SQL reserves (I think) one page for the heap/cluster and one page for each index (need to check) as well as the metadata.
This is one of the things that alleviates allocation contention on 2005+, making the problem far less severe than on SQL 2000. There's also implications for recompiles.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 19, 2010 at 5:16 am
GilaMonster (11/18/2010)
Jeff Moden (11/18/2010)
Actually, that's not the best advice. IIRC (Gail, please correct me if I'm wrong), Temp Tables have space reserved and the definition is preserved for a bit if you don't explicity drop the Temp Table.True, even if you do explicitly drop the temp table.
SQL reserves (I think) one page for the heap/cluster and one page for each index (need to check) as well as the metadata.
This is one of the things that alleviates allocation contention on 2005+, making the problem far less severe than on SQL 2000. There's also implications for recompiles.
One data page and one IAM page is cached with the query plan in the plan cache, only if:
* Named constraints are not created.
* Data Definition Language (DDL) statements that affect the table are not run after temp table creation
* The object is not created from dynamic SQL
See http://blogs.msdn.com/b/sqlserverstorageengine/archive/2008/12/22/tempdb-basics-cont.aspx
And yes, explicitly dropping the temporary table makes no difference to the caching behaviour.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
November 19, 2010 at 6:28 am
GilaMonster (11/18/2010)
Jeff Moden (11/18/2010)
Actually, that's not the best advice. IIRC (Gail, please correct me if I'm wrong), Temp Tables have space reserved and the definition is preserved for a bit if you don't explicity drop the Temp Table.True, even if you do explicitly drop the temp table.
SQL reserves (I think) one page for the heap/cluster and one page for each index (need to check) as well as the metadata.
This is one of the things that alleviates allocation contention on 2005+, making the problem far less severe than on SQL 2000. There's also implications for recompiles.
Ah... forgot about the recompiles. They can be a blessing or a bane. "It Depends". Thanks, Gail.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 19, 2010 at 6:29 am
Paul White NZ (11/19/2010)
GilaMonster (11/18/2010)
Jeff Moden (11/18/2010)
Actually, that's not the best advice. IIRC (Gail, please correct me if I'm wrong), Temp Tables have space reserved and the definition is preserved for a bit if you don't explicity drop the Temp Table.True, even if you do explicitly drop the temp table.
SQL reserves (I think) one page for the heap/cluster and one page for each index (need to check) as well as the metadata.
This is one of the things that alleviates allocation contention on 2005+, making the problem far less severe than on SQL 2000. There's also implications for recompiles.
One data page and one IAM page is cached with the query plan in the plan cache, only if:
* Named constraints are not created.
* Data Definition Language (DDL) statements that affect the table are not run after temp table creation
* The object is not created from dynamic SQL
See http://blogs.msdn.com/b/sqlserverstorageengine/archive/2008/12/22/tempdb-basics-cont.aspx
And yes, explicitly dropping the temporary table makes no difference to the caching behaviour.
Thanks for the confirmation and the time to research it, Paul.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply