Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is deleting all records in a table a bad practice in SQL Server?

Tags:

sql-server

I am moving a system from a VB/Access app to SQL server. One common thing in the access database is the use of tables to hold data that is being calculated and then using that data for a report.
eg.

delete from treporttable
insert into treporttable (.... this thing and that thing)
Update treportable set x = x * price where (...etc)  

and then report runs from treporttable

I have heard that SQL server does not like it when all records from a table are deleted as it creates huge logs etc. I tried temp sql tables but they don't persists long enough for the report which is in a different process to run and report off of.

There are a number of places where this is done to different report tables in the application. The reports can be run many times a day and have a large number of records created in the report tables.

Can anyone tell me if there is a best practise for this or if my information about the logs is incorrect and this code will be fine in SQL server.

like image 679
ozdeveloper Avatar asked May 19 '09 13:05

ozdeveloper


1 Answers

If you do not need to log the deletion activity you can use the truncate table command.

From books online:

TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause: both remove all rows in the table. But TRUNCATE TABLE is faster and uses fewer system and transaction log resources than DELETE.

http://msdn.microsoft.com/en-us/library/aa260621(SQL.80).aspx

like image 125
John Sansom Avatar answered Oct 14 '22 08:10

John Sansom