Mysql – Handling large DB tables that have millions of records

MySQL

So I have a large table(Table1), with about 5 million records. I have a second table(Table2) with about 108 million records. I am using a key value from Table1 to find any matching records in Table 2( there will be between 10-150 records per match). I am creating a new table for those records and deleteing them from Table2. Like a backup except not all records in Table2 will match something from table1. I have tried a whole match "select * from" which is way too large and have tried going one record at a time from Table1 but calculated it would take 35 days to run after letting it run for 2 days. So I guess i am looking for a good solution for handling large tables that isn't going to take forever and that doesn't run out of memory.

Thanks, feel free to point me to another question if it matches but i couldn't find any

Best Answer

You can try this:

create table Table3 as (select * from Table1 A where 
exists(select * from Table2 B where A.key=B.key));

And then delete from Table2 using newly created table Table3

delete from Table2 A where exists(select * from Table3 B where A.key=B.key);