Forum Moderators: coopster

Message Too Old, No Replies

Performance: Session vs Query?

         

SeanF

4:52 pm on Jan 31, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



HI:

I have a question about the relative performance of building a large $_SESSION array vs querying a MySQL database when you need info. I am sure there is no simple answer... just looking for some initial thoughts.

The application is built in PHP / MySQL. There are a number of pages which use the same information. So, there are a lot of scripts like "get_business_info.php", "get_customer_info.php", etc. which retrieve data from MySQL tables and are "included" on various pages. Some page retrieve data and store it in the $_SESSION array.

From one page, I did a "print_r" on $_SESSION and there are 423 session variables / arrays set. This might be even larger the more pages a user goes through. Some of these variables (but probably less than 10%) are used on almost every page. Then, there are some (maybe 25% which are only uses on very occasional pages). And almost every page uses some combination "get_xxxx_info.php" routines.

So, the general question is: what's the relative trade-off of consuming RAM with too many session variables vs using computational time to retrieve data from database tables?

What's a good way to start to get my arms around this?

Thanks!

Dimitri

6:03 pm on Jan 31, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



If you can, the best is to limit the amount of MySQL (or other DB) queries. Even if the DB server is caching frequent queries, there is still the cost of a connection, and communication with the server (even if the server is running on the same machine).

As for the RAM, today, I doubt it will be an issue, excepting if you run on a very cheap server/vps or sharedhost.

w3dk

10:16 pm on Jan 31, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



From one page, I did a "print_r" on $_SESSION and there are 423 session variables / arrays set.


To clarify, all this information is unique to the user browsing the site? No other user would want to see the same information?

what's the relative trade-off of consuming RAM with too many session variables vs using computational time to retrieve data from database tables?


Are you using memcache or something? Otherwise, the default PHP session handler uses "files", not RAM. (?)

SeanF

12:20 pm on Feb 1, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Thank you both for your speedy comments.

W3dk, answers to your questions:
To clarify, all this information is unique to the user browsing the site? No other user would want to see the same information?

Some information will be the same for all users browsing the site (web site business info, etc), other information is specific to each user (their business info, order info, preferences, etc). The balance is about 50/50, universal/unique. Should I be treating these "buckets" separately?

Are you using memcache or something? Otherwise, the default PHP session handler uses "files", not RAM. (?

Perhaps I use the term "RAM" incorrectly. I am referring to the overhead of keeping information in "memory" rather than retrieving it from the database. The application is running on a Virtual Private Server and the RAM never seems overly loaded (2Gb RAM, 8Gb used of 60Gb Disk space). It's the only application running on the VPS and there are probably never more than a couple dozen users hitting it at any given time.

csdude55

1:19 am on Feb 21, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



FWIW, I use session variables on my site to limit the same MySQL queries over and over, but nowhere near as large as you have. As far as I can tell, the server stores the session variable at /tmp/ as a plain text file with a filename that's cross referenced to a cookie on the user's machine.

It's entirely possible that there's something special about the /tmp/ directory, but from my experience opening, reading, and then closing a plain text file is a bit slower to process than a MySQL database with a persistent connection. So I've been using them very sparingly.