Opened 10 years ago
Last modified 4 weeks ago
#37702 new enhancement
Accept array of IDs in `delete_metadata_by_mid`
| Reported by: | peterwilsoncc | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Options, Meta APIs | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | performance |
Description
When deleting meta data in bulk (for example when an object is deleted), improve performance of meta deletion by accepting an array for delete_metadata_by_mid and related functions.
Related #37671.
Change History (4)
#3
@
5 weeks ago
I'd like to work on this.
One question on approach before I start: since this ticket was opened, #59269 has gone with a dedicated bulk_add_metadata() function rather than extending add_metadata() to accept arrays (see comment:6:ticket:59269 on avoiding overloaded signatures). For consistency I'd propose the same here: a new bulk_delete_metadata_by_mid( $meta_type, $meta_ids ) that fires the existing per-item actions and runs a single DELETE ... WHERE meta_id IN (...), with wp_delete_post() and wp_delete_comment() switched over to use it. The existing delete_metadata_by_mid() signature stays unchanged.
Happy to go the other way if extending the existing function is preferred.
This ticket was mentioned in PR #14408 on WordPress/wordpress-develop by @faisalahammad.
4 weeks ago
#4
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
What and why
When a post, comment, term, site or user is deleted, its meta is removed by calling delete_metadata_by_mid() once per row. Each call runs a SELECT and a DELETE, so an object with 200 meta rows costs 400 queries. This PR adds bulk_delete_metadata_by_mid( $meta_type, $meta_ids ), which does the same work with one SELECT and one DELETE no matter how many rows there are.
This follows the approach brianboy suggested in comment 3 on the ticket. It also follows the naming of bulk_add_metadata() from Trac ticket 59269, so delete_metadata_by_mid() keeps its signature.
Scope
In scope:
- New
bulk_delete_metadata_by_mid()inwp-includes/meta.php. - The per-row loops in
wp_delete_post(),wp_delete_attachment(),wp_delete_comment(),wp_delete_term(),wp_delete_site(),wp_delete_user()andwpmu_delete_user()now call the new function. - Unit tests.
Out of scope:
delete_metadata_by_mid()is unchanged, including its signature and hooks.- Other callers of
delete_metadata_by_mid(), such as the admindelete_meta()and XML-RPC, delete one row at a time and are left as they are.
Back compat notes
- The
delete_{$meta_type}_metadata_by_midfilter still runs once per ID. - These actions still fire once per row with the same arguments:
delete_{$meta_type}_metaanddeleted_{$meta_type}_meta- the old-style
delete_postmeta/deleted_postmetaanddelete_commentmeta/deleted_commentmeta
- The order changes. All
delete_*actions fire first, then the single DELETE runs, then alldeleted_*actions fire. Before, each row was deleted between its own pair of actions. Adeleted_post_metacallback that reads other meta on the same post will now find it already gone. - The rows are read with one direct query, so the
get_{$meta_type}_metadata_by_midfilter no longer runs on these paths. Before, each row went throughget_metadata_by_mid(). If that filter should keep running here, I can apply it to each row after the SELECT. Feedback welcome.
Testing
npm run env:start npm run env:install npm run test:php -- --filter Tests_Meta_BulkDeleteMetadataByMid npm run test:php -- --group meta,post,comment,taxonomy,user,attachment npm run test:php -- -c tests/phpunit/multisite.xml --group ms-site,ms-user,user,meta
All should pass. test_should_use_one_select_and_one_delete_query asserts that deleting 5 rows takes exactly 2 queries.
Manual check: add several meta entries to a post, delete the post permanently, and confirm in Query Monitor that wp_postmeta gets one SELECT and one DELETE instead of two queries per row.
Locally: 17 new tests pass. Single site groups above ran 4516 tests with 0 failures, and multisite groups ran 2381 tests with 0 failures. PHPCS shows no errors.
Related: #34848