Page 1 of 1

Sessions vs. Database

Posted: Thu Jan 12, 2006 1:29 pm
by FlyingHero
Hello everyone :)

Could you please tell what is more preferable: to store user information in session after retrieving it once from database or to send queries to database server every time this information is needed. The problem is that the retrieved (and then stored) information can be relatively large (1500-2000 records, each contains integer values, or possibly strings). The data is requested very often. The query that retrieves data is quite complex and it uses table joins. There might be many tens of thousands of users (or even hundreds) and also tens of thousands might be using web-service at the same time.

What can you say about the productivity and speed of both methods? Thanks :)

Posted: Thu Jan 12, 2006 1:38 pm
by RobertPaul
It does sound like you need some sort of caching solution, but I don't think that sessions are the answer.

Posted: Thu Jan 12, 2006 1:40 pm
by feyd
sessions, given the two choices, depending on the rate the records involved changes. An alternate is using a temporary table or view (if supported).

Posted: Thu Jan 12, 2006 2:58 pm
by FlyingHero
But won't this session variable entries be too large? Some people say that serialization/deserialization time for such kind of large session variables migth be longer than any kind of even most complex queries.

The information that we store changes rarely, maximum 3 times during the session.

Posted: Thu Jan 12, 2006 6:30 pm
by AKA Panama Jack
Actually using a database is far, far worse when it comes to speed. The serialization and deserialization of even long session data strings is faster than pulling the data from a database.

Also, many of the database caching schemes also serialize and deserialize the data. If you are using something like ADOdb for caching then you are going to introduce alot higher CPU load and be slower than using sessions. ADOdb's caching system is one of the slowest out there. About the only way caching under PHP is worthwhile is if the database has it built in and enabled. MySql can do this in later versions but in most cases it is disabled by default.

I have performed many tests with large amounts of data and using a database to retrieve the data over and over again was always many, many times slower and more CPU intensive than using session storage.

Posted: Fri Jan 13, 2006 7:16 am
by BDKR
I'm going to back up Panama Jack here. The only time a database would be a better solution is in the case of a web farm (web cluster) where the cluster managers (for whatever reason) aren't capable of returning a user back to the same machine in the cluster at each request.

Alternatively, there is Msession, which is specifically designed to be used in a cluster, but I'm not sure what's up with that project anymore as the old URL that I was aware of now redirects to Microsoft.
8O