Opened 4 weeks ago
Closed 3 weeks ago
#66291 closed enhancement (fixed)
Add pre_wp_count_attachments filter for the attachment counts query
| Reported by: | jdeep | Owned by: | westonruter |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | Posts, Post Types | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | performance |
Description
Similar to #66098: wp_count_attachments() offers no way to bypass its GROUP BY post_mime_type aggregate and serve the counts from another source. On a site with a large attachment count, every request that calls it pays for that query.
On every cache miss, wp_count_attachments() runs two queries over wp_posts. The first is a GROUP BY post_mime_type over all non-trashed attachments. The second is a separate COUNT(*) for trashed attachments. The result is cached in the object cache under a key built inline in the function (attachments, or attachments:<mime_type> when MIME types are passed). That key lives in the counts group, which is registered as non-persistent (see: #35430). The counts never survive past the current request, so every request that calls wp_count_attachments() re-runs both queries. On a site with millions of attachments, the first query alone takes ~8.7 seconds (see below).
With a pre_wp_count_attachments filter, wp_count_attachments() could serve the counts from a pre-computed store that is refreshed periodically by a background job, and skip both queries.
Benchmarks
Environment: MySQL 8.x, InnoDB buffer pool 8 GB.
Data: wp_posts ~4M rows (~8.3 GB data, ~1.2 GB index), ~2.8M attachments.
EXPLAIN ANALYZE of the GROUP BY post_mime_type query in wp_count_attachments(), with core's default indexes:
-> Table scan on <temporary> (actual time=8694..8694 rows=12 loops=1)
-> Aggregate using temporary table (actual time=8694..8694 rows=12 loops=1)
-> Filter: ((wp_posts.post_type = 'attachment') and (wp_posts.post_status <> 'trash')) (cost=657781 rows=2.02e+6) (actual time=0.113..7163 rows=2.86e+6 loops=1)
-> Table scan on wp_posts (cost=657781 rows=4.05e+6) (actual time=0.109..6414 rows=3.97e+6 loops=1)
The query scans all ~4M rows of wp_posts to return 12 rows, taking ~8.7 seconds.
Change History (5)
This ticket was mentioned in PR #14210 on WordPress/wordpress-develop by @jdeep.
4 weeks ago
#1
- Keywords has-patch has-unit-tests added
@jdeep commented on PR #14210:
3 weeks ago
#2
Hi @westonruter, Thanks for the review. All the requested changes have been resolved 👍🏻
@westonruter commented on PR #14210:
3 weeks ago
#4
Drafted SVN commit message:
Posts, Post Types: Add `pre_wp_count_attachments` filter. Introduces the `pre_wp_count_attachments` filter to `wp_count_attachments()`, the counterpart of `pre_wp_count_posts`. Returning a `stdClass` object short-circuits the function, skipping the object cache lookup and both of its queries. Since the `counts` cache group is non-persistent, those queries otherwise run on every request that needs the counts, and on a site with millions of attachments the `GROUP BY post_mime_type` aggregate alone can take several seconds. The filter allows the counts to be served instead from a value pre-computed in the background. Unlike post statuses, the set of MIME types is not known ahead of time, so only a missing trash count is filled in with zero. The filtered value should therefore contain a count for every MIME type matching `$mime_type`, and only those types. As with `pre_wp_count_posts`, the value is not cached and is passed through the `wp_count_attachments` filter. The `pre_wp_count_posts` filter introduced in r64245 is brought in line with the new one. A filtered value now short-circuits only when it is a `stdClass` object: any other non-null value was previously cast to an object, turning `false` into a nonsensical result, and filling in missing counts on another class can trigger a dynamic property deprecation. Both filters also clone the returned object, so that a callback which keeps it, such as in a static variable, does not have it modified by core or by later callbacks on the `wp_count_posts` and `wp_count_attachments` filters. Developed in https://github.com/WordPress/wordpress-develop/pull/14210. Follow-up to r64245. Props jdeep, westonruter. See #66098. Fixes #66291.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Adds a
pre_wp_count_attachmentsfilter towp_count_attachments(). Returning a non-null value short-circuits the function, skipping the object cache lookup and both count queries (GROUP BY post_mime_typeand the trashCOUNT(*)), so sites with large attachment tables can serve counts from a precomputed source.wp_count_attachments, and a missingtrashcount is set to 0. It is left to the filter callback to return counts for all MIME types present on the site.Use of AI Tools
Tool(s): Claude Code
Model(s): Opus 5.5
Used for: Drafted the patch and unit tests. I reviewed the implementation and the code changes and ran the new tests.