Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Can't create temporary table variables anymore

Author  Topic 

sql_er
Constraint Violating Yak Guru

267 Posts

Posted - 2011-04-14 : 16:14:38
Hi,

We had a very odd problem today. A server was non-responsive for a while and we identified that there was a job hanging for a few hours.This job causes millions of writes to the database within a short period of time. We killed the job and waited for a while for things to come down.

The server was still not very responsive. Looking at "sp_who2" results we identified that there was a lot of blocking going on and all related with DBName = 'tempdb'

We finally narrowed the culprits down to 2 stored procedures which create temporary variable tables. Once the logic was changed not to use temporary variable tables, all the blocking stopped and the server came back to normal.

It seems that simply creating the temporary variable tables (not even inserting any data into them) within the stored procedure, and then executing the stored procedure would make the procedure run forever, as if something is not allowing for the creation of the temp variable table to happen.

Could someone please suggest what could be the reason for this?
Could it be the issue with tempdb or something else? How to find out?

Thanks a lot!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-14 : 16:27:31
How about a temp table instead of a table variable? I believe that there is a bug in 2000 with table variabes. What does SELECT @@VERSION show?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sql_er
Constraint Violating Yak Guru

267 Posts

Posted - 2011-04-14 : 16:58:36
SELECT @@VERSION shows: Microsoft SQL Server 2000 - 8.00.2040 (Intel X86) May 13 2005 18:33:17 Copyright (c) 1988-2003 Microsoft Corporation Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

After some time the stored procedures are working with temp variables in them. Looks like it was a temporary problem. Could it be that it reached a limit on how many temp variables it could create?

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-14 : 17:14:27
Who knows. I'd highly recommend you upgrade to 2008 though.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2011-04-14 : 17:18:16
typically you would want to use table variables if the dataset is very small like a couple of hundred rows... temp tables perform better for anything higher..

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-14 : 17:27:28
We don't use table variables anymore. We had a major performance problem with them when there was only 1 row in it. Switching to a temp table fixed the problem. We worked with our DSE (PFE?) to identify the problem and then correct it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sql_er
Constraint Violating Yak Guru

267 Posts

Posted - 2011-04-14 : 17:47:21
Tara,

Do you remember what was the problem the temp variables caused?

I always thought it was good to use them when size is small (in our case 5-25 rows).

Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-14 : 17:53:53
I sure do remember. It was because table variables don't have statistics, and it was thinking there were thousands of rows in it but there was only 1. The switch to a temp table plus the appropriate index fixed the problem immediately.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2011-04-18 : 15:49:42
ITs true that table variables dont have statistics not can you create any other indexes except PK.
However, for a few rows of data SQL Server may end up scanning the table anyway..

Tara: Have you looked into creating a PK on the table variable to see if that gave any improvement?
#Temp tables work but the downside is depending on the workload of your system, if you have thousands of users calling the same proc/code.. you would be creating/dropping temp tables in large numbers putting a load on GAM pages constantly allocating/deallocating pages for objects..

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-04-18 : 16:11:16
The table variable already had a PK on it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2011-04-18 : 17:26:14
well.. one of those "unexplainable" things SQL Server does.. :)

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page
   

- Advertisement -