Is searching an PHP array faster than searching / retrieving from MySQL

It was interesting to know which is faster - if I have an array of 25,000 key-value pairs and a MySQL database with the same information, which will be faster to search through?

Thanks everyone!

+6
php mysql
source share
7 answers

The best way to answer this question is to run a test.

+5
source share

Although you should just try this on your own, I'm going to assume that there is a valid index and conclude that the database can do it faster than PHP because it is created for everyone.

However, this can lead to network latency, SQL and PHP parsing speeds, DB load and memory utilization.

+1
source share

One thing to keep in mind: if your search requires heavy use of resources in the database, it might be worth doing it in PHP, even if it's not so fast. When scaling, it is usually easier to add web servers than database servers.

+1
source share

Test it - profile something simple, as it should be trivial.

Also, remember that databases are designed to handle just this kind of tasks, so they will naturally be good at that. Even a naive binary search would have only 17 comparisons for this, so 25k elements are few. The real problem is sorting, but it has been conquered to death over the past 60 years.

0
source share

It depends. In mysql, you can use indexing, which will increase speed, but with php you do not need to send information over the network (if the mysql database is on another server).

0
source share

My first thought would be to speed up the search using arrays. But, on the other hand, it really depends on several factors:

  • How is your databasa data table created (does it use indexes correctly, etc.).
  • How is your request created?
  • Databases are generally quite optimized for such searches.
  • What type of search are you doing in the array? There are several types of searches you could do. The slowest is a direct search, in which you look at each line, checking the value, a faster approach is a binary search.

I suggested that you compare the select statement executed directly in the database and the array search in php. Not

0
source share

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

0
source share

All Articles