Cascade on delete performance: Whats the fastest way to delete a row its 1-Many rows?

后端 未结 3 1053
温柔的废话
温柔的废话 2021-01-22 08:23

I have a database in which there is a parent \"Account\" row that then has a 1-Many relationship with another table, and that table has a 1-Many relationship with another table.

3条回答
  •  粉色の甜心
    2021-01-22 08:30

    It sounds like you might have indexing issues.

    Assume a parent-to-child relationship on column ParentId. By definition, column ParentId in the Parent table must have a primary or unique constraint, and thus be indexed. The child table, however, need not be indexed on ParentId. When you delete a parent entry, SQL has to delete all rows in the child table that have been assigned that foreign key... and if that column is not indexed, the work will have to be done with table scans. This could occur once for each table in your "deletion chain".

    Of course, it might just be volume. Deleting a few k rows from 100k+ databases with multiple indexes, even if the "delete lookup" field is indexed, could take significant time -- and dont' forget locking and blocking if you've got users accessing your system during the delete!

    Deferring the delete until a schedule maintenance window, as KM suggests, would definitely be an option--though it might require a serious modification to your code base.

提交回复
热议问题