I have a temp table used for intermediate calculation in a stored procedure.
Here is code segment:
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;
Since we are doing this a lot in a day. It causes SQL Server performance issue.
I want to change above code to
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
I wonder if the new change will improve the SQL Server performance. If there is a better way to do this, please let me know, too.
Short version of answer
e.g., I've commented out stuff you don't need below and added a primary key on ID
-- IF OBJECT_ID('tempdb..#Updates','U') IS NULL
-- BEGIN
CREATE TABLE #Updates (
ID int not null PRIMARY KEY,
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
If, within that stored procedure, you
then using TRUNCATE TABLE #Updates
will be marginally faster that deleting from it.
Explanation/longer answer:
The temporary table is
If you run the stored procedure twice simultaneously, each will create, use, then delete its own temporary table - and they won't be able to be accessed by each other.
In terms of improving performance
Temporary tables (e.g., in Temp_DB) are typically very fast. They also have some advantages over normal tables as they need much less transaction logging. I'd be surprised if the creation of a temporary table really affects time that much.
Pinal Dave does a very nice quick video about whether there is an effect of Dropping temporary tables in a stored procedure vs just letting them be removed automatically - in short the answer is 'no'.
UPDATE: I just did a test on an OK-ish computer that is about 10 years old now.
CREATE PROCEDURE _TestA AS BEGIN CREATE TABLE #a (a int); END;
GO
CREATE PROCEDURE _TestB AS BEGIN CREATE TABLE #a (a int); CREATE TABLE #b (a int); END;
GO
EXEC _TestA;
GO 1000
EXEC _TestB;
GO 1000
The first took 4 seconds, and the second took 6 seconds. This suggests that creating a temp table should only take a few milliseconds at most.