a job to delete rows older than 3 months in mysql database

后端 未结 4 1801
走了就别回头了
走了就别回头了 2021-02-01 06:44

We use mysql server as a centralized logging system and I want to have a job to delete\\clean the table entries that are more than 3 months old regularly. What is the best way t

4条回答
  •  忘了有多久
    2021-02-01 07:24

    A simple way would be to scheduled a job that runs every night that calls a stored procedure to delete all rows older than three months. Depending on your O/S it should be easy to do. Do it each night and the amount of deleted data won't be as much as doing it once a month or so.

    I have also in the past, had code in my SP that inserted data to do the deletes of unneeded data at that time, so basically every time a row is inserted, right after the insert in ran a 'cleanup' proc that did a bit of maintenance. This wouldn't be my first choice, but its doable if the number of inserts is low, and the deletes can be done fast (you don't want to slow the user down). Nice side-effect of this is the data is always clean (i.e. all data is always <= 3 months old), but not sure if that matters in your app.

    You should also consider archiving the rows to another table instead in addition to deleting them, if there is any chance you might want the data someday for another purpose. I always do this, and you'd be surprised what a hero you can look like when some manager who doesn't even know you are saving the data, suddenly has an urgent need for it....and you can deliver for them.

提交回复
热议问题