• When loading users of a group the plugin executes one query for the IDs of users in this group and then two queries for each ID (SELECT * FROM wp_users, SELECT … from wp_usermeta).

    I understand that the wp_usermeta query probably comes from WordPress itself but why not load all users in a single query?

    We have about 6’000 users in a group which leads to 12’001 requests when loading a list of all users of that group. And we need to load all at once (pagination is not an option).

    https://www.ads-software.com/plugins/groups/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Kento

    (@proaktion)

    Hi,

    Can you please point to the specific code you refer to on our GitHub repository? https://github.com/itthinx/groups

    Thanks!

    Thread Starter tricki

    (@tricki1)

    Here you load the IDs of group members:

    https://github.com/itthinx/groups/blob/master/lib/core/class-groups-group.php#L140

    And in the Groups_User constructor you fire off a query to get the full user:

    https://github.com/itthinx/groups/blob/master/lib/core/class-groups-user.php#L108

    WP_User accepts an object in the constructor which doesn’t require a separate query for each user.

    Plugin Author Kento

    (@proaktion)

    Thanks, I see what you mean but as the users property returns an array of objects of type Groups_User we still need to initialize the user property of these and that’s why we have the call to get_user_by() where the WP_User object is created.

    Maybe it would help you if we had a user_ids property in Groups_Group which you could use to handle those users more efficiently? Or you could simply derive the query yourself meanwhile if you need a quicker solution.

    Thread Starter tricki

    (@tricki1)

    After further research I found another problem I’m having: When a WP_User object is initialized the capabilities are loaded. This in turn causes the WP meta cache to load and cache all meta data of each user, one by one.

    I created a pull request with two commits (I hope it’s in the correct branch..):

    1. Load all fields instead of just the ID and pass all data it to WP_User. This is what WP_User does internally as well.
    2. Add a method to pre-cache the user meta data in a single SQL statement

    This solves my problem and 50 seconds load time just became 1.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting users of group is inefficient’ is closed to new replies.