Make WordPress Core

Opened 2 years ago

Last modified 4 weeks ago

#59269 assigned enhancement

Add support for adding metadata in bulk

Reported by: johnbillion's profile johnbillion Owned by: johnbillion's profile johnbillion
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Database Keywords: has-patch has-unit-tests changes-requested
Focuses: performance Cc:

Description

Related: #34848

This ticket aims to introduce functions for bulk inserting metadata in a manner that's more performant than calling the add_*_meta() functions multiple times per object. This is particularly beneficial during large imports or data generation where add_*_meta() is called many times for a large number of users, posts, comments, or terms.

This can be achieved by inserting all the metadata for the object in one INSERT query and only deleting the object's meta cache once. The existing filters and actions that are triggered when calling the add_*_meta() functions individually should still all be fired as expected, once for each meta key.

The performance benefit should grow as the number of rows to insert increases, because both the number of database queries and the number of cache deletions for the object should remain constant (O(1)) rather than increasing correspondingly to the number of rows (O(n)).

That said, there does appear to be a sweet spot after which the performance of one large INSERT query starts to degrade, although according to the PHPBench tests I've added it still remains overall more performant than multiple queries. Some more testing is needed across different payload sizes and database versions.

Change History (16)

This ticket was mentioned in PR #5128 on WordPress/wordpress-develop by @johnbillion.


2 years ago
#1

  • Keywords has-patch has-unit-tests added

#2 @swissspidy
2 years ago

#34848 already covers adding meta in bulk according to the patches, so what‘s the difference?

#3 @johnbillion
2 years ago

Yes the patches on that ticket mostly cover adding (and deleting) metadata in bulk but the ticket itself is about updating metadata in bulk. Adding is much more straight forward than updating, hence splitting it out into a separate ticket.

#4 @johnbillion
6 months ago

  • Milestone changed from Awaiting Review to 6.9

#5 @namith.jawahar
4 months ago

Wouldn't overloading add_*_meta() functions maintain cohesiveness with similar functions in core instead of introducing a new function.

Eg : get_*_meta()

#6 @johnbillion
4 months ago

Functions with multiple signatures is poor design, it creates ambiguity and type safety issues. It also makes it difficult to determine whether a function is going to behave properly without resorting to checking the version of WordPress is in use. With the new functions there is no ambiguity and developers can use function_exists( 'bulk_add_post_meta' ) to easily check that the function exists prior to using it.

#7 @namith.jawahar
4 months ago

I used this code in two custom importers and it works great. We do require bulk delete too when this is introduced as both functionality are often (always?) required together in real world projects.

It might be a good idea to do insert, delete and “update” (by insert for new and looped update function calls for actual updates) together instead of just bulk add for it to be really useful.

#8 @johnbillion
4 months ago

Thanks for testing @namithjawahar! Let's keep this ticket focused, lest it never makes it in like #34848 so far hasn't.

  • Bulk adding is straight forward
  • Bulk deletion is probably straight forward but please open a separate ticket for that
  • Bulk updating is being handled in #34848

#9 @namith.jawahar
4 months ago

That’s reasonable, I can create a ticket PR for delete if that’s not already in there. I really think the three functions needs to go in together in the same release cycle to be of max benefits to devs.

@TimothyBlynJacobs commented on PR #5128:


3 months ago
#10

Should we have a way to allow for adding multiple entries for the same meta key?

@johnbillion commented on PR #5128:


3 months ago
#11

Maybe? I haven't had a need for it myself. Could be useful, but it would complicate the function signature as it wouldn't be able to support an associative array of meta key => meta value elements.

This ticket was mentioned in Slack in #core by wildworks. View the logs.


6 weeks ago

@wildworks commented on PR #5128:


6 weeks ago
#13

This ticket was featured in today's 6.9 Bug Scrub. I'd be happy if you could check out @aaronjorbin's feedback.

This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.


5 weeks ago

#15 @westonruter
5 weeks ago

  • Keywords changes-requested added
  • Owner set to johnbillion
  • Status changed from new to assigned

#16 @johnbillion
4 weeks ago

  • Milestone changed from 6.9 to 7.0
Note: See TracTickets for help on using tickets.