有哪些方法可以移动早于“Y”的数据? MySQL 中的存档/历史表需要几天?
希望每 X 天将数据从表 A 移动到历史表 B,以获取 Y 天前的数据,然后从历史表 B 中删除早于 Z 天的数据。
只是探索不同的方法来实现这一目标。 因此,任何建议将不胜感激。
变量示例 X - 7 天 Y - 60 天 z - 365天
谢谢
Looking to move data from a table A to history table B every X days for data that is Y days old and then remove the data from history table B that is older than Z days.
Just exploring different ways to accomplish this. So any suggestions would be appreciated.
Example for variables
X - 7days
Y - 60days
z - 365days
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 MySQL 5.1,您也许可以使用事件调度程序,而不是 cron。
我没有使用过它,但我在 SQL Server 中使用过类似的东西。
If you are using MySQL 5.1 you maybe able to use the event scheduler, instead of cron.
I have not used it but I have used something similar in SQL Server.
这看起来很简单。
您需要每晚的 cron 作业来运行脚本。
脚本文件本身也非常简单。 我们将只使用 mysqldump 和 Now() 函数;
您可以只将该行包含在 cron 文件中,但为了可扩展性等,我建议将其作为脚本文件。
This seems straight forward.
You want a nightly cron job to run a script.
The script file itself is pretty simple as well. We'll just use mysqldump and the Now() function;
You could just include that line in the cron file, but for scalability and such I reccomend making it a script file.
当然,最简单的方法是将在线表中的行插入到历史表中,然后进行删除。 正如 Quassnoi 所建议的那样,将其包装在 SPROC 中将是完美的。
另一种可能更好的方法是利用分区。 如果您的表按日期分区,那么您应该能够通过简单地删除相关分区来至少加快 DELETE 部分的速度。 INSERT 部分也可能会更快,因为所有插入的行都来自一个分区(如果您正确创建了分区)。
如果架构相同,甚至可能存在某种技巧,允许您相对即时地将分区从一个表移动到另一个表。
The simplest way of course would be to insert the rows from the online table to the history table then do your deletes. Wrapping that up in a SPROC as Quassnoi suggests would be perfect.
Another potentially better way would be to take advantage of partitioning. If you have your tables partitioned by date, you should be able to speed up at least the DELETE parts by simply dropping the relevant partition. The INSERT part may also be faster since all the inserted rows would come from one partition (if you create your partitions right).
If the schemas are the same, there may even be some kind of trick that would allow you to just move the partitions from one table to the other relatively instantaneously.