Advertisement
ovizii

optimize_DBs

Dec 2nd, 2011
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.44 KB | None | 0 0
  1. <?php
  2. echo '<pre>' . "\n\n";
  3. set_time_limit(100);
  4.  
  5. $time = microtime();
  6. $time = explode(' ', $time);
  7. $time = $time[1] + $time[0];
  8. $start = $time;
  9.  
  10. //Connection variables :
  11. $h = 'localhost';
  12. $u = 'root';
  13. $p = '*****';
  14.  
  15. $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
  16.  
  17. $db_link = mysql_connect($h,$u,$p);
  18.  
  19. $res = mysql_db_query($dummy_db, 'SHOW DATABASES', $db_link) or die('Could not connect: ' . mysql_error());
  20. echo 'Found '. mysql_num_rows( $res ) . ' databases' . "\n";
  21. $dbs = array();
  22. while ( $rec = mysql_fetch_array($res) )
  23. {
  24. $dbs [] = $rec [0];
  25. }
  26.  
  27. foreach ( $dbs as $db_name )
  28. {
  29. echo "Database : $db_name \n\n";
  30. $res = mysql_db_query($dummy_db, "SHOW TABLE STATUS FROM `" . $db_name . "`", $db_link) or die('Query : ' . mysql_error());
  31. $to_optimize = array();
  32. while ( $rec = mysql_fetch_array($res) )
  33. {
  34. if ( $rec['Data_free'] > 0 )
  35. {
  36. $to_optimize [] = $rec['Name'];
  37. echo $rec['Name'] . ' needs optimization' . "\n";
  38. }
  39. }
  40. if ( count ( $to_optimize ) > 0 )
  41. {
  42. foreach ( $to_optimize as $tbl )
  43. {
  44. mysql_db_query($db_name, "OPTIMIZE TABLE `" . $tbl ."`", $db_link );
  45. }
  46. }
  47. }
  48.  
  49. $time = microtime();
  50. $time = explode(' ', $time);
  51. $time = $time[1] + $time[0];
  52. $finish = $time;
  53. $total_time = round(($finish - $start), 6);
  54. echo 'Parsed in ' . $total_time . ' secs' . "\n\n";
  55. ?>
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement