简体   繁体   English

SQL Server性能临时表或变量

[英]SQL Server performance temp tables or variables

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? 最好使用8个临时表(它们从数据库中的其他数据中获取信息)还是只是将数据库中的数据放入各种变量中并进行访问?

Which would give the best performance to fix the timeout issue? 哪种方法可以最好地解决超时问题?

Depends on how many entries you have in the "temp" tables. 取决于您在“ temp”表中有多少个条目。

  • 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, eg you can insert rows and then rollback and those rows will "disappear" from your temp tabl 一个真正的临时表#temp可以在其上定义索引,SQL Server将对其进行分析以获取其行数等。并且它确实参与了事务,例如,您可以插入行然后回滚,这些行将“消失”从您的临时表

  • 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. SQL Server始终将表变量@temp视为仅具有一行-如果您那里只有几行,这不是问题。 But if you need to store hundreds of rows, this assumption by the SQL Server query optimizer can lead to highly inefficient query plans. 但是,如果您需要存储数百行,则SQL Server查询优化器的这种假设可能会导致效率极低的查询计划。 Also: table variables do not participate in transactions - that can be a good thing - or a bad one - depending on your environment 另外:表变量参与事务-根据您的环境,这可能是好事,也可能是坏事

Based on your responses above, you should definitely use temp tables. 根据上面的回答,您绝对应该使用临时表。 You should also look at indexing them to increase your performance. 您还应该考虑将它们编入索引以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM