【发布时间】:2021-02-25 00:30:44
【问题描述】:
我有一个临时表,用于存储过程中的中间计算。
这里是代码段:
CREATE TABLE #Updates
(
ID int not null,
ID2 int not null,
ID3 int not null
);
-- Do some operations and updates
IF OBJECT_ID('tempdb..#Updates','U') IS NOT NULL
DROP TABLE #Updates;
因为我们一天要做很多这样的事情。它会导致 SQL Server 性能问题。
我想把上面的代码改成
IF OBJECT_ID('tempdb..#Updates','U') IS NULL
BEGIN
CREATE TABLE #Updates
(
ID int not null,
ID2 int not null,
ID3 int not null
);
END
-- Do some operations and updates
IF OBJECT_ID('tempdb..#Updates','U') IS NOT NULL
DELETE FROM #Updates
我想知道新的变化是否会提高 SQL Server 的性能。如果有更好的方法,也请告诉我。
【问题讨论】:
-
不要
DELETE,它会一样慢 - 如果有的话,只需TRUNCATE TABLE #Updates。
标签: sql create-table drop-table delete-record