[Resolved] Optimise in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

[Resolved] Optimise in PHP?

Post 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??
Last edited by Dale on Wed May 03, 2006 1:50 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Yeah, but how would I write a little bit of code that will allow me to optimise my MySQL database??
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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");
}
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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`
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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. :?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Sorry, I feel like sometimes I lot of members are more focused on arguing about solutions rather than helping the poster.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Funny thing is, I never even considered this. This is the absolute best answer for the original poster.
yeah, same here. :(
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

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