TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Optimizing PHP Code (http://www.talkphp.com/general/1458-optimizing-php-code.html)

webosb 11-14-2007 03:53 PM

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?

Nor 11-14-2007 04:36 PM

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/

wGEric 11-14-2007 05:53 PM

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.

wGEric 11-14-2007 05:57 PM

Quote:

Originally Posted by Nor (Post 4104)
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.

Wildhoney 11-14-2007 06:38 PM

Quote:

Originally Posted by wGEric (Post 4109)
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.

dschreck 11-15-2007 01:11 AM

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.

Webitz 11-15-2007 02:16 PM

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?

bluesaga 11-15-2007 02:27 PM

Quote:

Originally Posted by Webitz (Post 4191)
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

Wildhoney 11-15-2007 02:30 PM

How would LAMP (or more specifically WAMP) help him?

Webitz 11-15-2007 02:30 PM

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?

bluesaga 11-15-2007 02:37 PM

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

webosb 11-15-2007 02:39 PM

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

dschreck 11-15-2007 06:20 PM

Quote:

Originally Posted by Webitz (Post 4191)
...

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.


All times are GMT. The time now is 05:28 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0