title: How do I speed up deletes from a large database table?
body: <p>Here's the problem I am trying to solve: I have recently completed a data layer re-design that allows me to load-balance my database across multiple shards.  In order to keep shards balanced, I need to be able to migrate data from one shard to another, which involves copying from shard A to shard B, and then deleting the records from shard A.  But I have several tables that are very big, and have many foreign keys pointed to them, so deleting a single record from the table can take more than one second.</p>
  
  <p>In some cases I need to delete millions of records from the tables, and it just takes too long to be practical.</p>
  
  <p>Disabling foreign keys is not an option.  Deleting large batches of rows is also not an option because this is a production application and large deletes lock too many resources, causing failures.  I'm using Sql Server, and I know about partitioned tables, but the restrictions on partitioning (and the license fees for enterprise edition) are so unrealistic that they are not possible.</p>
  
  <p>When I began working on this problem I thought the hard part would be writing the algorithm that figures out how to delete rows from the leaf level up to the top of the data model, so that no foreign key constraints get violated along the way.  But solving that problem did me no good since it takes weeks to delete records that need to disappear overnight.</p>
  
  <p>I already built in a way to mark data as virtually deleted, so as far as the application is concerned, the data is gone, but I'm still dealing with large data files, large backups, and slower queries because of the sheer size of the tables.</p>
  
  <p>Any ideas?  I have already read older related posts here and found nothing that would help.</p>