Database Size

Put this in a block, and it will show the database size in bytes and in megabytes. It calculates the database size by adding the table sizes and index sizes.
Restrict the block to the admin page or any other low traffic page.
The code was tested on 4.7.* and 5.5.
This is MySQL specific.

<?php 
function db_size_info($dbsize) {
$bytes = array('KB', 'KB', 'MB', 'GB', 'TB'); 
if (
$dbsize < 1024) $dbsize = 1;             
for (
$i = 0; $dbsize > 1024; $i++) $dbsize /= 1024;
$db_size_info['size'] = ceil($dbsize);
$db_size_info['type'] = $bytes[$i];
return
$db_size_info;
}
// Database size = table size + index size:
$rows = db_query("SHOW TABLE STATUS");
$dbssize = 0;
while (
$row = mysql_fetch_array($rows)) {
$dbssize += $row['Data_length'] + $row['Index_length'];
}
print
"$dbssize bytes<br />";
$dbssize = db_size_info($dbssize);
print
"or<br />";
print
"{$dbssize['size']} {$dbssize['type']}";
?>

Module locations

deekayen - August 1, 2008 - 14:08

If you don't like PHP code being eval()ed on your site from user input like this, I added this code to the DB Maintenance module in 5.x-dev as a block minus the bytes output. Similar information is also output by the Site Documentation module, but not in a block.

 
 

Drupal is a registered trademark of Dries Buytaert.