• Resolved bt_dev

    (@biotrace)


    We are experiencing the same issue outlined in this thread – https://www.ads-software.com/support/topic/horrible-performance-of-users-php/

    The count_users() query is running on every admin page causing slowdown. Our query differs slightly than the one mentioned in the original post:

    SELECT COUNT(NULLIF(meta_value LIKE '****SNIPPED USER ROLES FOR SECURITY****), COUNT(NULLIF(meta_value = 'a:0:{}', false)), COUNT(*)
    FROM wp_usermeta
    INNER JOIN wp_users
    ON user_id = ID
    WHERE meta_key = 'wp_capabilities'

    ****SNIPPED USER ROLES FOR SECURITY****

    So far we have attempted to fix the issue by implementing the suggested fix from @missveronicatv below:

    Change line 337 in
    /plugins/ultimate-member/includes/core/class-query.php
    from
    $result = count_users();
    to
    $result = count_users( 'um_cache' );

    Install this code snippet to your active theme’s functions.php file
    or use the “Code Snippets” plugin.

    add_filter( 'pre_count_users', 'pre_count_users_by_um_caching', 10, 3 );
    
    function pre_count_users_by_um_caching( $null, $strategy, $site_id ) {
    
        if( $strategy != 'um_cache' ) return null;
    
        $result = array();
        $result['total_users'] = 0;
        $array = array( 'approved', 
                        'rejected', 
                        'awaiting_admin_review', 
                        'awaiting_email_confirmation', 
                        'inactive',
                     );
    
        foreach( $array as $status ) {
            $result['total_users'] += intval( UM()->query()->count_users_by_status( $status ));
        }
    
        return $result;
    }

    The slow query is still running on all admin pages however. The query is most likely very slow due to the fact we have a lot of different user roles with many users.

    Are there any updates on how to fix UM slow user queries?

Viewing 7 replies - 1 through 7 (of 7 total)
  • missveronica

    (@missveronicatv)

    @biotrace

    There is no update about this issue.
    This is my latest bug report:

    https://github.com/ultimatemember/ultimatemember/issues/1271

    Thread Starter bt_dev

    (@biotrace)

    Thanks for the update @missveronicatv

    I’m wondering if there is a temporary workaround to disable the specific slow query entirely as it shouldn’t be required on all admin pages.

    Hopefully we have a fix for this soon.

    missveronica

    (@missveronicatv)

    @biotrace

    You can try this code snippet, which will replace all UM and WP count_users
    by using the UM status counts available.

    add_filter( 'pre_count_users', 'um_count_users_with_performance', 10, 3 );
    
    function um_count_users_with_performance( $null, $strategy, $site_id ) {
    
        $account_status = array( 'approved', 'rejected', 'awaiting_admin_review', 'awaiting_email_confirmation', 'inactive' );
        $result['total_users'] = 0;
    
        foreach( $account_status as $status ) {
            $count = UM()->query()->count_users_by_status( $status );
            $result['total_users'] = $result['total_users'] + $count;
        }
    
        return $result;
    }

    Install the code snippet into your active theme’s functions.php file
    or use the “Code Snippets” plugin.

    https://www.ads-software.com/plugins/code-snippets/

    Thread Starter bt_dev

    (@biotrace)

    Thank you, I’ll give the code snippet a try.

    missveronica

    (@missveronicatv)

    @biotrace

    Sorry,
    all the lists of roles are lost for WP if the WP calls are replaced.

    missveronica

    (@missveronicatv)

    @biotrace

    Try this code snippet which will only replace UM count_users.

    add_filter( 'pre_count_users', 'um_count_users_with_performance', 10, 3 );
    
    function um_count_users_with_performance( $result, $strategy, $site_id ) {
    
        $e = new \Exception;
        if ( strpos( $e->getTraceAsString(), '/plugins/ultimate-member/' )) {
            $account_status = array( 'approved', 'rejected', 'awaiting_admin_review', 'awaiting_email_confirmation', 'inactive' );
            $result['total_users'] = 0;
    
            foreach( $account_status as $status ) {
                $count = UM()->query()->count_users_by_status( $status );
                $result['total_users'] = $result['total_users'] + $count;
            }
        }
        return $result;
    }
    Thread Starter bt_dev

    (@biotrace)

    That worked great @missveronicatv

    The slow query is no longer initializing on every admin page. Thanks very much for your help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Slow count_users query on all admin pages’ is closed to new replies.