I have 3 tables abc
ab and c all are related through an id a.id = b.id = c.id
My task is to select all rows from ab and c where a.date
is older than 6 months and insert them into a new db for archiving.
Here is what I have so far:
insert into dbArchive.a select * from db.a where receivedDate < CURRENT_DATE() - INTERVAL 6 MONTH;`
This seems to select all entries from table a that are older than 6 months and insert them into the archive db table a.
What is the best and most efficient way to find all rows from tables b and c that have the same id and insert them to the archive db tables b and c?
I have over 1 million records that are older than 6 months so I am weary about performance issues.
After inserting into dbArchive.a
as you have done here, you can run the following to insert into dbArchive.b
and same way for dbArchive.c
.
INSERT INTO dbArchive.b(id, etc..)
SELECT db.b.id, etc..
FROM dbArchive.a INNER JOIN db.b ON dbArchive.a.id = db.b.id;
So after playing around for a while this seems to be the best approach I have found that actually works:
INSERT INTO archive.b SELECT * FROM db.b WHERE `id` IN( SELECT `id` FROM archive.a )
I am not sure if this is the most efficient method but it gets the job done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.