Which would be faster: DB or Session?

AdamDuritz99

Diamond Member
Mar 26, 2000
3,233
0
71
Ok, i'm working on a program in PHP and MySQL and I was wondering which is faster? Would it be faster pulling all the info need from a user in a database and putting it into a session right when they log on in just one data fetch? Or would it be fast to just pull the data as needed? Like one page it might just pull data it needed to log on then later on pull their email or their address.

peace
sean
 

AdamDuritz99

Diamond Member
Mar 26, 2000
3,233
0
71
Hmm, Ok I'll try to explain it a little better.

Let's say I was building a web based program that requires users to log on like ign.com or ebay.com to use their service. In PHP you could use cookies or sessions, and i'll be using sessions for many obvious reasons. Would it be faster for the website software and server when a user logs in,that I go ahead and put all information about that user that would be userful to work with the site into the session. So I only have to call a varible in the session when needed instead of querying the database again for more info about the user. Or would querying the database be faster than pulling session varibles? If I pull all infomation at the time of log in, there will be about 10-20 individual varibles in an array. EX: $SESSION["user"]["username"], $SESSION["user"]["f_name"], $SESSION["user"]["l_name"], $SESSION["user"]["address"], etc...

peace
sean
 

RedRooster

Diamond Member
Sep 14, 2000
6,596
0
76
Personally, I'd pull the username or userId or whatever on login, and then depending how often the other info is used I'd make my decision based on that. If any of the user's info is used on more than one page, I'd just put all the info into session variables(which I LOVE in PHP, by the way) and use it when needed. If you're site only has a dozen pages or something, then perhaps just use the userid in a session and pull the other stuff from the db when needed.
So, in short, about 80% of the time, I'd choose just to have everything as session variables.
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
I think it depends on your design goals and how much user info you will be pulling.

If you store too much into your session variables, it could make your app less scalable.

However, assuming that your server has the memory resources and you don't serve millions of concurrent users, I'd say it's better to pull all the info into session variables.

But then I'd pull them out only if I absolutely need to. There is no point in getting all the user info if they will sit in memory doing nothing. And any connection to the database causes performance impact.

I'd pull out the most frequently and likely accessed user data into session variables. And if needed, make some trips to the database to pull out more details.