I have a large data table. There are 10 million records in this table.
What is the best way for this query
Delete LargeTable where readTime < dateadd(MONTH,-7,GETDATE())
I have a large data table. There are 10 million records in this table.
What is the best way for this query
Delete LargeTable where readTime < dateadd(MONTH,-7,GETDATE())
Shorter syntax
I was able to delete 19 million rows from my table of 21 million rows in matter of minutes. Here is my approach.
If you have a auto-incrementing primary key on this table, then you can make use of this primary key.
Get minimum value of primary key of the large table where readTime < dateadd(MONTH,-7,GETDATE()). (Add index on readTime, if not already present, this index will anyway be deleted along with the table in step 3.). Lets store it in a variable 'min_primary'
Insert all the rows having primary key > min_primary into a staging table (memory table if no. of rows is not large).
Drop the large table.
Recreate the table. Copy all the rows from staging table to main table.
Drop the staging table.
@Francisco Goldenstein, just a minor correction. The COMMIT must be used after you set the variable, otherwise the WHILE will be executed just once:
If you are using SQL server 2016 or higher and if your table is having partitions created based on column you are trying to delete(for example Timestamp column), then you could use this new command to delete data by partitions.
TRUNCATE TABLE WITH ( PARTITIONS ( { | } [ , ...n ] ) )
This will delete the data in selected partition(s) only and should be the most efficient way to delete data from part of table since it will not create transaction logs and will be done just as fast as regular truncate but without having all the data deleted from the table.
Drawback is if your table is not setup with partition, then you need to go old school and delete the data with regular approach and then recreate the table with partitions so that you can do this in future, which is what I did. I added the partition creation and deletion into insertion procedure itself. I had table with 500 million rows so this was the only option to reduce deletion time.
For more details refer to below links: https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql?view=sql-server-2017
SQL server 2016 Truncate table with partitions
Below is what I did first to delete the data before I could recreate the table with partitions with required data in it. This query will run for days during specified time window until the data is deleted.
If you are Deleting All the rows in that table the simplest option is to Truncate table, something like
Truncate table will simply empty the table, you cannot use WHERE clause to limit the rows being deleted and no triggers will be fired.
On the other hand if you are deleting more than 80-90 Percent of the data, say if you have total of 11 Million rows and you want to delete 10 million another way would be to Insert these 1 million rows (records you want to keep) to another staging table. Truncate this Large table and Insert back these 1 Million rows.
Or if permissions/views or other objects which has this large table as their underlying table doesnt get affected by dropping this table you can get these relatively small amount of the rows into another table drop this table and create another table with same schema and import these rows back into this ex-Large table.
One last option I can think of is to change your database's
Recovery Mode to SIMPLE
and then delete rows in smaller batches using a while loop something like this..and dont forget to change the Recovery mode back to full and I think you have to take a backup to make it fully affective (the change or recovery modes).
This variation of M.Ali's is working fine for me. It deletes some, clears the log and repeats. I'm watching the log grow, drop and start over.