Forum OVH  

Précédent   Forum OVH > Hébergements mutualisés > HOW-TO
S'inscrire FAQ Guides Recherche Messages du jour Marquer les forums comme lus

Réponse
 
Outils de la discussion
Vieux 19/03/2007, 18h23   #1
Abogil
Membre
 
Date d'inscription: octobre 2006
Messages: 3 798
Envoyer un message via Skype™ à Abogil
Script de mesure de la taille d'une base de données

Bonjour à toutes et à toutes,

J'ai réalisé un petit script, basé sur show table status, qui donne le total de la taille d'une base de données plus pour chaque table :
- La taille des Data,
- La taille de l'index,
- Le nombre de lignes,


Code PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Taille d'une base de données</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<?PHP

   
// Paramètres d'entrée à modifier :
   // --------------------------------

   
$DBhost  "mysql5_1";
   
$DBName  "xxxxx";
   
$DBowner "yyyyy";
   
$DBpw    "zzzzz";

   
// DOCUMENTATION
   // -------------

/*   

   // http://dev.mysql.com/doc/refman/5.0/en/show-table-status.html

   0  Name        The name of the table.
   1  Engine        The storage engine for the table. See Chapter 14, Storage Engines and Table Types.
   2  Version        The version number of the table's .frm file.
   3  Row_format    The row storage format (Fixed, Dynamic, Compressed, Redundant, Compact). Starting with MySQL/InnoDB 5.0.3, the format of InnoDB tables is reported as Redundant or Compact. Prior to 5.0.3, InnoDB tables are always in the Redundant format.
   4  Rows        The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40 to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.
            The Rows value is NULL for tables in the INFORMATION_SCHEMA database.
   5  Avg_row_length    The average row length.
   6  Data_length    The length of the data file.
   7  Max_data_length    The maximum length of the data file. This is the total number of bytes of data that can be stored in the table, given the data pointer size used.
   8  Index_length    The length of the index file.
   9  Data_free        The number of allocated but unused bytes.
   10 Auto_increment    The next AUTO_INCREMENT value.
   11 Create_time    When the table was created.
   12 Update_time    When the data file was last updated. For some storage engines, this value is NULL. For example, InnoDB stores multiple tables in its tablespace and the data file timestamp does not apply.
   13 Check_time    When the table was last checked. Not all storage engines update this time, in which case the value is always NULL.
   14 Collation        The table's character set and collation.
   15 Checksum        The live checksum value (if any).
   16 Create_options    Extra options used with CREATE TABLE.
   17 Comment        The comment used when creating the table (or information as to why MySQL could not access the table information).
*/


   // Ouverture de la base et accès aux tables
   // ----------------------------------------

   
$fh_db=mysql_connect($DBhost$DBowner$DBpw); 
   if ((
$fh_db)) 
      { 
      
// echo "DATABASE <b> >>$fh_db<< " . $DBName . "</b> Connexion OK <br> ";
      
$sel=mysql_select_db($DBName,$fh_db); 
      if (
$sel)   echo "DATABASE <b>" $DBName "</b>  Ouverture OK <br> ";
      else        echo 
"DATABASE <b>" $DBName "</b>  Ouverture <font color=red> impossible </font>  <br> "
      } 
   else
      {
      echo 
"DATABASE <b>" $DBName "</b>  Connexion <font color=red> impossible </font>  <br> "
      exit;
      }

   
$Aff_size         "";
   
$Total            0;
   
$Total_data       0;
   
$Total_index      0;
   
$Tab_tables       = array();
   
$result_count_row 0;
   
$count            0;

   
$sql_query "SHOW TABLE STATUS FROM $DBName";
   
