5

i am working a on a permission system. In each page it will need to check whether if the user has the permission to do so. I have two choices, store the data in a session variable (which is only updated during login) or query the database for the information every time. Which is faster?

I realized that if the permission changes, I will need to update the session variable, therefore the user needs to relogin to "see" the changes in permission, but that is not a factor in the decision, only speed is.

1
  • Is speed for the single session case the important factor here or do you need speed with a large number of active sessions? Commented Feb 23, 2010 at 7:50

4 Answers 4

7

Speed of SESSION vs. DB is dependent on a number of factors:

  • How much data will be stored in the session variable
  • What's the actual backing store of the session variables (if it is database backed sessions, it'll be basically the same time)

I can tell that for small amounts of data, file based session variables will be faster than DB access.

You need to measure it to obtain a relevant comparison between the two methods in your application. I personally doubt that it will make a such a difference as to not go for the session solution.

Sign up to request clarification or add additional context in comments.

3 Comments

the size of the data might be around 1kb, so does that makes a difference?
I know that this is old question, but would it be any impact on mysql connection time? because with mysql code need to establish connection to mysql, with session it doesnt. would this effect the performance?
If you can please provide some proof rather than theory that comes from nowhere. so everybody can compare it from the experiment itself..
7

Set new value in session take:

Time: 0.00062895 Seconds

Insert same value in database take:

Time: 0.00000811 Seconds

Insert same value in Cookies

Time: 0.00000906 Seconds

Or you can test by using this code:

$before = microtime(true);
    // Put your code here
$after = microtime(true);
$Speed = number_format(( $after - $before), 8);

echo "<h1>Time:  " . $Speed . " Seconds</h1>";

1 Comment

It would be perfect if this included about the taken time to obtain the data and the number of data too :)
1

I would store that kind of information in session :

  • That data is specific to the current user
  • Each user has its own version of the data
  • That kind of data is not likely to change often -- which means waiting until the session expires and the user comes back is often OK
    • and if you think it'll change often, you can keep some timestamp in session, alongside that data, to keep track of "when" that was fetched from the DB for the last time ; and if it's been fetched "too long ago", just re-fecth it every couple of minutes.


I would also add that, if one day you start having several distinct web-servers, you'll be able to store the session data using memcached -- which means it scales way better than the database.

Comments

0

Short answer: Storing it in a session variable is probably a little faster, since you've already populated it from the database. That being said, I doubt that the speed of a single simple database query will bog you down in any real measurable way.

1 Comment

When a user may are making request at 5 per second, I think it matters. (Rapid searching)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.