Temporary tables store on disk and table variables – in memory. This means that write to temp table should be significantly slower when I insert let’s say one thousand guids. However when I checked two below scripts, it turned out that the time is almost identical 877/896. Why is that?
DROP TABLE #TEST
Create Table #test
(
Id UNIQUEIDENTIFIER
)
DECLARE @i INT = 1;
WHILE @i < 1000
BEGIN
INSERT INTO #test VALUES (NEWID())
SET @i = @i + 1;
END
AND
DECLARE @table1 TABLE
(
Id UNIQUEIDENTIFIER
)
DECLARE @i INT = 1;
WHILE @i < 1000
BEGIN
INSERT INTO #test VALUES (NEWID())
SET @i = @i + 1;
END