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 8281025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:53:31+00:00 2026-06-08T09:53:31+00:00

I am making a piece of PHP code that takes user XML input (containing

  • 0

I am making a piece of PHP code that takes user XML input (containing multiple records/items, usually around 20 to 100), parses it, and then checks it against a database of records. If a record is not in the database, the PHP script should INSERT it. If the record is in the database, the script should either discard the user’s input or run an UPDATE of that record, depending on whether the user has a ‘replace records’ checkbox checked.

My question is, which is faster: To SELECT the columns of the entire table that determine uniqueness, then sort through them in PHP? Or, for each record, to do a SELECT COUNT() FROM table WHERE name=(input name) AND region=(input region) and see if any records come back?

One big SQL query + quite a lot of PHP sorting time, or 100 small SQL queries and one PHP comparison?

EDIT: People have been requesting details, so:

Database size: 250 records or less
Columns indexed: I haven’t put indexes in YET, but I will set up the name and region columns with an index in the production version.
Format of returned SELECT: If I do the big SELECT, it’ll be returned in an associative array of row objects, due to the DB class I’m using (WPDB).
What constitutes uniqueness: The name and region columns determine if a record is unique. If a name-region combination is not in the database, then the record is unique.

As an example, name:”Paris” region:”France” and name:”Paris” region:”Texas” are two unique records. But, so are name:”Paris” region:”France” and name:”Marseilles” region:”France”.

  • 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-06-08T09:53:32+00:00Added an answer on June 8, 2026 at 9:53 am

    It’s faster to use “REPLACE INTO”. Syntax is exactly the same as “INSERT INTO” but if there are any values that are not unique that must be unique because of an index (either a Primary Key or a Unique Index) then the existing record will be replaced. No need to check in advance, and it’s one query.

    Note that replace will replace the ENTIRE record. If you want to simply update one field then run “INSERT INTO… ON DUPLIACTE KEY UPDATE field=value” and this will insert the new record or update an existing one. Still one query.

    As for the other part (removing records if checkbox checked)), simply run “DELETE” and if the record is not there, it won’t be deleted – again, one query.

    This way, you don’t risk a record being added/deleted between your check and your update, plus you save queries, plus it’s easier for someone else to read what’s going on.

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

Sidebar

Related Questions

I'm making a C++ wrapper for a piece of C code that returns a
I am currently making a piece of software that will allow the user to
I'm making a simple interface that will allow a user to modify pieces of
I am making a piece of code for DirectX app. It's meaning is to
I'm making a Grade log application and I have this piece of code here
I have a piece of jquery code that isn't firing correctly in IE (8
This is my first php script. I actually code in vb.net.. I am making
I dont know what am doing wrong with this piece of Code. Its making
I'm making an applet that lets users crop out a piece of an image
I am making an app that plays audio... with each sound piece playing I

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.