Why are you truncating right before dumping the temp table?

I see some code where the author truncated the temp table just before dropping the temp table. Is there a reason for this?

TRUNCATE TABLE #Temp
DROP TABLE #Temp

      

+2


source to share


6 answers


Perhaps the author was under the impression that DROP TABLE would be faster if the table was already empty and knew that TRUNCATE would be faster than DELETE.



+2


source


Another reason is that it DROP TABLE

is a fully logged operation, so by pruning first (which is never logged), you omit the transaction log overhead.



+3


source


Perhaps it's a fist. (not that I'm perfect).

+1


source


On very large temporary tables, it is sometimes faster to truncate and then discard, because truncate simply moves the pointer. This is usually not required because temporary tables are lost on their own.

+1


source


Another possibility is that the encoder is trying to avoid the delayed crash that will occur when the temp table is larger than 8MB. That is, shorten it, then lower it. I'm not sure if this SQL engine will be tricked, but it might force synchronous cleanup. I do not understand why you want to do this, perhaps to avoid some problem with accumulating pending drops (Temp tables for destruction)

+1


source


Perhaps if the command TRUNCATE

throws an exception because of existing foreign keys?

0


source







All Articles