Very slow wp-admin. Help diagnosing fault by displaying mysql queries and query times

I have noticed that my wp-admin is extremely slow. The front-end of the site is lightning fast.

e.g. if I visit https://domain.co.uk/wordpress/wp-admin/edit-comments.php, the page takes between 10-30 seconds to load.

Read More

I ran a script to see if I can diagnose the problem:

add_filter( 'admin_footer_text', 'performance', 20 );

if (is_admin())
{
    function performance( $visible = false ) {

    $stat = sprintf(  '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
        );

    echo $visible ? $stat : " {$stat} " ; 
}}

To find the following: 119 queries in 21.929 seconds, using 57.43MB memory on the edit-comments.php page. There are only 49 comments awaiting moderation so I cannot imagine that this is a problem with mysql reading the comments.

Is there a way of displaying individual mysql scripts and query times in that same footer of wp-admin so I can find out what’s wrong?

I have tried modifying the above code to the following, but that just overlays all queries over the entire page and is entirely unreadable:

function performance( $visible = false ) {

    global $wpdb;

    $stat = sprintf(  '%d queries in %.3f seconds, using %.2fMB memory',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
        );

    echo $visible ? $stat : "<!-- {$stat} -->" ;

    if (SAVEQUERIES) {
        foreach ($wpdb->queries as $query)
        {
            echo '<ul>';
                foreach ($query as $q)
                {
                    echo '<li>'. $q .'</li>';
                }
            echo '<ul>';
        }
    }
}

If the numerical values in $wpdb->queries are mysql query times, then the excessive php load time is caused by something in one of the PHP scripts.

Is there a way that I can do something similar but with each plugin to determine what the cause is?

Related posts

Leave a Reply