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

J.D. Grimes jdg at codesymphony.co
Fri Jan 3 16:17:30 UTC 2014


On Jan 3, 2014, at 11:01 AM, Nikola Nikolov <nikolov.tmw at gmail.com> wrote:

> Do you/they really need all of the meta fields for all users? Since WP_User
> can load meta field values as properties of the user object( or by using
> $user->get( $meta_key ) ), I don't see why you would need to load all of
> the meta fields into the memory.
> 
> The problem here is because of the fact that they have lots of users(each
> one with their own meta information), creating an array of objects with all
> of this data just depletes the allocated memory for the PHP process.
> 
> So, while loading the meta values as properties of the object might end-up
> slower(since I believe, you have to make a separate request to get each
> field from the DB), it should in theory work.

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.

> Also, it might be a better alternative to create a script that would do
> what they are trying to do in incremental steps(maybe 100, or 500, or
> however many users work without exhausting the memory limit) instead of in
> one big chunk.

Yeah, you should pull out like 500 or so at a  time. You can use get_users() to do that, just use the number and offset parameters.

-J.D.




More information about the wp-hackers mailing list