Gavin
Gavin

Reputation: 1

What is the Best way to "Store" data for quick and repetitous retrieval

Fictious Background I get 100 hits a minute for "the hottest car"

The hottest car always changes by the minute and its currently: "A Pinto"

Everytime I receive what the current hottest car is, I save it into a MySQL Database.

Situation Everytime I get a hit for "Whats the hottest car", I need to return the answer. I feel confident that retrieving the answer from a file vs. a DB will be faster and less work for the processor due to PHP storing the file into memory. My concern is that if I get a new file, how do I make sure i'm returning the information in the new file and not the old information stored in memory.

P.S. If my assumptions are wrong and there is a faster way, please let me know.

Thanks

Upvotes: 0

Views: 1983

Answers (5)

Steven
Steven

Reputation: 928

Be careful with your assumptions. It can be tempting to assume that file access is faster, since you just have to read a file instead of connect to, query, and retrieve from a database. But bear in mind that databases are designed from the ground up for this kind of fast access to rapidly-changing information, and they have a lot of optimizations built in.

So look into caching, it is often a win, but I would not assume that file access is always faster. You can of course profile the different approaches to see if you have a bottleneck.

Upvotes: 2

Alfred
Alfred

Reputation: 61793

Your assumption is most probably going to be invalid. MySQL has a query cache which keeps your query in memory. Even if not I don't think you should but using the filesystem or unless you are using /dev/shm because that's mapped to memory. I would use a library like Cache_Lite to ease the pain of caching.

But if you want to make your site really fast you should install APC. You should always install APC if you want your website to be fast because caching the compiled bytecode of PHP scripts. Or use an in-memory databases redis or memcached, because these are even better in memory databases. redis is the easiest to install only using make and you don't need any ROOT permission either.

P.S: You should check out this redis tutorial because it is really powerful in-memory database.

Upvotes: 1

mfonda
mfonda

Reputation: 8003

Memcache, memcache, memcache!

https://www.php.net/memcached

Upvotes: 0

Dr.Molle
Dr.Molle

Reputation: 117354

Instead of using a DB/file it should be the fastest way if you directly access the memory: http://www.php.net/manual/en/book.shmop.php

Upvotes: 0

Jonah
Jonah

Reputation: 10091

I'm pretty sure it would be a bad idea to store that information in a file. The biggest problem is file read locks. If one person tries to get the file while another person is getting it, there's a conflict and a fatal error.

You really should go the database route, especially if you're planning on persisting the older "hottest cars". And if performance is a concern, you should look into PHP caching (see @Andrew's comment).

Upvotes: 0

Related Questions