sql - 在存储过程结束时显式删除本地临时表有什么好处?

标签 sql sql-server tsql

考虑以下伪 T-SQL 代码(由存储过程执行):

CREATE TABLE #localTable ...

<do something with the temporary table here>

DROP TABLE #localTable;

DROP TABLE语句是存储过程执行的最后一条语句——该语句有什么好处吗?

请注意,我并不是在询问在存储过程的中间删除临时表(本地或非本地)(即在不再需要表之后,但在存储过程结束之前)过程代码)——由于减少了继续执行存储过程所需的内存,这似乎具有重要的好处。我想知道显式删除表与在存储过程完成执行时“让”SQL Server 执行此操作相比是否有任何好处(或任何影响,实际上是积极的还是消极的)。

最佳答案

有一篇关于此的详细文章 here .

The temporary object is renamed to an internal form when DROP TABLE is executed, and renamed back to the same user-visible name when CREATE TABLE is encountered on the next execution. In addition, any statistics that were auto-created on the temporary table are also cached. This means that statistics from a previous execution remain when the procedure is next called.

关于sql - 在存储过程结束时显式删除本地临时表有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670924/

相关文章:

sql - 如何指定不会被转换为 SQL 命令查询参数的常量值 NHibernate 标准

sql - 如何在 SQL Server 中查询大于特定日期的所有日期?

sql-server - 关于在 T-SQL 中为我将要讲述的 CASE 设计更快算法的问题

sql-server-2008 - 为什么 CREATE PROCEDURE 语句在此处与 IF 语句一起使用时会失败?

sql - 为什么 SUM(COL1 + COL2) 和 SUM(COL1) + SUM(COL2) 会产生不同的结果?

mysql - SQL,转换整数以进行时间戳减法

mysql - 在单个 MySQL 列中分隔标签

c# - 捕获异常记录器错误

c# - 为什么我的 SQL 'INSERT' 语句执行了两次?

mysql - 带计数的 SQL 复杂查询