1

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

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Badr Erraji
  • 131
  • 8

2 Answers2

3

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.

Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709
  • it's already partionned by date. i need to drop every date older than 3 months and not that is not an end of month. I already have a view for valid dates so i do delete where not in valid dates but again there is too much to delete – Badr Erraji Mar 21 '19 at 16:03
  • 2
    If you cannot use this method you can read through this post by Aaron Bertrand about batch deletes and view his test results. https://sqlperformance.com/2013/03/io-subsystem/chunk-deletes – JoeDBAHAHAHA Mar 21 '19 at 16:09
0

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>
Heinzi
  • 159,022
  • 53
  • 345
  • 499