DevSnips.com Code Snippet Repository
search:    


Navigation
  Home
About
Library
Contact
 
Snippet Library
  ColdFusion   338  
  ASP   201  
  PHP   101  
  HTML   11  
  JavaScript   77  
  XML   2  
  CSS   5  
  SQL   13  
  JSP   2  
  C#   1  
  ASP.NET   0  
  Submit a Code Snippet
 
Blog Archive
  September 2007
August 2007
July 2007
June 2007
May 2007
November 2006
October 2006
Search Archives
 
Random Affiliates
  Tom Morris
Uno-Code
BioMetric Base
ReviewMe!

Want to become an affiliate?
Read more...


Privacy Policy
© 2010

Blog Archive

 
Optimizing MySQL with PHP

This code snippet deals with PHP and MySQL. MySQL is a very powerful database and is commonly used in PHP applications. One item that is rarely discussed is optimizing and repairing tables using a query. As you add and delete records, you add overhead to the tables. Basically, this is unused space that MySQL has allocated and cannot get rid of.

If you have a data table that has many adds and deletes, this overhead can become quite large over time. An example of this would be something that monitors stats or logs, etc. Recording Snort alert data is great example! Eventually, your overhead can go into several megabytes or even larger. As this overhead grows, it can impact the performance of the MySQL server.

You can repairs these tables quite easily. I like to handle my database maintenance in the administrative backends of my applications. On the script that handles my CRUD (CReate Update Delete), I like to add a optimizeTables function. This user-defined functions takes one argument, an array of table names.


function optimizeTable($inArray){
	global $connect
	if(is_array($inArray) && sizeof($inArray) > 0){
		$tableList		= "";
		foreach($inArray as $table){
			$tableList	.= "`".$table."`,";
		}
		$tableList		= rtrim($tableList,",");
		$SQL			= "OPTIMIZE TABLE ".$tableList;
		if(!mysql_query($SQL,$connect)){
			trigger_error(sprintf("SQL Error: %d: %sn" , mysql_errno(), mysql_error()), E_USER_ERROR);
		}
	}
}

It is simply called like this:


$tableArray			= array('session','tblOne','tblTwo');
optimizeTable($tableArray);
unset($tableArray);

You could optimize all the tables on a log out call, during the log in phase or cron a command line PHP script (PHP-CLI) that does this on a nightly basis.

Keeping your tables optimized is just another piece of the smooth running application puzzle.


Submitted on 06/09/07 at 11:44PM
Post Comment | Comments: 0
Bookmark to:
Add 'Optimizing MySQL with PHP' to Del.icio.us Add 'Optimizing MySQL with PHP' to digg Add 'Optimizing MySQL with PHP' to FURL Add 'Optimizing MySQL with PHP' to blinklist Add 'Optimizing MySQL with PHP' to reddit Add 'Optimizing MySQL with PHP' to Feed Me Links Add 'Optimizing MySQL with PHP' to Technorati Add 'Optimizing MySQL with PHP' to Yahoo My Web Add 'Optimizing MySQL with PHP' to Newsvine Add 'Optimizing MySQL with PHP' to Socializer Add 'Optimizing MySQL with PHP' to Ma.gnolia Add 'Optimizing MySQL with PHP' to Stumble Upon Add 'Optimizing MySQL with PHP' to Google Bookmarks Add 'Optimizing MySQL with PHP' to RawSugar Add 'Optimizing MySQL with PHP' to Squidoo Add 'Optimizing MySQL with PHP' to Spurl Add 'Optimizing MySQL with PHP' to BlinkBits Add 'Optimizing MySQL with PHP' to Netvouz Add 'Optimizing MySQL with PHP' to Rojo Add 'Optimizing MySQL with PHP' to Blogmarks Add 'Optimizing MySQL with PHP' to Shadows Add 'Optimizing MySQL with PHP' to Simpy Add 'Optimizing MySQL with PHP' to Co.mments Add 'Optimizing MySQL with PHP' to Scuttle

Go Back








Advertisements

GoToMeeting - Online Meetings Made Easy