In SQL Server 2012, I need to delete 100+ million rows from a table with 4-5 indices.
How can I proceed? I guess that the transaction log will be full and thus cancel the transaction.
Thanks for your help
In SQL Server 2012, I need to delete 100+ million rows from a table with 4-5 indices.
How can I proceed? I guess that the transaction log will be full and thus cancel the transaction.
Thanks for your help
Depending on the size of the table, you might find it faster to empty and repopulate:
create table temp_t
select *
from t
where <i want to keep conditions>;
truncate table t;
insert into t
select *
from temp_t;
In the longer term, you may want to add partitions so it is easier to delete rows. For instance, partitioning by date would allow you to drop old records with a minimum of overhead.
If you
it should suffice to do a chunked delete. Lots of examples can be found in various related questions here on SO and on dba.SE (e.g. this one or that one). To make this answer self-contained, I will quote one of those examples (taken from this answer by gbn and slightly modified) here:
SELECT 'Starting' --sets @@ROWCOUNT
WHILE @@ROWCOUNT <> 0
DELETE TOP (10000) MyTable WHERE <some condition>