Below is a delete query that takes 20 minutes to delete 100000 records at a time on a very fast server (quad-processor with 32GB RAM). I have very similar queries that take less than 3 minutes with very similar conditions, but just can't figure this
one out.
Facts:
- SQL Server 2012
- all three tables have more than 40 million records each, 30+ will be deleted from one of them;
- has to be done in a loop;
- T1 table has two non-clustered indexes: a) "part_one", "part_two" b) "part_two";
- T2 table has a non-clustered index on "some_value" column and "some_id" is a PK;
- T3 table has a non-clustered index on "some_id" column;
What else can I do to speed this up?
Thanks!
Declare @RowCount int
Set @RowCount = 1
Set RowCount 100000
While @RowCount > 0
Begin
Set @RowCount = 0
Delete T3
From T1 With (NoLock)
Inner Join T2 With (NoLock) On
T2.some_value = T1.part_one + '-' + T1.part_two
Inner Join T3 On
T3.some_id = T2.some_id
Where
T1.part_two <> 'X' And
T1.part_two <> ''
Set @RowCount = @@rowcount
End