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

J.D. Grimes jdg at codesymphony.co
Fri Jan 3 17:21:56 UTC 2014

Ah, OK, I didn’t look at that function. So it would be better to just set fields to ‘all’ then, as that returns WP_User objects too. I didn’t know that, I guess because actually it didn’t prior to 3.5. I’ve updated the codex: http://codex.wordpress.org/Function_Reference/get_users#Parameters


On Jan 3, 2014, at 11:45 AM, Otto <otto at ottodestruct.com> wrote:

> 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.
> -Otto
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list