0

Imagine we have a huge database (MySQL & PHP) with two tables that have:

Important id in table #1 
Important id in table #2

Logged in people to the site can like any record from these tables that they encounter on any page of the website. Those like will be stored and added live to:

Important_SESSION_Array_#1
Important_SESSION_Array_#2

On each like, the SESSION arrays get updated with the new value as first in the array. All the values are unique ID's: the AI integers. (And related to other tables in the dBase.)

Then, on each page load you access these two session arrays, and look for matching ID's from other tables that are loaded live.

This situation raises several questions:

  • how big or long can these session arrays be? Is there an actual limit?
  • is there any knowledge on this for the use on mobile devices and limited session storage on devices?
  • is there a better way for procedure on this? (I am sure there is!)
  • Any more insights on this are welcome

It is an example, but one that we actually encounter with over 20 million records on this level. Hence, we're looking for better ways to work with huge data amounts like these - even per user.

Any thoughts you have on this are very welcome!

KJS
  • 1,176
  • 1
  • 13
  • 29
  • You're loading your whole database into the session for each user? Why? Just use database caching for your queries or something if you're trying to minimize db performance problems. – developerwjk Aug 18 '14 at 23:20
  • Hahaha! Of course not! But we are at a situation that we really have to know all about anyone clicking around. Just hoping someone here can give us some more insights. I tried to keep the question simple, actually. – KJS Aug 18 '14 at 23:23
  • 1
    As to the maximum size of the session -- http://stackoverflow.com/questions/4649907/maximum-size-of-a-php-session and http://stackoverflow.com/questions/4274955/are-there-limits-for-session-variables – Kai Aug 18 '14 at 23:25
  • 1
    "Is there any knowledge on this for the use on mobile devices and limited session storage on devices?" The session is stored on the client as a single cookie with a unique ID, it maintains 'state' and links the browser to the data, in the session, on the server. There is 'no' actual data stored on the client apart from this ID. – MrYellow Aug 18 '14 at 23:29

1 Answers1

1

The maximum data to be stored in a session array is the same as the maximum data that PHP is allowed to use (memory_limit). Note that it might be inconvenient to fill the allowed memory up with session data only, leaving no space for any other variables.

PHP sessions are stored on the server, the capabilities of the client browser are irrelevant in this regard.

Even though it may be possible to allow PHP to have session data of 1 giga byte or more, it is considered bad practice when you are abusing the session to act as a cache. ALL data in the session is completly read into memory from permanent storage (most likely files) when executing session_start() and then deserialized (which requires huge amounts of CPU), and at the end of the script (or when executing session_write_close()) the whole data is again serialized (CPU!) and written back to permanent storage (I/O). Constantly moving 1 GB of data from and to file storage will considerably hamper your applications performance.

Saving the IDs of stuff a person likes into the session is not the best approach for this. Liking one single id is one INSERT into the database. Querying which IDs the person has liked is one SELECT. Both are powered by the correct index settings (the user id comes to mind), and will only ever be executed if the code actually requires that knowledge. This most likely is eating way less performance than storing that info in the session (with no upper limit of how many ids will ever be stored).

Sven
  • 69,403
  • 10
  • 107
  • 109
  • Good insights! Thanks! – KJS Aug 19 '14 at 08:02
  • I didn't know about 'PHP sessions are stored on the server'. More Q's come to mind: what is better: query every page to generate an array needed, or to store it in a cookie as well? (We have a lot of data to be active to run live for members of our site.) – KJS Aug 31 '14 at 23:45