0

I have a temp table full of GUIDs ##TempCleanseTheseCustomers.

I want to delete several related tables to a customer table. Around 30 related tables. Here is some examples:

Delete from AddressHistory
Where CustomerId in (select CustomerId From ##TempCleanseTheseCustomers)

Delete from Address
Where CustomerId in (select CustomerId From ##TempCleanseTheseCustomers)


DECLARE @MessageMessageId uniqueidentifier
set @MessageMessageId = 
(
SELECT        TOP (1)  Message.MessageId
FROM            Customer INNER JOIN
                     CustomerMessage ON Customer.CustomerId = 
CustomerMessage.CustomerId INNER JOIN
                     Message ON CustomerMessage.MessageId = 
Message.MessageId
Where Customer.CustomerId in (select CustomerId From 
##TempCleanseTheseCustomers)
)


Delete from MessageAttachment
Where MessageId = @MessageMessageId

Delete from Message
Where MessageId = @MessageMessageId

How can make this run as fast as possible? (##TempCleanseTheseCustomers contains over 2 million rows)

I have tried moving the table to memory. Running it via C# code etc.

Per G
  • 375
  • 1
  • 4
  • 18

1 Answers1

0

May be you can check that there are not duplicated records on ##TempCleanseTheseCustomers and check the existence of indexes and keys on the other several tables