Results 1 to 1 of 1

Thread: automatically optimize all the tables in your database

  1. #1
    Senior Member GiLL's Avatar
    Join Date
    Jun 2006
    Location
    india
    Posts
    363
    Thanks
    10
    Thanked 112 Times in 38 Posts
    Blog Entries
    2
    Rep Power
    0

    Thumbs up automatically optimize all the tables in your database

    Use this script preferably with a cronjob to automatically optimize all the tables in your database. Under root privilleges the script will search for all the databases in your server and will optimize them all.

    Just change the connection variables ($h, $u, $p) and execute it from your browser.


    PHP 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";
    ?>
    left wap stuff

  2. The Following User Says Thank You to GiLL For This Useful Post:

    ttxnam (26-04-12)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Backup cPanel automatically Script
    By synergyx2009 in forum Scripts Forum
    Replies: 4
    Last Post: 30-04-09, 08:27
  2. How To Optimize Gallery With Uploader
    By dhruva in forum Coding Forum
    Replies: 8
    Last Post: 10-04-09, 19:23
  3. Database And Tables
    By CyberNet69 in forum Site / Script testing and error fixing
    Replies: 3
    Last Post: 23-02-09, 05:41
  4. Reported Pm Automatically (lavalair)
    By maul in forum Site / Script testing and error fixing
    Replies: 3
    Last Post: 20-10-07, 16:57
  5. Nicks Get Deleted Automatically Help Plz
    By hero in forum Site / Script testing and error fixing
    Replies: 10
    Last Post: 23-05-07, 08:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

SEO by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19