1

I am currently optimizing a stored procedure, because it times out due to amount of calculations and entries present.

However, I am unsure about a situation. Is it best to use 8 temp tables (which draw their information from other data in the database) or to just put the data from the database into various variables and access them?

Which would give the best performance to fix the timeout issue?

3
  • 1
    It's not that hard to test is it? Commented Jun 7, 2012 at 18:26
  • How many rows would you expect in each temp table? Also, when you are talking about variables, are you talking about table variables or regular data types? Commented Jun 7, 2012 at 18:26
  • maybe 1000 entries, and regular data types Commented Jun 7, 2012 at 18:28

2 Answers 2

1

Depends on how many entries you have in the "temp" tables.

  • a real temp table #temp can have indices defined on it, it will be analyzed by SQL Server for its number of rows etc. and it does take part in transactions, e.g. you can insert rows and then rollback and those rows will "disappear" from your temp tabl

  • a table variable @temp will always be considered by SQL Server to have exactly one row - that's not a problem if you have just a handful of rows in there. But if you need to store hundreds of rows, this assumption by the SQL Server query optimizer can lead to highly inefficient query plans. Also: table variables do not participate in transactions - that can be a good thing - or a bad one - depending on your environment

Sign up to request clarification or add additional context in comments.

2 Comments

what about for 1000+ entries?
@MasterP: in such a case, I would probably lean towards a "real" temp table (#temp or ##temp)
0

Based on your responses above, you should definitely use temp tables. You should also look at indexing them to increase your performance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.