Saturday, February 14, 2009

Make Your MySQL Perform Fast




In this page I will share you a PHP script that finds all your tables, and then perform Optimize on every single one.This tips based on my experience that my page is having a MySQL slow query then I browse the internet and found the script that solved my page problem.Make sure you modify the four variables at the top of the script so they match your configuration,below is the script you needed to make your MySQL perform fast:

// Change these four variables to match your configuration.
// You will probably only need to change “user”, “pwd” and “dbName”.
$server = ”localhost”;
$user = ”mysql_user”;
$pwd = ”mysql_password”;
$dbName = ”mysql_dbName”;

$link = mysql_connect($server, $user, $pwd);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db($dbName, $link);
if (!$db_selected) {
die ('Can't use $dbName : ' . mysql_error());
}

// Find the names of all tables in the selected db.
$alltables = mysql_query("SHOW TABLES");

// Go through all tables.
while ($table = mysql_fetch_assoc($alltables))
{
foreach ($table as $db => $tablename)
{
// Perform the magic “OPTIMIZE” on every table in the db.
mysql_query("OPTIMIZE TABLE '".$tablename."'")
or die(mysql_error());
}

}

mysql_close($link);
?>

Click here to get your free mobile phone or apple ipod

No comments:

Post a Comment

Custom Search