0

I tried to store an array containing about 50000 keys and respective values to the user meta using update_user_meta. Since, this was only a test, I ran a for loop to generate the array.

$test = array ();
for ($i = 0; $i <= 50000; $i++) {
    $test[$i] = $i;
}
update_user_meta($user_id, 'test', $test);

I ended up getting the error WordPress database error: [MySQL server has gone away]. Eventually, I was able to get it working with an array size of 48000 keys. Since update_user_meta converts arrays to serialized data before storing, is there a limit to the serialized data that user meta or post metas can handle? Or does this happen due to some other issue?

I tried the timeout fix suggested here. But I am not sure if it is supposed to work for WP 3.3.1. or whether if at all it is solution to my problem.

Please note I am using a local dev install using MAMP.

1
  • Currently, I have increased the max_allowed_packet size for MySQL to 5MB. This has given me the ability to store an array with 200K keys to user meta. However, Tom suggests an interesting solution of using custom user taxonomies. I need a solution that's scalable. I'd like to have opinion from others on which approach should be taken in this context. Saving an array as serialized data in user meta or custom user taxonomies?
    – John
    Commented Apr 17, 2012 at 22:53

1 Answer 1

3

According to the DB schemea, the meta_value column is of type longtext, and that has a maximum size of 4GB, however you're unlikely to reach that.

Eitherway storing large quantities of data in a single field is bad both from a practical point of view, a performance point of view, and data storage.

I advise instead you use a custom taxonomy for this kind of data ( yes taxonomies aren't just for post objects )

10
  • Thx for the reply. The data I need to store to user meta would be done for each user registered on the site. In future the user base could climb to 100K users or more. Wouldn't saving the data as custom taxonomy for each user bloat the database?
    – John
    Commented Apr 17, 2012 at 19:44
  • No more than saving it as user meta, BUT, if you ever needed t do queries on that data it would be much much easier to do it using the taxonomy than to search through user meta
    – Tom J Nowell
    Commented Apr 17, 2012 at 20:51
  • After lot of deliberation I have finally understood the benefits of using user taxonomies for this need. I ended up creating a user taxonomy with the same name as the username of a User A. And all other users whose ID I earlier used to add to meta data of User A, I now add to the user taxonomy that was created as above. Please advise if this approach is correct? Secondly, I see wp_user_query class for querying users. Not sure how to make it work with user taxonomy. Could you please update your answer with an example on how to run queries to fetch users using user taxonomy?
    – John
    Commented Apr 19, 2012 at 16:57
  • A taxonomy is used to classify objects, or more specifically object IDs. Most people use this as in post IDs, and thus assume it means only post IDs. Please refer here: justintadlock.com/archives/2011/10/20/…
    – Tom J Nowell
    Commented Apr 19, 2012 at 17:19
  • There's also custom post types, where the term slug is the user login/user ID
    – Tom J Nowell
    Commented Apr 19, 2012 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.