Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php session vs mysql speed

Tags:

php

mysql

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.

like image 403
vener Avatar asked Dec 09 '22 17:12

vener


2 Answers

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.

like image 164
Vinko Vrsalovic Avatar answered Dec 26 '22 02:12

Vinko Vrsalovic


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>";
like image 26
Mohamad Hamouday Avatar answered Dec 26 '22 02:12

Mohamad Hamouday