get_results( $wpdb->prepare( 'SELECT table_name AS name, COALESCE(data_length, 0) + COALESCE(index_length, 0) AS "size", COALESCE(table_rows, 0) AS "rows" FROM information_schema.TABLES WHERE table_schema = %s AND table_name LIKE %s ORDER BY size DESC', DB_NAME, $wpdb->esc_like( $wpdb->prefix ) . '%' ) ); $total_size = 0; $total_rows = 0; $table_count = 0; $largest = array(); // If the query returned results, calculate sizes. if ( ! empty( $tables ) ) { foreach ( $tables as $table ) { $total_size += (int) $table->size; $total_rows += (int) $table->rows; ++$table_count; if ( count( $largest ) < 5 ) { $largest[] = array( 'name' => $table->name, 'size' => static::format_bytes( (int) $table->size ), 'rows' => static::format_number( (int) $table->rows ), ); } } } // Fallback: If size is still 0, try alternative method. if ( 0 === $total_size && $table_count > 0 ) { $size_result = $wpdb->get_var( $wpdb->prepare( 'SELECT SUM(COALESCE(data_length, 0) + COALESCE(index_length, 0)) FROM information_schema.TABLES WHERE table_schema = %s', DB_NAME ) ); $total_size = (int) $size_result; } // Second fallback: Count WordPress tables only (with prefix filter). // This handles edge cases where the first query returned no results. if ( 0 === $table_count ) { // Try again with explicit prefix filter - the first query may have failed. $prefix_pattern = $wpdb->esc_like( $wpdb->prefix ) . '%'; $all_tables = $wpdb->get_results( $wpdb->prepare( 'SELECT table_name AS name, COALESCE(data_length, 0) + COALESCE(index_length, 0) AS size, COALESCE(table_rows, 0) AS rows FROM information_schema.TABLES WHERE table_schema = %s AND table_name LIKE %s ORDER BY size DESC', DB_NAME, $prefix_pattern ) ); if ( ! empty( $all_tables ) ) { foreach ( $all_tables as $table ) { $total_size += (int) $table->size; $total_rows += (int) $table->rows; ++$table_count; if ( count( $largest ) < 5 ) { $largest[] = array( 'name' => $table->name, 'size' => static::format_bytes( $table->size ), 'rows' => static::format_number( $table->rows ), ); } } } } $size_mb = $total_size / 1024 / 1024; $details = array( 'total_size' => static::format_bytes( $total_size ), 'size_mb' => round( $size_mb, 2 ), 'total_rows' => static::format_number( $total_rows ), 'table_count' => $table_count, 'largest_tables' => $largest, ); if ( $size_mb > 1000 ) { return static::fail( 'medium', sprintf( /* translators: %s: database size */ __( 'Your database is %s, which is quite large. This will significantly increase migration time and may cause timeouts with some migration tools.', '0-day-analytics' ), static::format_bytes( $total_size ) ), __( 'Consider cleaning up old revisions, spam comments, and transients before migrating. Use a migration tool that handles large databases in chunks, or export/import via command line.', '0-day-analytics' ), $details ); } if ( $size_mb > 500 ) { return static::warning( sprintf( /* translators: %s: database size */ __( 'Your database is %s. This is manageable but may take longer to migrate. Consider cleaning up before migration.', '0-day-analytics' ), static::format_bytes( $total_size ) ), __( 'Clean up post revisions, spam, and expired transients to speed up migration. Tools like WP-Optimize can help.', '0-day-analytics' ), $details ); } if ( $size_mb > 100 ) { return static::info( sprintf( /* translators: %s: database size */ __( 'Your database is %s, which is a typical size for an established site. Standard migration tools should handle this without issues.', '0-day-analytics' ), static::format_bytes( $total_size ) ), $details ); } return static::pass( sprintf( /* translators: %s: database size */ __( 'Your database is %s, which is small and will migrate quickly with any standard migration method.', '0-day-analytics' ), static::format_bytes( $total_size ) ), $details ); } } }