dngus
dngus

Reputation: 11

performance difference between querying a database or searching an array?

I have a suggestion feature that takes what the user has written so far (say "abc") and offers suggestions based on a list of 20k+ items.

Now I don't want anything fancy, it just searches for strings that begin with "abc", and thats what I want.

My question is: is there a huge difference in performance if the list of items is hard-written in a PHP array, against an SQL db?

thanks!

Upvotes: 1

Views: 1207

Answers (2)

a1ex07
a1ex07

Reputation: 37354

I'd say it depends. In most cases, database performs faster, but take into account that the most expensive operation is disk IO, and having data hardcoded in php array removes needs of reading data from hard drive. So if you you database has a high level of fragmentation, you may have a bit better performance with sequential search over array.
However, when it comes to large amount of data, db will do the job better.

Upvotes: 2

NiematojakTomasz
NiematojakTomasz

Reputation: 2473

Yes, Complexity of searching in database by indexed column should be about log(n), simply searching array will be n (unless you are using binsearch on sorted array). Anyway RMDBS will do it much quicker since it have strongly optimized code for doing such tasks and PHP is scripting language using references everywhere(very slow).

Upvotes: 1

Related Questions