Server Load and PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Server Load and PHP

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.. :)
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Last edited by feyd on Wed Jan 19, 2005 8:49 pm, edited 1 time in total.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that stuff has to do with browser/client-side caching.. nothing with the server.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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..
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

patrikG wrote: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.
Know of any good online tutorials?

Cheers

Ben
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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:
patrikG wrote: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.
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.
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post 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.
Post Reply