A Performance Concept: Knowledgeable Feedback Required

A

Anonymous

Guest
Please provide feedback regarding this concept.


Purpose:

To minimalize server resourcse.

Concept:

If web users are pulling the same data commonly from a MySQL database, check to see if the queried data exists in a session file, if the data does not exist: store the common queried data in the session file. Finally, use the session variables with the queried data instead of subsequent calls to the database.

Performance:

This would absolutely minimalize subsequent calls to the database. But, what negative effects might happen? The main questions is..."Will this create a performance boost?" and "Do session files require more processing/bandwidth/etc than Mysql queries?".

Thank you
 
That's probably what I should have done...but using a session avoids a mysql connection call completely. Here is an example of the method I am using:

Code:
// Retrieve Store Settings, Append To Session //
if (!isset ($_SESSION['store_name'])){
	$query = mysql_query ("SELECT store_name,phone,email,state,logo,target_department,theme,products_per_page,posters FROM settings");
	$row = mysql_fetch_array ($query);
	$_SESSION['store_name'] = $row['store_name'];
	$_SESSION['phone'] = $row['phone'];
	$_SESSION['email'] = $row['email'];
	$_SESSION['state'] = $row['state'];
	$_SESSION['logo'] = $row['logo'];
	$_SESSION['target_department'] = $row['target_department'];
	$_SESSION['theme'] = $row['theme'];
	$_SESSION['products_per_page'] = $row['products_per_page'];
	$_SESSION['posters'] = $row['posters'];
	$_SESSION['merchant_logo'] = 'images/' . $row['theme'] . '_merchant.jpg';
	$_SESSION['arrow'] = 'images/' . $row['theme'] . '_arrow.gif';
	$_SESSION['signs'] = array ('images/' . $row['theme'] . '_plus.gif','images/' . $row['theme'] . '_minus.gif');
}
 
Ah, ok. But thus we are talking about different things.
Yes, in that situation... that's the best method.
You can also use "normal" arrays for that.
 
Well...I'm getting a bit nervous about the amount of content I am putting into my session...mostly because I don't know how much information sessions can withstand before performance goes downhill.

session variables are no different than normal variables...right? It's just like including a file into memory? I don't know how well memory is handled with php sessions or php variables in general...I imagine this is based on the operating system's memory management? I know there will probably be about 100-500 array elements in this enormous session array structure.

The data flows into the structure only when it is required...which is so perfect. It's like a visual basic dataset customized for performance.

The session files are stored on the web server until they are destroyed right? And the variables are stored into the memory of the client computer?

Thank you for your help. I'm pretty sure it's a very good idea for managing data on a large scale website.
 
Well i'm not sure what kinda of project you're building, but let's say session variables are not your solution. Of course you must use it, but not if you're using it to store information that may interfer in the presentation of your website such as layout, images, etc. You shouldn't do this. You can also have a MySQL based session handler. Actualy a combination of MySQL query caching plus a templates system that also uses cache can help you a lot in the performance of your website. I suggest you to take a lot at the PHPBB forum code so you can have an idea on how all that system and users' information are handled.
 
The query cache only caches exact queries...I have thousands of unique queries because my application is a catalog of directories and products. Additionally, I do not have access to the mysql configuration on most web hosts.

---

What I was doing was putting my catalog listings into a session array, only as they are required. So, if you are browsing my site...if you go to the "apparel department", the entire department STRUCTURE is saved in the session file. When you go the next step to view a group of products, they are then saved in apparel department structure. So, you can see that if you load the department, the department listings are cached (i.e apparel shirts, apparel jeans) into the session. Then, if you click on apparel jeans, the jean products will be saved to the session in the apparel department under the apparel jeans listing.
It's failsafe as products are rarely added or deleted...but does it consume too much memory?

I believe that loading the structure of the catalog into an array and simply formatting the array structure into a string to display the html would be great...it still may be, but no one I have asked can tell me if it will destroy the web server performance. In other words, once I have harnessed by query results, it strategically place them in the session file in a structure. Then I use foreach loops on the session structure array instead of queries to the database.

Okay. So here is what I think I know. And what I need you to help me understand if you would be so kind as you have already been. I thank you.


1. Session files are stored in a temporary directory on the webserver. They are simply files and reserve no virtual memory. They will be deleted every 30 minutes so if they are 10kb in size, no big deal. that's 500mb for 50,000 users...sounds big but not for 50,000 users. mostly only a portion of that disk space will be used as the sessions are deleted frequently.

2. The session file is loaded into client memory, not server memory, I HOPE! THAT is the BIG QUESTION. Does anyone know? If the file is stored on the server and it's contents are only loaded into memory on the client, than my system is an excellent cache.

But I need to know if the session is stored on web server memory or client memory...this is the big question.
 
It seems there might be a HUGE problem. I just viewed the size of a session file after only storing 10 products. It was 12k. I imagine some files would be around 50k per user. This is terrible for disk space on the server. God, I wasted so much time working on this...like 30 hours.

But this does bring up a good question....

What if I created a cron job that pulled out the entire contents of my catalog from the database each night at midnight and stored it in a file on my server. I could serialize the information into the file and unserialize it into variables at runtime. It would be one file....everyone could use it for that day and it would be update each night.

This may actually be a great idea. But the question arises again...how much memory is too much? I will have to run a test to see if my system handles the memory well. If it does, I think this may work.

What do you think?
 
For hierarchical website structures such as menu trees, catalogs, and/or any large group of records that uses a database...

If mass quantities of unchanging data are stored in your database and you have a large volume of users, it might be a good idea to setup a script that pulls the data from the database and serializes it into many small files. Each file should be as small as possible, containing the smallest set of data required for it to be its own component within your data structure...watch out for how much memory you occupy.

When the data is required, include the serialized file in your website files. Unserialize the data. This loads the data into an array of variables for usage in the website file. It's like having non-volatile session files...except its only one session file for all users.

The serialized files should be recreated daily at a time when traffic is minimal by setting up a cron job for the script.

Of course, you can use the serialize() function to pass the array of data that was pulled from the database into a file for storage and the unserialize function to pass the string representation contained in the file as an array in memory which can then be used in your pages.

This is a rare method indeed...but still an interesting concept that a few people might implement.
 
Back
Top