Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3677536
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:14:00+00:00 2026-05-19T03:14:00+00:00

I am trying to import data from a large database. I have two tables

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:14:01+00:00Added an answer on May 19, 2026 at 3:14 am

    From your pseudo code I see that you check record existence in Table 3 for each record in select * from table1 INNER JOIN table2 USING(id). You eventually end up with thousands of select commands that degrade performance. You can improve it by

    select t1.*, t2.*, t3.pk_field from table1 t1 INNER JOIN table2 t2 USING(id)
    LEFT JOIN table3 t3 ON (join codition)
    

    Now you can just test if t3.pk_field is null (insert if it’s null, update otherwise). The next step to improve it is to write one query that takes care of everything , using INSERT ON DUPLICATE KEY UPDATE as proposed by jasonbar:

    INSERT INTO table3(col1, col2,...) 
     select t1.col1, t1.col2, t2.col3, .... 
     from table1 t1 INNER JOIN table2 t2 USING(id)
    ON DUPLICATE KEY UPDATE counter = new_value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do an import data from a query I created in MS Query. Lets
Is it possible to import outlined data from Excel? This is where Excel shows
I am trying to read binary data from sys.stdin using Python 2.7 on Windows
I'm trying to do moving some data from my development machine to a server
I'm trying to load a default set of data from a csv file into
I am trying to import an excel file using asp.net and C#. I found
I'm trying to load HTML/CSS from an external domain into a SWF using Actionscript
We currently have a java/jsp online web service that includes it's own custom calendar.
After trying to get this to work for a while and searching around I
I am trying to catch a file sent with form and perform some operations

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.