6

Was curious to know which is faster - If i have an array of 25000 key-value pairs and a MySQL database of identical information, which would be faster to search through?

thanks a lot everyone!

1
  • 1
    hi, did you found the results ? Commented May 9, 2010 at 9:14

7 Answers 7

4

The best way to answer this question is to perform a benchmark.

Sign up to request clarification or add additional context in comments.

Comments

1

Although you should just try it out yourself, I'm going to assume that there's a proper index and conclude that the DB can do it faster than PHP due to being built to be all about that.

However, it might come down to network latencies, speed of parsing SQL vs PHP, or DB load and percentage of memory use.

Comments

1

It depends. In mysql you can use indexing, which will increase speed, but with php you don't need to send information through net(if mysql database on another server).

Comments

1

My first thought would be it is faster searching with arrays. But, on the other side it really depends on several factors:

  1. How is your databasa table designed (does it use indexes properly, etc)
  2. How is your query built
  3. Databases are generally pretty optimized for such searches.
  4. What type of search you are doing on the array? There are several type of searches you could do. The slowest is a straight search where you go through each row an check for a value, a faster approach is a binary search.

I presumed that you are comparing a select statement executed directly on a database, and an array search in php. Not

1 Comment

Hey Nikola, that is exactly what I would be doing - essentially comparing a string acquired from a Select Statement on the database to a string got from User-Input. I'm comparing this to the array_search function in PHP
1

One thing to keep in mind: If your search is CPU intensive on the database it might be worth doing it in PHP even if it's not as fast. It's usually easier to add web servers than database servers when scaling.

Comments

0

Test it - profiling something as simple as this should be trivial.

Also, remember that databases are designed to handle exactly this sort of task, so they'll naturally be good at it. Even a naive binary search would only have 17 compares for this, so 25k elements isn't a lot. The real problem is sorting, but that has been conquered to death over the past 60+ years.

Comments

0

MySQL is built to efficiently sort and search through large amounts of data, such as this. This is especially true when you are searching for a key, since MySQL indexes the primary keys in a table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.