問題描述
是否可以每周在 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模板網!