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

  • SEARCH
  • Home
  • 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 4240916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:13:54+00:00 2026-05-21T03:13:54+00:00

I have about 30K records in an XML file and this file is updated

  • 0

I have about 30K records in an XML file and this file is updated all the time.

I’m trying to insert and if exists update a MySQL db.

This is the code I’d like to use, but it runs very slowly, does anyone have any ideas for improving its performance?

// getting xml file
$dom = new DOMDocument();
$dom->load('products.xml');

// getting xml nodes using xpath
$xpath = new DOMXPath($dom);
$productid = $xpath->query('//NewDataSet/Product/ProductId');
$price = $xpath->query('//NewDataSet/Product/Price');

// Reading all nodes and if mach found in db update price, else insert as new record**
for($i=0;$i<$allNodes->length;$i++){
    $testproductid = $productid->item($i)->nodeValue;
    $testprice = $price->item($i)->nodeValue;
    if(mysql_num_rows(mysql_query("Select productid from test where productid ='$testproductid'"))){
        mysql_query("UPDATE test SET price = '$testprice' WHERE productid = '$testproductid'");
    }else{
        mysql_query("INSERT INTO test (price, productid) VALUES ('$testprice','$testproductid')");
    }
}
  • 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-21T03:13:55+00:00Added an answer on May 21, 2026 at 3:13 am

    First, this line can result in bad behaviors:

    if(mysql_num_rows(mysql_query("Select productid from test where productid ='$testproductid'")))
    

    What happens if mysql_query() fails? Do something like that instead:

    $res = mysql_query("Select productid from test where productid ='$testproductid'");
    if ($res) {
    ... CODE HERE ...
    }
    

    Is productid an index? Also, you can formulate your query as:

    Select productid from test where productid ='$testproductid' LIMIT 1
    

    In this case, MySQL won’t look for more records. Also, try to insert more than one record in on single INSERT statement. See this:

    http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html

    Take a look at the REPLACE command. That would replace the SELECT/UPDATE/INSERT conditions, but it might not be a great improvement of performance though.

    http://dev.mysql.com/doc/refman/5.0/en/replace.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that has about 30k records, of which 2k I
I have a site that needs to search thru about 20-30k records, which are
I have a db table filled with about ~30k records. I want to randomly
I have about ten amazon ec2 servers all constantly writing to my mysql database
I have about 20 grid views that I have to create. All of them
I have about 8 entities that all have a one to one relationship with
I have about 1 million records in my contact table in the DB, now
i have a question about basic mysql database optimisation. I have 3 tables, Articles,
I have about 150 text files filled with character information. Each file contains two
I have about 100 elements in like and am trying to create an animation

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.