Audit Database Table Size

Last updated 1 month ago

PHP
function my_plugin_check_db_table_sizes() {
    global $wpdb;

    // Query the database for table sizes
    $results = $wpdb->get_results( "SHOW TABLE STATUS", OBJECT );

    echo '<h2>Database Table Sizes:</h2><table><tr><th>Table Name</th><th>Size (MB)</th></tr>';
    
    foreach ( $results as $row ) {
        $size_in_mb = round( $row->Data_length / 1048576, 2 );
        echo '<tr><td>' . $row->Name . '</td><td>' . $size_in_mb . ' MB</td></tr>';
    }
    
    echo '</table>';
}
add_action( 'admin_footer', 'my_plugin_check_db_table_sizes' );

All code snippets are licensed GPLv2 or later unless otherwise stated.