Page 1 of 1

[Resolved] Optimise in PHP?

Posted: Tue May 02, 2006 6:03 pm
by Dale
Just a quickie... how would I optimise the database (just a certain table) in PHP ??

Also is it harmful to keep optimising it??

Posted: Tue May 02, 2006 6:05 pm
by Chris Corbyn
Database in PHP? You mean a MySQL table? Usually you'd add indexes conservatively on the columns fileds you access must frequently.

Posted: Tue May 02, 2006 6:08 pm
by Dale
Yeah, but how would I write a little bit of code that will allow me to optimise my MySQL database??

Posted: Tue May 02, 2006 6:19 pm
by Ollie Saunders
Just a quickie...
No it is not. Optimization is a large discipline. There are many reasons why code may run slowly and even more possible solutions to such problems. So to be of real help you will have to be more specific with your question and present us with the relevent code and database structure.
how would I optimise the database (just a certain table) in PHP ??
PHP is generally faster that any database so it does make sense to outsource database operations to PHP although the possibities for doing this are severely limited.
Also is it harmful to keep optimising it??
It is never harmful to optimize, unless of course you do it wrong. Its good to get into the habit of simple optimizations but even this will require knowledge of what executes quickly what does not. The larger the system, and the more performance you demand from it, the more complex the optimizations become. You can be assured that Google, Microsoft, and Yahoo have whole departments dedicated to optimization despite performance already being intrinsic to all their software design.

I believe the best, and quite possibly the easiest, form of optimization is optimization by design. That is, optimized code resulting from decisions made, with the benefit of experience, before you begin to write any code. Anything less will lie somewhere between a hack and redesign depending on your programming ability and strength of motivation.

Posted: Tue May 02, 2006 6:59 pm
by RobertGonzalez
A well laid out database design will save you tons of headaches and redesign time in the future. As far as using PHP to optimize your MySQL database (this is for MySQL only), I would download phpMyAdmin and see how they process index and key requests as part of their script.

Posted: Tue May 02, 2006 7:21 pm
by Ollie Saunders
This is relevent. Its about a fairly large project and how they found they could use PHP to optimise database performance. They don't go into any specifics unfortunately.

Posted: Tue May 02, 2006 7:43 pm
by Christopher
Maybe Dale means:

Code: Select all

function optimize_mysql_table($dsn, $db, $table) {
    mysql_connect($dsn);
    mysql_select_db($db);
    mysql_query("OPTIMIZE TABLE $table");
}

Posted: Tue May 02, 2006 9:11 pm
by Benjamin
About time someone answers the damn question. Everything doesn't have to be debated just for the sake of showing how big your smurf is. It's a pretty simple answer.

Code: Select all

OPTIMIZE TABLE `TableName`

Posted: Tue May 02, 2006 11:13 pm
by John Cartwright
agtlewis wrote:About time someone answers the damn question. Everything doesn't have to be debated just for the sake of showing how big your smurf is. It's a pretty simple answer.

Code: Select all

OPTIMIZE TABLE `TableName`
Thats not called for. :?

Posted: Tue May 02, 2006 11:34 pm
by Benjamin
Sorry, I feel like sometimes I lot of members are more focused on arguing about solutions rather than helping the poster.

Posted: Wed May 03, 2006 12:27 am
by RobertGonzalez
arborint wrote:Maybe Dale means:

Code: Select all

function optimize_mysql_table($dsn, $db, $table) {
    mysql_connect($dsn);
    mysql_select_db($db);
    mysql_query("OPTIMIZE TABLE $table");
}
Funny thing is, I never even considered this. This is the absolute best answer for the original poster.

Posted: Wed May 03, 2006 1:43 am
by Ollie Saunders
Funny thing is, I never even considered this. This is the absolute best answer for the original poster.
yeah, same here. :(

Posted: Wed May 03, 2006 1:49 am
by Dale
arborint wrote:Maybe Dale means:

Code: Select all

function optimize_mysql_table($dsn, $db, $table) {
    mysql_connect($dsn);
    mysql_select_db($db);
    mysql_query("OPTIMIZE TABLE $table");
}
Thank you very much :D That's what I was looking for :p

However thanks to everyone else too that has provided me with some new knowledge. :D Cheers!!