Deleting millions of rows in MySQL

前端 未结 14 748
春和景丽
春和景丽 2020-12-02 07:05

I recently found and fixed a bug in a site I was working on that resulted in millions of duplicate rows of data in a table that will be quite large even without them (still

14条回答
  •  一整个雨季
    2020-12-02 07:56

    Here's the recommended practice:

    rows_affected = 0
    do {
     rows_affected = do_query(
       "DELETE FROM messages WHERE created < DATE_SUB(NOW(),INTERVAL 3 MONTH)
       LIMIT 10000"
     )
    } while rows_affected > 0
    

    Deleting 10,000 rows at a time is typically a large enough task to make each query efficient, and a short enough task to minimize the impact on the server4 (transactional storage engines might benefit from smaller transactions). It might also be a good idea to add some sleep time between the DELETE statements to spread the load over time and reduce the amount of time locks are held.

    Reference MySQL High Performance

提交回复
热议问题