How to Display DB Queries Time Spent and Memory Consumption in WordPress

Home » Snippets » How to Display DB Queries Time Spent and Memory Consumption in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you curious about how much time and memory your WordPress site’s database queries are consuming? Monitoring the performance of your site’s database can be essential for optimizing and troubleshooting. In this article, we will explore how to display the time spent and memory consumption of database queries in WordPress. By tracking this information, you will be able to identify any bottlenecks or memory-intensive queries and take steps to improve the overall performance of your website. Let’s dive in and learn how to monitor your database performance in WordPress.

					function wpturbo_display_db_queries_info() {
    global $wpdb;
    
    $total_time = 0;
    $total_queries = 0;
    $total_memory = 0;
    
    foreach ( $wpdb->queries as $query ) {
        $total_time += floatval( $query[1] );
        $total_queries++;
    }
    
    $total_memory = memory_get_peak_usage( true );
    
    echo "<div class='wpturbo-db-queries-info'>";
    echo "<p>Total Queries: " . $total_queries . "</p>";
    echo "<p>Total Time Spent: " . $total_time . " seconds</p>";
    echo "<p>Total Memory Consumption: " . $total_memory . " bytes</p>";
    echo "</div>";
}
add_action( 'wp_footer', 'wpturbo_display_db_queries_info' );
				

The code snippet provided is a function called wpturbo_display_db_queries_info(), which is responsible for displaying information about the database queries made during the execution of a WordPress website.

To begin, we start by declaring the $wpdb global variable. This variable is an instance of the WordPress database class, which provides us with methods and properties to interact with the database.

Next, we initialize variables to keep track of the total time spent executing queries, the total number of queries made, and the total memory consumption.

Inside the foreach loop, we iterate through each query stored in the $wpdb->queries array. This array contains information about each executed database query, such as the query string and the time taken to execute it.

For each query, we extract the time taken ($query[1]) as a float value using floatval() and add it to the $total_time variable. We also increment the $total_queries variable to keep track of the number of queries made.

After the loop, we use the memory_get_peak_usage() function to get the peak memory usage during the execution of the current script. The true parameter ensures that the returned value is in bytes.

Finally, we display the gathered information within an HTML div element with a class name wpturbo-db-queries-info. Inside this element, we output the total number of queries, the total time spent in seconds, and the total memory consumption in bytes.

By hooking the wpturbo_display_db_queries_info() function to the wp_footer action, the information about database queries will be displayed at the bottom of the website’s front end.

You can customize the styling of the displayed information by adding your own CSS styles to the wpturbo-db-queries-info class.

Register an account to save your snippets or go Pro to get more features.