[wp-hackers] get_users() exhausts memory with 'all_with_meta' argument

Otto otto at ottodestruct.com
Fri Jan 3 16:45:23 UTC 2014

On Fri, Jan 3, 2014 at 10:17 AM, J.D. Grimes <jdg at codesymphony.co> wrote:
> Actually, the meta fields are never retrieved until called for explicitly. All the ‘all_with_meta’ field does is return WP_User objects that give easy access for retrieving the meta if needed, rather than dumb objects like it does otherwise.

No, I think it does actually get all the data from the DB.

Looking at trunk, if you examine wp-includes/user.php, line 577-578,
you find this:

if ( 'all_with_meta' == $qv['fields'] ) {
   cache_users( $this->results );

The cache_users function is over in wp-includes/pluggable.php. It
first does a "SELECT * FROM $wpdb->users" for the relevant IDs, thus
getting those user fields into memory. However, it then goes on to
call the update_meta_cache() function with the id's of the users.

The update_meta_cache function (over in wp-includes/meta.php) will do
a SELECT of all the meta info from that user meta table for those IDs
and store them in the object cache.

So yes, it is loading all that meta data into memory (the object
cache) in advance. If you have a lot of users and are not using a
persistent object cache, this will eat up lots and lots of PHP memory.

Best to avoid using all_with_meta for large result sets, unless
wp_using_ext_object_cache() returns true.


More information about the wp-hackers mailing list