$result_query mysql_query($sql_query); 

   if (!isset(
$result_query))  echo " <br> ####  " mysql_error() . " <br> ####  " "Requête : \$sql_query=$sql_query <br> <br> \n"
   else
      {
      while (
$result_query && $row=mysql_fetch_row($result_query)) 
         {
         
$count $count 1;
         
$Table_liste $row[0];  // Name
         
$row_1       $row[1];  // Engine
         
$row_2       $row[2];  // Version
         
$row_3       $row[3];  // Row_format
         
$row_4       $row[4];  // Rows
         
$row_5       $row[5];  // Avg_row_length

         
$Table $row ['0'];                                // Name
         
$Tab_tables ["$Table"]['Total_Rows'] = $row ['4'];  // Rows
         
$Tab_tables ["$Table"]['Table_Size'] = $row ['6'];  // Data_length
         
$Tab_tables ["$Table"]['Index_Size'] = $row ['8'];  // Index_length
         
$Total $Total $Tab_tables ["$Table"]['Table_Size'] + $Tab_tables ["$Table"]['Index_Size'];
         
$Total       $Total       $Tab_tables ["$Table"]['Table_Size'] + $Tab_tables ["$Table"]['Index_Size'];
         
$Total_data  $Total_data  $Tab_tables ["$Table"]['Table_Size'];
         
$Total_index $Total_index $Tab_tables ["$Table"]['Index_Size'];
         }
      } 


   
// Contenu de chaque table MySQL

 
   
$Tab_col = array();
   
reset ($Tab_tables);
   while(list(
$Table$data) = each($Tab_tables))
      {
      
$count 0;
      
$sql_query_col="SHOW COLUMNS FROM $Table"
      
$result_col=mysql_query($sql_query_col); 
      
$Tableau_colonnes = array();
      while (
$result_col && $row=mysql_fetch_array($result_col)) 
         { 
         
$count    $count 1;
         
$Nom_col  $row[0];
         
$Type_col $row[1];
         
$Tab_col ["$Table"]['Liste_col']["$Nom_col"]['Type_col']= $Type_col;
         }
      
$Tab_col ["$Table"]['Liste_col']['Nb_col']= $count;
      }

   
// Fermeture de la connexion
   
mysql_close($fh_db);                                     


   
// Affichage des tailles des tables MySQL

   
$Total_len strlen($Total);
   if (
$Total_len && $Total_len <= 6)   $Total substr($Total$Total_len ) . " " substr($Total$Total_len 3);
   if (
$Total_len && $Total_len <= 9)   $Total substr($Total$Total_len ) . " " substr($Total$Total_len 3) . " " substr($Total$Total_len 3);

   
$Aff_size .= "<center> \n";
   
$Aff_size .= "<table cellspacing=0 cellpadding=0 border=0 > \n";
   
$Aff_size .= "<tr><td colspan=4> </td></tr> \n";
   
$Aff_size .= "<tr><td colspan=4> <center> Base : <b> $DBName </b> Taille : <b> $Total </b> </center> </td></tr> \n";
   
$Aff_size .= "<tr><td colspan=4> </td></tr> \n";
   
$Aff_size .= "<tr> \n";
   
$Aff_size .= "<td> <b> Table        </b> </td> \n";
   
$Aff_size .= "<td> <b> Taille data  </b> </td> \n";
   
$Aff_size .= "<td> <b> Taille index </b> </td> \n";
   
$Aff_size .= "<td> <b> Lignes       </b> </td> \n";
   
$Aff_size .= "</tr> \n";
   
$Aff_size .= "<tr><td colspan=4> </td></tr> \n";


   
// Affichage des tables
   
reset ($Tab_tables);
   while(list(
$Table$data) = each($Tab_tables))
      {
      
$Table_Size $Tab_tables ["$Table"]['Table_Size'];
      
$Table_Size_len strlen($Table_Size);
      if (
$Table_Size_len && $Table_Size_len <= 6)   $Table_Size substr($Table_Size$Table_Size_len ) . " " substr($Table_Size$Table_Size_len 3);
      if (
$Table_Size_len && $Table_Size_len <= 9)   $Table_Size substr($Table_Size$Table_Size_len ) . " " substr($Table_Size$Table_Size_len 3) . " " substr($Table_Size$Table_Size_len 3);

      
$Total_Rows $Tab_tables ["$Table"]['Total_Rows'];
      
$Total_Rows_len strlen($Total_Rows);
      if (
$Total_Rows_len && $Total_Rows_len <= 6)   $Total_Rows substr($Total_Rows$Total_Rows_len ) . " " substr($Total_Rows$Total_Rows_len 3);
      if (
$Total_Rows_len && $Total_Rows_len <= 9)   $Total_Rows substr($Total_Rows$Total_Rows_len ) . " " substr($Total_Rows$Total_Rows_len 3) . " " substr($Total_Rows$Total_Rows_len 3);

      
$Index_Size $Tab_tables ["$Table"]['Index_Size'];
      
$Index_Size_len strlen($Index_Size);
      if (
$Index_Size_len && $Index_Size_len <= 6)   $Index_Size substr($Index_Size$Index_Size_len ) . " " substr($Index_Size$Index_Size_len 3);
      if (
$Index_Size_len && $Index_Size_len <= 9)   $Index_Size substr($Index_Size$Index_Size_len ) . " " substr($Index_Size$Index_Size_len 3) . " " substr($Index_Size$Index_Size_len 3);

      
$Aff_size .= "<tr> \n";
      
$Aff_size .= "<td align=left>   " $Table .      "</td> \n";
      
$Aff_size .= "<td align=right>  " $Table_Size "</td> \n";
      
$Aff_size .= "<td align=right>  " $Index_Size "</td> \n";
      
$Aff_size .= "<td align=right>  " $Total_Rows "</td> \n";
      
$Aff_size .= "</tr> \n";
      }

   
