Getting MySQL table size with PHP


Posted in: mysql | Save to del.icio.us | Twit This! 4 Feb 2009

Below is a small script that lets you read the table sizes of a MySQL database.

<?php
 
$link = mysql_connect('host', 'username', 'password');
 
$db_name = "your database name here";
$tables = array();
 
mysql_select_db($db_name, $link);
$result = mysql_query("SHOW TABLE STATUS");
 
while($row = mysql_fetch_array($result)) {
    /* We return the size in Kilobytes */
    $total_size = ($row[ "Data_length" ] + 
                   $row[ "Index_length" ]) / 1024;
    $tables[$row['Name']] = sprintf("%.2f", $total_size);
}
 
print_r($tables);
 
?>


It will return data like the following for a ‘wordpress’ database:

Array
(
    [wp_comments] => 80.00
    [wp_links] => 48.00
    [wp_options] => 224.00
    [wp_pollsa] => 16.00
    [wp_pollsip] => 16.00
    [wp_pollsq] => 16.00
    [wp_postmeta] => 48.00
    [wp_posts] => 112.00
    [wp_term_relationships] => 32.00
    [wp_term_taxonomy] => 32.00
    [wp_terms] => 48.00
    [wp_usermeta] => 48.00
    [wp_users] => 48.00
)



Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



1 Response

1

Pablo

February 5th, 2009 at 5:29 am

Great script, simple and useful!

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 12 Users Online
    • 10 Guests, 2 Bots