TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-14-2007, 03:53 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default Optimizing PHP Code

I've created stuff in php before where it looks fine to begin with and then later down the road when the site starts getting traffic... the code becomes a problem where load balancing had to come into play. I've been using xdebug to find long running functions ...Can anyone give some pointers on fully optimizing your php code?
webosb is offline  
Reply With Quote
Old 11-14-2007, 04:36 PM   #2 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

You could try compressing the html using ob_start("gz_handler");
http://phplens.com/lens/php-book/opt...ugging-php.php

Another google article I found:
http://devilib.com/solutions-framewo...n-tips-faster/
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 11-14-2007, 05:53 PM   #3 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Reduce the number of SQL queries you execute on each page load. Every SQL query is a call to the SQL server which takes some time.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 11-14-2007, 05:57 PM   #4 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

Quote:
Originally Posted by Nor View Post
You could try compressing the html using ob_start("gz_handler");
http://phplens.com/lens/php-book/opt...ugging-php.php

Another google article I found:
http://devilib.com/solutions-framewo...n-tips-faster/
This actually puts more load on the server because it has to compress the HTML before sending it. Yes users can download the HTML quicker but the server will take longer to actually send it. If your server can handle it then your users will like it if you compress the HTML before sending it. But if it is taking too long to compress it, then it isn't worth it.
__________________
Eric
wGEric is offline  
Reply With Quote
Old 11-14-2007, 06:38 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by wGEric View Post
Reduce the number of SQL queries you execute on each page load. Every SQL query is a call to the SQL server which takes some time.
Just to elaborate on this valid point. It's better to have 10 small and quick queries instead of 1 big and slow query. Also, in your SQL queries, use the EXPLAIN command before SELECT and then analyse the results. You'll want to stay well away from ALL queries as this means that MySQL has had to scan the entire table (or more) to find what it's looking for. See this page in MySQL's documentation for further information.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-15-2007, 01:11 AM   #6 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

One of the first steps is to optimize your database. Check the schema, add index's when needed, check your queries, etc. Also choosing the correct table type can help a lot. Watch out for locked tables / rows. Or COUNT(*) on IonnoDB tables... They can kill apps.

Another one is to make sure you're caching data you need to cache. Example would be, besides HTML pages, if you generate thumbnails or other graphics with PHP, to cache and save those (honestly, when does a thumbnail change all by itself?)

Also, look at how your system/core php files are designed.
Are you using multiple require / include statements that aren't needed? Are your include files excessively large? Could they be split into smaller more specific tools / packages ?
I mention this because everytime you need to require a file, it server must then read disk. Which doesnt seem so bad... but increase traffic and average hit per page... I've seen some sites/pages have up to 15 - 25 includes get bombed.

Just little tid bits to look out for...

Edit:
Oh, and also, the point about the file size of PHP scripts... PHP is an interpreted language, which means if you can get it done in less code, get it done. So having excessive unneeded code for PHP to compile and process slows down the page.

Last edited by dschreck : 11-15-2007 at 01:13 AM. Reason: Added additional explaination
dschreck is offline  
Reply With Quote
Old 11-15-2007, 02:16 PM   #7 (permalink)
The Wanderer
 
Join Date: Oct 2007
Posts: 12
Thanks: 0
Webitz is on a distinguished road
Default

What if the server you host your website on was dedicated, for example I have a dedicated 1and1 server, runs my one website, PHP Data driven.
(Win2k3, IIS, before you laugh theres more reason behind choosing that toolset).
What sort of things can I do on the Server side of things to make PHP and MySQL database queries run faster?
Webitz is offline  
Reply With Quote
Old 11-15-2007, 02:27 PM   #8 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Quote:
Originally Posted by Webitz View Post
What if the server you host your website on was dedicated, for example I have a dedicated 1and1 server, runs my one website, PHP Data driven.
(Win2k3, IIS, before you laugh theres more reason behind choosing that toolset).
What sort of things can I do on the Server side of things to make PHP and MySQL database queries run faster?
Change to LAMP :D? thats Linux Apache Mysql PHP for those not so geeky :P
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-15-2007, 02:30 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How would LAMP (or more specifically WAMP) help him?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-15-2007, 02:30 PM   #10 (permalink)
The Wanderer
 
Join Date: Oct 2007
Posts: 12
Thanks: 0
Webitz is on a distinguished road
Default

Aye but like I said, I have reason for going Microsoft. I will enventually be setting up Virtual Linux server so I can have a LAMP service - but does that actually Optimize your PHP? Or just work really well with it?
Webitz is offline  
Reply With Quote
Old 11-15-2007, 02:37 PM   #11 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

Lol LAMP was a bit of a joke, but PHP does run more stable and quicker on LAMP generally speaking, but thats another point.

Try some code optimisers:
http://eaccelerator.net/
http://www.zend.com/products/zend_optimizer
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 11-15-2007, 02:39 PM   #12 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default

I am using lamp... running on SuSE w/ Apache2, MySQL, PHP5 on a box with 4xCPUs and 2GB of RAM... I'm talking about optimizing code. Problem looks like it might be with this MIME email class that I'm using... there is a function that takes a LONG time to execute. I use apache benchmark to stress test the box while running a profiler (xdebug) to see where the source of the problem might be...
webosb is offline  
Reply With Quote
Old 11-15-2007, 06:20 PM   #13 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

Quote:
Originally Posted by Webitz View Post
...

What sort of things can I do on the Server side of things to make PHP and
MySQL database queries run faster?
Well first thing to do is start by using the EXPLAIN SELECT syntax, start checking out what the hell is going on. If you're running a JOIN syntax, you want to avoid the 'ALL' type, your goal should be to get eq_ref. Also beware of the 'INDEX' type, which preforms the same as ALL, but is a little faster.

You also want to look at the number or writes you have to do the database, and consider if your I/O volume does require you to replicate, or cluster. Obviously at some point you reach the limits of your DB hardware limitations. And when that happens, bad things happen.

I'd also look into the schema of data, if you find that you're running a lot of complicated searches or something of the like, you should cache the search queries.
I worked before on a site, where when one person searched for a term, that was cached, everyday the cache starts over. So if 'Bob' searches for 'panda images', he finds what he wants, and the server caches those results. Then, if 'Jill' searches for 'panda images' as well, she gets the same results, just without any extra load on the server.
There's an infinite number of possibilities of ways to cache data. I'd recommend checking into a way that is most adaptable to your site or projections needs.
dschreck is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design