$Aff_size .= "<tr><td colspan=4> </td></tr> \n";

   
$Total_data_len strlen($Total_data);
   if (
$Total_data_len && $Total_data_len <= 6)   $Total_data substr($Total_data$Total_data_len ) . " " substr($Total_data$Total_data_len 3);
   if (
$Total_data_len && $Total_data_len <= 9)   $Total_data substr($Total_data$Total_data_len ) . " " substr($Total_data$Total_data_len 3) . " " substr($Total_data$Total_data_len 3);

   
$Total_index_len strlen($Total_index);
   if (
$Total_index_len && $Total_index_len <= 6)   $Total_index substr($Total_index$Total_index_len ) . " " substr($Total_index$Total_index_len 3);
   if (
$Total_index_len && $Total_index_len <= 9)   $Total_index substr($Total_index$Total_index_len ) . " " substr($Total_index$Total_index_len 3) . " " substr($Total_index$Total_index_len 3);

   
$Aff_size .= "<tr> \n";
   
$Aff_size .= "<td align=left>   <b>     Totaux :         </b> </td> \n";
   
$Aff_size .= "<td align=right>  <b> " $Total_data  " </b> </td> \n";
   
$Aff_size .= "<td align=right>  <b> " $Total_index " </b> </td> \n";
   
$Aff_size .= "<td align=left>                                 </td> \n";
   
$Aff_size .= "</tr> \n";

   
$Aff_size .= "</table> \n";
   
$Aff_size .= "</center> \n";

   
$Aff_size .= "<br> \n";


   
// Affichage des résultats

   
print $Aff_size;
?>
__________________
Cordialement.
Michel FOUILLADE
Camions occasion - Achat ventes - Annonces

Dernière modification par Abogil 20/03/2007 à 09h52
Abogil est déconnecté   Réponse avec citation
Vieux 19/03/2007, 19h55   #2
sadkun
Membre
 
Date d'inscription: septembre 2006
Messages: 884
Re : Script de mesure de la taille d'une base de données

Merci pour cette très bonne initiative
sadkun est déconnecté   Réponse avec citation
Vieux 19/03/2007, 20h31   #3
Abogil
Membre
 
Date d'inscription: octobre 2006
Messages: 3 798
Envoyer un message via Skype™ à Abogil
Re : Script de mesure de la taille d'une base de données

L'as-tu testé et comparé avec les résultats donnés avec le manager V3 ?

J'obtiens moins de 3% d'erreurs entre ma méthode et le manager V3.

Pour moi, je trouve :
- Manager V3 : 31.15 Mo
- Mon script : 30 427 572 Octets
__________________
Cordialement.
Michel FOUILLADE
Camions occasion - Achat ventes - Annonces
Abogil est déconnecté   Réponse avec citation
Vieux 23/03/2007, 14h40   #4
Tech-Jonathan
Membre
 
Date d'inscription: juin 2006
Messages: 1
Re : Script de mesure de la taille d'une base de données

Coucou,

Le script fonctionne bien sur une base mysql5 mais sur ma base mysql4 ca affiche des données éronnées.
__________________
Votre technicien ovh
Jonathan
Tech-Jonathan est déconnecté   Réponse avec citation
Réponse

Outils de la discussion

Règles de messages
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is oui
Les smileys sont activés : oui
La balise [IMG] est activée : non
Le code HTML peut être employé : non



Fuseau horaire GMT +2. Il est actuellement 17h41.


© OVH 1999-2010