我有8個表:如何使用其他連接表中的記錄對記錄進行歸檔?
users:
uid
users_removed:
uid
messages:
mid
uid FK users (uid)
messages_removed:
mid
uid
comments:
cid
mid FK messages (mid)
comments_removed:
cid
mid
files:
fid
mid FK messages (mid)
files_removed:
fid
mid
當我從表中刪除記錄「用戶」我想(從用戶在刪除之前)將其移動到users_removed表。我還希望將所有相應的消息(以及文件和註釋)移至* _removed表。
我使用觸發器:
CREATE TRIGGER delete_user BEFORE DELETE ON users
FOR EACH ROW BEGIN
INSERT IGNORE INTO users_removed
SELECT * FROM users WHERE uid = OLD.uid;
DELETE FROM messages WHERE OLD.uid in (owner_id, author_id);
END
|
CREATE TRIGGER delete_message BEFORE DELETE ON messages
FOR EACH ROW BEGIN
INSERT IGNORE INTO messages_removed
SELECT * FROM messages WHERE mid = OLD.mid;
DELETE FROM comments WHERE mid = OLD.mid;
DELETE FROM files WHERE mid = OLD.mid;
END
|
CREATE TRIGGER delete_comment BEFORE DELETE ON comments
FOR EACH ROW BEGIN
INSERT IGNORE INTO comments_removed
SELECT * FROM comments WHERE cid = OLD.cid;
END
|
CREATE TRIGGER delete_file BEFORE DELETE ON files
FOR EACH ROW BEGIN
INSERT IGNORE INTO files_removed
SELECT * FROM files WHERE fid = OLD.fid;
END
|
但它與> 5萬個用戶的速度很慢,> 1M消息,評論和文件。
有沒有快速的方法來做到這一點?
我可能會將此列作爲表格中的一列。 – sshow 2009-08-27 15:18:27