Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: sessions vs. database

I have a class that retrieves its memeber (more or less 10 members) from a database.

My question is: is it more efficient to fetch them every time from the db (MySQL), leaving just an ID in the session's array or store them directly in the session's array?

And is the difference in performance terms so great? (given a database with say 100.000 rows)

like image 340
Federico klez Culloca Avatar asked Jul 16 '09 23:07

Federico klez Culloca


1 Answers

Considering you're storing an ID in the session anyway, the session makes the most sense. Doing a session_start() loads your session information so whether you've loaded 1 or 10 items after that is largely irrelevant (unless they're really large but that'll be a problem in any case).

So stick with the session.

If you get really concerned about speed use an in-memory cache like APC or memcache. Worrying about speed for 10 items from the filesystem or database is a distraction. The difference is going to be so minimal as to be irrelevant.

Note: the above assumes two things:

  1. The query is performant (retrieving 10 rows out of 100k should be doable in under 0.1 seconds); and
  2. You are doing one query not 10.
like image 100
cletus Avatar answered Sep 21 '22 16:09

cletus