I am trying to import data from a large database. I have two tables with couple of hundred thousands records. I have to search data in two tables then insert that record in a new table (3rd table), if the record already exists in 3rd table then I have to update one column record in 3rd table.
This sounds easy, but it is taking so long to process.
Below are sample queries and pseudo code:
select * from table1 INNER JOIN table2 USING(id)
search in table 3 ->
if record exist{
update record in table 3 (update counter in a column)
}else{
Insert new record in table 3
}
First and second table has more than two hundred thousands records. As I start inserting record in 3rd table it kills the whole speed because then it also have to search in 3rd table to update or insert a record.
Database Name = MySql
Language = Php
What is the problem? How can I improve this? I can not wait hours to process it :(
Thanks
EDIT:
In table 3, id has primary key and all other columns are normal. Database schema is too big plus complex. Do you guys want , I paste hundred of lines here?
Can you please guys point out mistake in my pseudo code and query? What index or structure I can use to improve performance??
Structure
Table 1 - usr_id, first name, last name (usr_id is primary key)
Table 2 - id, usr_id, amount (id is primary key and usr_id is foreign key)
Table 3 - new_id , first name, last name, usr_id, total_amount (new_id is primary key and usr_id is foreign key
)
I check if table 3 has same first name and last name then update total amount, if they are different then insert a new record