Page 1 of 2
Server Load and PHP
Posted: Wed Jan 19, 2005 8:02 pm
by Mr Tech
Hey there,
Are there any tips on writing PHP to keep the Server Load low and quicker laoding especially for scripts being viewed a lot of tiems daily?
Thanks
Tech
Posted: Wed Jan 19, 2005 8:07 pm
by feyd
preprocessing content that isn't exactly dynamic. Using caching techniques. Optimizing the script to use less loops, or more efficiently use them. Make fewer queries.. there's a few..

Posted: Wed Jan 19, 2005 8:30 pm
by Mr Tech
Sup Feyd?
Could you please explain this a bit more? I don't quite understand:
Preprocessing content that isn't exactly dynamic.
Also regarding using caching techniques, if a dynamic page is updated will the cache stop it from displaying it or will it update?
Have you got any sample of a cache that you would recommend?
Posted: Wed Jan 19, 2005 8:39 pm
by feyd
the caching routine should be aware of how to check if an update is needed, provided the cache is x period of time old. i.e., cached data shouldn't need to update too often. If the content is continually updated, then it shouldn't be cached, as it's a waste of time.
as for preprocessing content that isn't exactly dynamic.. This is part of caching, typically.. like say you have multiple includes that happen routinely.. instead of including 10 files, you consolidate them into 1 or 2. Another example is if you reuse the same query in seperate places during a script. Instead of querying the database again, it should have been stored (provided it was not too big) into a variable for easier access.
there's Zend's Optimizer as well.. which can speed up a script.. but I think those should be used as last resorts, instead of band-aids.
Posted: Wed Jan 19, 2005 8:47 pm
by Mr Tech
OK thanks feyd.
Well the script is mostly updated 4-5 times(ocne a day) a week. Sometimes there are really small updates every once in a while. So I suppose I should try out the cache...
I'll see if I can find some code for it.
Cheers
Tech
Posted: Wed Jan 19, 2005 8:54 pm
by Mr Tech
Hey Feyd,
I had a search in Google and found that to use cache you need to modify the php.ini file etc. Is it possible to cache without it?
I found this code though:
Code: Select all
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");
Header("Content-Type: image/gif");
Can that be used? Any ideas on what needs to be changed as by the looks of things its turned cache off?
If it can't be used can anyone recommend a good cache script preferrably that doesn't require the php.ini file to be updated?
Cheers
Tech
Posted: Wed Jan 19, 2005 8:58 pm
by feyd
that stuff has to do with browser/client-side caching.. nothing with the server.
Posted: Thu Jan 20, 2005 3:43 am
by patrikG
Schlossnagl's "Advanced PHP Programming" (see:
http://www.phpclasses.org/reviews/id/0672325616.html ) has a chapter dedicated to caching, application optimisation and server load.
Posted: Thu Jan 20, 2005 8:38 am
by magicrobotmonkey
Whilst programming, I try to keep access times in mind. The time it takes to access the DB is a couple of orders of magnitude higher than the time it takes to hit memory - likewise, but not as bad, with disk accesses. So generally I try to pull in any info that has to come from the database as efficiently as possible and just keep it in memory (generally arrays).
This includes some obvious things like filtering things on the DB end rather than the PHP end. I have situations where an ton of rows is returned and the code is only looking for one.
Another thing is using left joins. Often multiple querys can be crunched down to one with some tricky SQL. I think that SQL is often underutilized by PHP developers. Of course, this left join thing doesn't always work - sometimes it is much slower when done this way. This is where using some timers to help you optimize ccan be useful.
Posted: Thu Jan 20, 2005 8:56 am
by onion2k
Your code should have to do as little work as possible in order to get the job done. Pages should be cached where possible, static content should exist as precompiled data, scripts should access the file system as little as possible..
Posted: Thu Jan 20, 2005 8:58 am
by onion2k
magicrobotmonkey wrote:I think that SQL is often underutilized by PHP developers.
Newer versions of MySQL having things like stored procedures, transactions and triggers will help this sort of thing a heck of a lot. Or you could just use PostgreSQL now..
Posted: Thu Jan 20, 2005 4:45 pm
by Mr Tech
Know of any good online tutorials?
Cheers
Ben
Posted: Thu Jan 20, 2005 4:56 pm
by timvw
onion2k wrote:magicrobotmonkey wrote:I think that SQL is often underutilized by PHP developers.
Newer versions of MySQL having things like stored procedures, transactions and triggers will help this sort of thing a heck of a lot. Or you could just use PostgreSQL now..
I think the point of magicrobmonkey is that most people don't even know what joins,stored procedures, transactions, triggers are.
Currently i choose dbms like this:
want to select data really fast -> use mysql
want more functionality -> use postgresql
want to handle files -> use filesystem
Posted: Fri Jan 21, 2005 2:55 am
by patrikG
timvw wrote:onion2k wrote:magicrobotmonkey wrote:I think that SQL is often underutilized by PHP developers.
Newer versions of MySQL having things like stored procedures, transactions and triggers will help this sort of thing a heck of a lot. Or you could just use PostgreSQL now..
I think the point of magicrobmonkey is that most people don't even know what joins,stored procedures, transactions, triggers are.
Currently i choose dbms like this:
want to select data really fast -> use mysql
want more functionality -> use postgresql
want to handle files -> use filesystem
Stored procedures & triggers will be part of MySQL 5, which is due to be officially released some time this year. These features have been essential part of other databases for a long time, such as PostgreSQL, Firebird, Oracle, MSSQL etc. Currently, nothing of the sort is available in MySQL.
To optimise, you don't need stored procedures or triggers in the first instance (and transactions have nothing to do with optimisation). If you're not familiar with them, read up on them, but think more about how you can optimise your SQL, before you start thinking about advanced DB features.
If you don't know your SQL, your stored procedures will be just as inefficient. Subqueries (which are common in other databases, will be part of MySQL from what? release 5 or was it the lates 4.x release?) are very useful for optimising your SQL, joins as well.
Remember: optimising is not so much a question of "shall I use arrays or objects", but a question of how much data the entire application environment needs to handle. The less, the faster your app will run, even if the code is unoptimised. The point I am making is simply: use SQL to minimize the amount of work PHP needs to do - SQL can be very powerful.
Of course, it's an artform to correctly identify the bottleneck of an application. Caching can help, but not solve the problem - if you're queries are unoptimised, not much will change. Maybe it's the server setup or the network environment that's causing your app to run so slowly. I've always found it amazing to watch pros at work when they're optimising.
Mr Tech wrote:
Know of any good online tutorials?
I'd advise spending the money on the book if you're serious about advanced PHP. Schlossnagle's book is very well worth the money imho.
Posted: Wed Feb 23, 2005 4:28 pm
by bg
dont know how much this would help, but using ' instead of ", especially for static strings, will stop php from parsing the string for variables. I use single quotes exclusively, imho it makes the code easier to read when variables are not embedded within a string.