Best Practice Question - Performance Efficiency & Resource Utilization
Does anyone have any general guidelines/preferences relating to the use of:
- row results from a MySQL Table, vs.
- $_SESSION variables with stored values, vs.
- CONSTANTS from define() statements
I am interested in knowing what might be the best method to using variable data throughout the processing of a stream of PHP scripts.
Generally speaking, is it faster and more memory efficient to select the variable data from MySQL tables and refer to that data throughout the scripts by pulling from the query results,
or is it faster and more memory efficient to echo the variable data stored in session variables,
or is it faster and more memory efficient to echo the variable data defined as constants?
I know that somewhere therein, lies the proper use of a combination of these methods, but what tends to be the best from a performance and resource standpoint.
Well, first we need to clarify if this data is constant (unchanging) and the same for all users. If not, then we can forget about define()-ing constants.
If it is variable data, then we also need to know again if it is the same for all users or can be different. If it can be different, then we probably need to use sessions in some manner, though you still have the option of whether to save the session data in the file system (e.g. by the standard PHP session handling) or in the database (which can be done with a custom session handler).
If at this point it's still a choice between file-based sessions or database-based sessions, the filesystem approach is probably slightly faster, though if it uses the same database connection you were going to use already for that script, then the database need not be noticeably slower (assuming you've created a reasonably efficient database design). A database solution can, on the other hand, be somewhat more secure and robust, as it takes advantage of the strengths of the DBMS.
The data changes very infrequently and is common to all users during processing. Example, I have a set of variables that allows for customizing static header and footer data on the page. If the user wants to update this information, I could have them update a db table containing the info or update a text file -- it doesn't matter to me which way the user updates the info.
What I am interested in is:
- is it more efficient to store this data in a table and then pull from the table to dynamically construct the web page on each and every page build during the session
- or is it more efficient to store this data in a table, then load the data into session variables once at the outset of the session and refer to the data from the session variables throughout the remainder of the user session on each and every subsequent page load
- or is it most efficient to store this data in a file as constants and build the pages from the constants on each and every subsequent page load for the user session?
Hope that clarifies my general question a bit...
In terms of processing efficiency/speed, I suspect the best would be to put them into a PHP include file. Whether they would be constant or variable declarations probably wouldn't matter in terms of speed, though constants have global scope, so that might be a factor (and of course they are constant and cannot be changed once defined).
The downside of such an approach is that if you need to change those values, someone has to edit the PHP source code, which is more prone to bugs/mistakes than using an admin front end to modify a database, for example.
A possible in-between choice would be to use a config file along with parse_ini_file() to load your variables, while changing the values would require only editing a fairly simple text file.
My personal favorite when working in an object-oriented application is to use class constants in an interface, which can then be implemented by any class which needs to use those constants. (This has the same drawback of a plain PHP include file in terms of changing values.)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)