views:

103

answers:

5

How would you temporarily store several thousands of key => value or key => array pairs within a single process. Lookups on key will be done continuously within the process, and the data is discarded when the process ends.

Should i use arrays? temporary MySQL tables? Or something in between?

+1  A: 

Memcached is a popular way of caching data.

If you're only running that one process and don't need to worry about concurrent access, I would do it inside php. If you have multiple processes I would use some established solution so you don't have to worry about the details.

Fabian
I only need to store the data within a single process. The data is generated there, and it dies there. I don't see how memcached would help with that?
Mads Jensen
Then I would argue against mysql, just keep the data inside php
Fabian
memcached should be faster than mysql for this specific type of problem
jigfox
But memcache is a CACHE, that means data gets lost, when the cache is full.
ZeissS
I don't know a lot of memcached, but I believe it can be configured to keep the date until it's deleted by the user or by the application. But you can also use Redis, what is similar to memcached, and redis definitely don't remove data until the user or the app removes the data, and it's really fast
jigfox
+1  A: 

It all depends on your application and your hardware. My bet, is to let databases do (especially MySQL) just Databases' work. I mean, not to much work than store and retrieve data. Other DBMS may be real efficient (Informix, for example) but sadly, MySQL is not. Temporary tables may be more efficient than PHP arrays, but you increase the number of connections tu the DB.

Scalability is an issue too. Doing it in PHP is better in that way.

santiagobasulto
+2  A: 

It depends on how many several thousands mean and how big the array gets in the memory. If you can handle it in PHP, you should do it, because the usage of mysql creates a little overhead here.

But if you are on a shared host, or you have limited memory_limit in the php.ini and can't increase it you can use a temporary table in MySQL.

Also you can use some simple and fast key value storage like Memcached or Redis, they can also work in Memory only, and have a real fast lookup of keys (Redis promises Time Complexity of O(1))

jigfox
What is the down vote for? I would appreciate a comment here, so I know what I did wrong.
jigfox
+2  A: 

Several thousand?! You mean it could take up several KILObytes?!

Are you sure this is going to be an issue? Before optimizing, write the code the simplest, straightforward way, and check later what really needs optimalization. Also, only having the benchmark and the full code will you be able to decide on the proper way of caching. Everything else is a waste of time and the root of all evil...

sibidiba
Thank you for setting me straight. I accepted another answer, because it fit the question asked. But your answer has a very valid point, which I will keep in the back of my mind. I always spend to much time optimizing unfinished code.
Mads Jensen
A: 
  • If the data lookup will be done with only few queries do it with mysql temporary table.

  • If there will be many data lookups its almost always best to store it in php side. (connection overhead)

Imre L