Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in PHP, which is faster - reading a file or a database call?

I've a web app built in PHP with Zend on a LAMP stack. I have a list of 4000 words I need to load into memory. The words have categories and other attributes, and I need to load the whole collection every time. Think of a dictionary object.

What's the best way to store it for quick recall? A flat file with something like XML, JSON or a serialized object? A database record with a big chunk of XML, JSON or a serialized object? Or 4000 records in a database table?

I realize different server configs will make a difference, but assume an out-of-the-box shared hosting plan, or WAMP locally or some other simple setup.

like image 855
Corey Avatar asked Oct 06 '10 22:10

Corey


People also ask

Which is faster reading from file or database?

Especially in the case when small files are read first time, MySQL database is much faster than a pure filesystem. This is probably caused by better caching in MySQL database.

Which is faster PHP or SQL?

If you are fetching data from SQL and then doing the calculations in PHP over the fetched data its far better to fetch the required result and avoid PHP processing. The time taken to fetch the data in SQL is time consuming but once its done calculations are more over same.


1 Answers

If you're using APC (or similar), your fastest result is probably going to be coding the word list directly into a PHP source file and then just require_once()'ing it.

like image 83
Alex Howansky Avatar answered Oct 10 '22 15:10

Alex Howansky