How can I delete expired data from a huge table without having the log file grow out of control?

后端 未结 3 738
面向向阳花
面向向阳花 2021-01-05 08:28

I have a huge table (3 billion rows), which unfortunately contains mostly expired data. I want to simply delete all of these expired rows, and keep the rest.

I can e

3条回答
  •  攒了一身酷
    2021-01-05 08:57

    You really don't want to mess with trying anything silly like turning off logging when you want to do a lot of work on a table since any issues during the long task could easily lead to database corruption and other issues. However, there is a way around your issue.

    Create a temp table that matches the schema of your real table. Populate it with the data you want to KEEP. Then, truncate the original table (extremely fast and easy on the log files). Finally, move the data out of the temp table and into your original (and now empty) table.

    If you use auto-incrementing primary keys, you will need to force the field to take your original keys (so you don't have issues later).

提交回复
热议问题