将表 A 中的行移动到表存档中

发布于 2024-12-09 02:55:18 字数 1054 浏览 0 评论 0原文

是否可以每周自动将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

永言不败 2024-12-16 02:55:18

就我个人而言,我会使用 MySQL 事件调度程序。这是一个内置的事件调度程序,类似于 Linux 中的 CRON。

您可以指定它来调用一个、多个过程或函数,或者以指定的时间间隔运行一些 SQL。

阅读 MySQL 文档,但一个例子是:

CREATE EVENT mydatabase.myevent
ON SCHEDULE EVERY 1 WEEK STARTS CURRENT_TIMESTAMP + INTERVAL 10 MINUTE
DO
 call clean_tables();

所以这就是说“每周调用一次 clean_tables() 并在 10 分钟内进行第一次调用”

一个问题是事件调度程序(我认为)默认情况下被禁用。要打开它,请运行:

SET GLOBAL event_scheduler = ON;

然后您可以运行:

SHOW PROCESSLIST;

以查看事件调度程序线程是否正在运行。

至于保留表 A ID 列(如果必须的话)。我会将 Table_Archive 上的 ID 保留为该表的唯一 ID,即使其成为主键 & auto_increment,然后有一个“Original_TableA_ID”列来存储 TableA ID。如果需要,您可以为其添加唯一索引。

所以 Table_Archive 会是这样的:

create table `Table_Archive` (
ID int unsigned primary key auto_increment, -- < primary key auto increment
tableAId unsigned int not null, -- < id column from TableA
stringValue varchar(100),
timestamp datetime,
UNIQUE KEY `archiveUidx1` (`tableAId`) -- < maintain uniqueness of TableA.ID column in Archive table
);

似乎没有人回答你原来的问题“我如何将 _now 更改为 3 天前的日期?”。您可以使用 INTERVAL 来完成此操作:

DELIMITER $

CREATE PROCEDURE clean_tables ()
BEGIN
BEGIN TRANSACTION;

DECLARE _now DATETIME;
SET _now := NOW();

INSERT
INTO    Table_Archive
SELECT  *
FROM    TableA
WHERE   timestamp < _now - interval 3 day;
FOR UPDATE;

DELETE
FROM    TableA
WHERE   timestamp < _now - interval 3 day;

COMMIT;
END$

DELIMITER ;

最后一点是,您应该考虑在 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:

CREATE EVENT mydatabase.myevent
ON SCHEDULE EVERY 1 WEEK STARTS CURRENT_TIMESTAMP + INTERVAL 10 MINUTE
DO
 call clean_tables();

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:

SET GLOBAL event_scheduler = ON;

You can then run:

SHOW PROCESSLIST;

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:

create table `Table_Archive` (
ID int unsigned primary key auto_increment, -- < primary key auto increment
tableAId unsigned int not null, -- < id column from TableA
stringValue varchar(100),
timestamp datetime,
UNIQUE KEY `archiveUidx1` (`tableAId`) -- < maintain uniqueness of TableA.ID column in Archive table
);

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:

DELIMITER $

CREATE PROCEDURE clean_tables ()
BEGIN
BEGIN TRANSACTION;

DECLARE _now DATETIME;
SET _now := NOW();

INSERT
INTO    Table_Archive
SELECT  *
FROM    TableA
WHERE   timestamp < _now - interval 3 day;
FOR UPDATE;

DELETE
FROM    TableA
WHERE   timestamp < _now - interval 3 day;

COMMIT;
END$

DELIMITER ;

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.

街角卖回忆 2024-12-16 02:55:18

如果您希望自动执行该脚本/查询,您可能需要查看cron jobs

如果您使用cpanel,请查看http://www .siteground.com/tutorials/cpanel/cron_jobs.htm

You 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

粉红×色少女 2024-12-16 02:55:18

添加 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.

桃扇骨 2024-12-16 02:55:18

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文