Database Optimisation Script in PHP


Well, sometimes, you probably need to optimise your MySQL tables. In order to avoid the hassle loggin to your remote SSH server, you probably want to write a script and put it somewhere accessible under HTTP protocol, e.g., http://www.yourdomain.com/somewheresafe/optimise.php

The php well supports mysql connections. It is very handy to have the following script ready.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
  require("conn.php");
  if (!$dbok) { die ('db not ok');}
 
  $alletabellen = mysql_query("SHOW TABLES");
 
  while($tabel = mysql_fetch_assoc($alletabellen))
  {
     foreach ($tabel as $db => $tabelnaam)
     {
        mysql_query("OPTIMIZE TABLE `".$tabelnaam."`", $link) or die(mysql_error());
        echo "<b>$tabelnaam</b> optimized.\n";
     }
  }
?>
<?php
  require("conn.php");
  if (!$dbok) { die ('db not ok');}

  $alletabellen = mysql_query("SHOW TABLES");

  while($tabel = mysql_fetch_assoc($alletabellen))
  {
     foreach ($tabel as $db => $tabelnaam)
     {
        mysql_query("OPTIMIZE TABLE `".$tabelnaam."`", $link) or die(mysql_error());
        echo "<b>$tabelnaam</b> optimized.\n";
     }
  }
?>

Inside conn.php, you will need to set up the connections to $link.

Doing this regularly will ensure a good performance of your website, especially if you have quite many database tables.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
192 words
Last Post: Codeforces: A. Super Agent
Next Post: Codeforces: B. Taxi

The Permanent URL is: Database Optimisation Script in PHP

Leave a Reply