28, May 2012

Optimize all your tables automatically - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  Databases  >  MySQL
Topic: Optimize all your tables automatically
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Optimize all your tables automatically  (Read 12244 times)
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« on: Jan 30, 2006, 07:22:40 pm »

Here is a script (as promised) that will optimize all the tables in all your mysql databases.

The script is written in php, and it will optimize only the tables that actually have overhead.

To execute it, just replace the connection variables (h,u,p) with the ones you use to connect to your server.

Code:
<?php
echo '<pre>' "\n\n";
 
set_time_limit100 );
 
  
$time microtime();
  
$time explode(' '$time);
  
$time $time[1] + $time[0];
  
$start $time;
  
 
//Connection variables :
 
$h 'localhost';
 
$u 'root';
 
$p 'password';
 
 
$dummy_db 'mysql';//The php->mysql API needs to connect to a database even when executing scripts like this. If you got an error from this(permissions), just replace this with the name of your database

 
$db_link mysql_connect($h,$u,$p);
 
 
$res mysql_db_query($dummy_db'SHOW DATABASES'$db_link) or die('Could not connect: ' mysql_error());
 echo 
'Found 'mysql_num_rows$res ) . ' databases' "\n";
 
$dbs = array();
 while ( 
$rec mysql_fetch_array($res) )
 {
$dbs [] = $rec [0];
 }
 
 foreach ( 
$dbs as $db_name )
 {
echo "Database : $db_name \n\n";
$res mysql_db_query($dummy_db"SHOW TABLE STATUS FROM `" $db_name "`"$db_link) or die('Query : ' mysql_error());
$to_optimize = array();
while ( $rec mysql_fetch_array($res) )
{
if ( $rec['Data_free'] > )
{
$to_optimize [] = $rec['Name'];
echo $rec['Name'] . ' needs optimization' "\n";
}
}
if ( count $to_optimize ) > )
{
foreach ( $to_optimize as $tbl )
{
mysql_db_query($db_name"OPTIMIZE TABLE `" $tbl ."`"$db_link );
}
}
 }
 
 
$time microtime();
  
$time explode(' '$time);
  
$time $time[1] + $time[0];
  
$finish $time;
  
$total_time round(($finish $start), 6);
  echo 
'Parsed in ' $total_time ' secs' "\n\n";
?>


Enjoy Smiley
« Last Edit: Jan 30, 2006, 07:25:09 pm by Nikolas »

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Gender: Male
Posts: 56
374 credits
Members referred : 0



« Reply #1 on: Jan 30, 2006, 07:28:36 pm »

Thank you so much for sharing this.

I asked you about the OPTIMIZE (in the other thread), because I was planning to do this Smiley

Thanks again.
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=1367
Tags : php mysql databases Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  Databases  >  MySQL
Topic: Optimize all your tables automatically
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 06:16:39 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 62.845
Total Topics: 11.031
Total Members: 21.453
Tutorials : 58
Resources : 929
Designs : 395
Latest Member: rashmimaharjan

109 Guests, 2 Users online :

14 users online today:




Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.