SQL Server Efficiently dropping a group of rows with millions and millions of rows

前端 未结 13 569
遇见更好的自我
遇见更好的自我 2021-02-04 12:26

I recently asked this question: MS SQL share identity seed amongst tables (Many people wondered why)

I have the following layout of a table:

Table: Star

13条回答
  •  情话喂你
    2021-02-04 13:31

    What do you do with the star data? If you only look at data for one category at any given time this might work, but it is hard to maintain. Every time you have a new category, you will have to build a new table. If you want to query across categories, it becomes more complex and possibly more expensive in terms of time. If you do this and do want to query across categories a view is probably best (but do not pile views on top of views). If you are looking for data on a particular star, would you know which table to query? If not then how are you going to determine which table or are you goign to query them all? When entering data, how will the application decide which table to put the data into? How many categories will there be? And incidentally relating to each having a separate id, use the bigint identities and combine the identity with the category type for your unique identifier.

    Truly do you need to delete the whole category or only the star that the data changed for? And do you need to delete at all, maybe you only need to update information.

    Have you tried deleting in batches (1000 records or so at a time in a loop). This is often much faster than deleting a million records in one delete statement. It often keeps the table from getting locked during the delete as well.

    Another technique is mark the record for deletion. Then you can run a batch process when usage is low to delete those records and your queries can run on a view that excludes the records marked for deletion.

    Given your answers, I think your proposal may be reasonable.

提交回复
热议问题