将表 A 中的行移动到表存档中
是否可以每周自动将 3 天前的行移动到 mysql 表中名为“Table_Archive”的其他表中?
tableA ex:
ID | stringvalue | Timestamp
1 | abc | 2011-10-01
2 | abc2 | 2011-10-02
3 | abc3 | 2011-10-05
4 | abc4 | 2011-10-10
5 | abc5 | 2011-10-11
移动之后
tableA:
ID | stringvalue | Timestamp
4 | abc4 | 2011-10-10
5 | abc5 | 2011-10-11
Table_Archive:
ID | stringvalue | Timestamp
1 | abc | 2011-10-01
2 | abc2 | 2011-10-02
3 | abc3 | 2011-10-05
当新输入进入tableA时,下一步移动中的ID(PK)不会有任何问题吗?
我得到了什么:
CREATE PROCEDURE clean_tables ()
BEGIN
BEGIN TRANSACTION;
DECLARE _now DATETIME;
SET _now := NOW();
INSERT
INTO Table_Archive
SELECT *
FROM TableA
WHERE timestamp < _now - 3;
FOR UPDATE;
DELETE
FROM TableA
WHERE timestamp < _now - 3;
COMMIT;
END
如何将 _now 更改为 3 天前的日期?
Is it possible to move rows that are 3 days old into an other table called "Table_Archive" automatically in mysql ones a week?
tableA ex:
ID | stringvalue | Timestamp
1 | abc | 2011-10-01
2 | abc2 | 2011-10-02
3 | abc3 | 2011-10-05
4 | abc4 | 2011-10-10
5 | abc5 | 2011-10-11
After the move
tableA:
ID | stringvalue | Timestamp
4 | abc4 | 2011-10-10
5 | abc5 | 2011-10-11
Table_Archive:
ID | stringvalue | Timestamp
1 | abc | 2011-10-01
2 | abc2 | 2011-10-02
3 | abc3 | 2011-10-05
And when new input comes into tableA it wont be any problems with ID (PK) in the next move?
What Ive got:
CREATE PROCEDURE clean_tables ()
BEGIN
BEGIN TRANSACTION;
DECLARE _now DATETIME;
SET _now := NOW();
INSERT
INTO Table_Archive
SELECT *
FROM TableA
WHERE timestamp < _now - 3;
FOR UPDATE;
DELETE
FROM TableA
WHERE timestamp < _now - 3;
COMMIT;
END
How do I change _now to be the date 3 days ago?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我个人而言,我会使用 MySQL 事件调度程序。这是一个内置的事件调度程序,类似于 Linux 中的 CRON。
您可以指定它来调用一个、多个过程或函数,或者以指定的时间间隔运行一些 SQL。
阅读 MySQL 文档,但一个例子是:
所以这就是说“每周调用一次 clean_tables() 并在 10 分钟内进行第一次调用”
一个问题是事件调度程序(我认为)默认情况下被禁用。要打开它,请运行:
然后您可以运行:
以查看事件调度程序线程是否正在运行。
至于保留表 A ID 列(如果必须的话)。我会将 Table_Archive 上的 ID 保留为该表的唯一 ID,即使其成为主键 & auto_increment,然后有一个“Original_TableA_ID”列来存储 TableA ID。如果需要,您可以为其添加唯一索引。
所以 Table_Archive 会是这样的:
似乎没有人回答你原来的问题“我如何将 _now 更改为 3 天前的日期?”。您可以使用 INTERVAL 来完成此操作:
最后一点是,您应该考虑在 TableA 的时间戳列上创建索引,以提高 clean_tables() 过程的性能。
Personally, I would make use of the MySQL Event Scheduler. This is a built in event scheduler rather like CRON in Linux.
You can specify it to call a procedure, procedures or functions or run a bit of SQL at designated intervals.
Read the MySQL docs but an example would be:
So this is saying "call clean_tables() once a week and make the first call in 10 minutes' time"
One gotcha is that the event scheduler is (I think) disabled by default. To turn it on run:
You can then run:
To see whether the event scheduler thread is running.
As for preserving your Table A ID column (if you must). I would keep the ID on Table_Archive as unique to that table i.e make it the primary key & auto_increment and then have a 'Original_TableA_ID' column in which to store the TableA ID. You can put a unique index on this if you want.
So Table_Archive would be like:
Nobody seems to have answered your original question "How do I change _now to be the date 3 days ago?". You do that using
INTERVAL
:One final point is that you should consider creating an index on the timestamp column on TableA to improve the performance of you clean_tables() procedure.
如果您希望自动执行该脚本/查询,您可能需要查看
cron jobs
。如果您使用
cpanel
,请查看http://www .siteground.com/tutorials/cpanel/cron_jobs.htmYou may need to have a look into
cron jobs
if you want that script/query to be executed automatically.If you are using
cpanel
have a look into http://www.siteground.com/tutorials/cpanel/cron_jobs.htm添加 Tom Mac 关于事件调度程序的最佳答案(imo) - 请注意,在备份架构时,您必须通过 --events=TRUE 标志指定您希望与它一起备份的事件。
如果您在工作台中手动导出,则最新版本在主“导出到磁盘”选项卡上有一个复选框 - 旧版本将其隐藏在“高级导出选项”选项卡中。
Adding to the best answer (imo) by Tom Mac regarding the event scheduler - be aware that when backing up the schema, you have to specify that you want the events backed up with it via the --events=TRUE flag.
If you're exporting manually in the workbench, the latest version has a checkbox on the main 'Export To Disk' tab - older versions hide it away in the Advanced Export Options tab.
MySQL 可能会使用 MySQL Event Scheduler 在特定时间自动执行查询。检查此链接了解更多详细信息。
https://dev.mysql.com/doc/refman/ 5.7/en/event-scheduler.html
It is possible, MySQL will execute query automatically at specific time using MySQL Event Scheduler. Check this link for more details.
https://dev.mysql.com/doc/refman/5.7/en/event-scheduler.html