Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 PHP Code: Optimize All Database Tables (mySQL) Jump to:  
Category: >> PHP Code >> Optimize All Database Tables (mySQL) Bookmark and Share

<< lastnext >>

Snippet Name: Optimize All Database Tables (mySQL)

Description: Short but useful script that optimizes all tables in a mySQL database.

Comment: (none)

Language: PHP, MYSQL
Highlight Mode: PHP
Last Modified: February 26th, 2009

// Define mySQL variables
// Connect to mySQL
MYSQL_CONNECT($hostname, $username, $password);
// Statement to select the databases
$db_select = 'SHOW DATABASES';
// Query mySQL for the results
$db_result = MYSQL_QUERY($db_select);
     // Loop through all the databases
     WHILE ($db_row = MYSQL_FETCH_ARRAY($db_result)) {
          // Select currently looped database and continue only if successful
          IF (MYSQL_SELECT_DB($db_row[0])) {
               // Echo database name
               ECHO "<br><b>";
               ECHO $db_row[0];
               ECHO "</b><br>";
               // Statement to select the tables in the currently looped database
               $tbl_status = 'SHOW TABLE STATUS FROM ' . $db_row[0];
               // Query mySQL for the results
               $tbl_result = MYSQL_QUERY($tbl_status);
                    // Check to see if any tables exist within database
                    IF(MYSQL_NUM_ROWS($tbl_result)) {
                         // Loop through all the tables
                         WHILE ($tbl_row = MYSQL_FETCH_ARRAY($tbl_result)) {
                              // Statement to optimize table
                              $opt_table = 'OPTIMIZE TABLE ' . $tbl_row[0];
                              // Query mySQL to optimize currently looped table
                             $opt_result = MYSQL_QUERY($opt_table);
                              // Echo table name
                              ECHO "  <i>";
                              ECHO $tbl_row[0];
                              ECHO "</i><br>";
                         } // End table while loop 
                    } ELSE {
                         // Alert that there are no tables within database
                              ECHO "  <i>No Tables</i><br>";
                    } // End table exists if statement
          } // End database if statement
     } // End database while loop
// Alert that operation was successful
ECHO "<br><br><b>Above tables successfully optimized.</b>";

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 25 users online    © 2009 psoug.org

Forgot your password?