<i id='4fTZL'><tr id='4fTZL'><dt id='4fTZL'><q id='4fTZL'><span id='4fTZL'><b id='4fTZL'><form id='4fTZL'><ins id='4fTZL'></ins><ul id='4fTZL'></ul><sub id='4fTZL'></sub></form><legend id='4fTZL'></legend><bdo id='4fTZL'><pre id='4fTZL'><center id='4fTZL'></center></pre></bdo></b><th id='4fTZL'></th></span></q></dt></tr></i><div id='4fTZL'><tfoot id='4fTZL'></tfoot><dl id='4fTZL'><fieldset id='4fTZL'></fieldset></dl></div>

      <bdo id='4fTZL'></bdo><ul id='4fTZL'></ul>
  1. <legend id='4fTZL'><style id='4fTZL'><dir id='4fTZL'><q id='4fTZL'></q></dir></style></legend>

      <tfoot id='4fTZL'></tfoot>

      <small id='4fTZL'></small><noframes id='4fTZL'>

    1. 将行从 TableA 移动到 Table-Archive

      时间:2023-08-21
        <i id='j4WOn'><tr id='j4WOn'><dt id='j4WOn'><q id='j4WOn'><span id='j4WOn'><b id='j4WOn'><form id='j4WOn'><ins id='j4WOn'></ins><ul id='j4WOn'></ul><sub id='j4WOn'></sub></form><legend id='j4WOn'></legend><bdo id='j4WOn'><pre id='j4WOn'><center id='j4WOn'></center></pre></bdo></b><th id='j4WOn'></th></span></q></dt></tr></i><div id='j4WOn'><tfoot id='j4WOn'></tfoot><dl id='j4WOn'><fieldset id='j4WOn'></fieldset></dl></div>
        • <bdo id='j4WOn'></bdo><ul id='j4WOn'></ul>
        • <small id='j4WOn'></small><noframes id='j4WOn'>

          <legend id='j4WOn'><style id='j4WOn'><dir id='j4WOn'><q id='j4WOn'></q></dir></style></legend>

                <tbody id='j4WOn'></tbody>
              <tfoot id='j4WOn'></tfoot>
                本文介绍了将行从 TableA 移动到 Table-Archive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                是否可以每周在 mysql 表中自动将 3 天前的行移动到另一个名为Table_Archive"的表中?

                Is it possible to move rows that are 3 days old into an other table called "Table_Archive" automatically in mysql ones a week?

                表A例如:

                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
                

                搬家后

                表A:

                ID | stringvalue | Timestamp
                4  | abc4        | 2011-10-10
                5  | abc5        | 2011-10-11
                

                表_档案:

                ID | stringvalue | Timestamp
                1  | abc         | 2011-10-01
                2  | abc2        | 2011-10-02
                3  | abc3        | 2011-10-05
                

                当新的输入进入tableA时,下一步的ID(PK)不会有任何问题吗?

                And when new input comes into tableA it wont be any problems with ID (PK) in the next move?

                我得到了什么:

                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 天前的日期?

                How do I change _now to be the date 3 days ago?

                推荐答案

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

                Personally, I would make use of the MySQL Event Scheduler. This is a built in event scheduler rather like CRON in Linux.

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

                You can specify it to call a procedure, procedures or functions or run a bit of SQL at designated intervals.

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

                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();
                

                所以这是说每周调用一次 clean_tables() 并在 10 分钟后进行第一次调用"

                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;
                

                然后您可以运行:

                SHOW PROCESSLIST;
                

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

                To see whether the event scheduler thread is running.

                至于保留您的表 A ID 列(如果必须).我会将 Table_Archive 上的 ID 保留为该表的唯一标识,即使其成为主键 &auto_increment 然后有一个 'Original_TableA_ID' 列来存储 TableA ID.如果需要,您可以在其上放置唯一索引.

                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.

                所以 Table_Archive 应该是这样的:

                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
                );
                

                似乎没有人回答您最初的问题如何将 _now 更改为 3 天前的日期?".您可以使用 INTERVAL 来做到这一点:

                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 ;
                

                最后一点是,您应该考虑在 TableA 的时间戳列上创建索引,以提高 clean_tables() 过程的性能.

                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.

                这篇关于将行从 TableA 移动到 Table-Archive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何修改现有表以添加时区 下一篇:ejabberd 16.06 + mysql 5.5.50,不保存消息历史

                相关文章

                最新文章

                <tfoot id='WbvAT'></tfoot>

                <small id='WbvAT'></small><noframes id='WbvAT'>

                <legend id='WbvAT'><style id='WbvAT'><dir id='WbvAT'><q id='WbvAT'></q></dir></style></legend>

                <i id='WbvAT'><tr id='WbvAT'><dt id='WbvAT'><q id='WbvAT'><span id='WbvAT'><b id='WbvAT'><form id='WbvAT'><ins id='WbvAT'></ins><ul id='WbvAT'></ul><sub id='WbvAT'></sub></form><legend id='WbvAT'></legend><bdo id='WbvAT'><pre id='WbvAT'><center id='WbvAT'></center></pre></bdo></b><th id='WbvAT'></th></span></q></dt></tr></i><div id='WbvAT'><tfoot id='WbvAT'></tfoot><dl id='WbvAT'><fieldset id='WbvAT'></fieldset></dl></div>

                      <bdo id='WbvAT'></bdo><ul id='WbvAT'></ul>