Opened 9 months ago
Last modified 7 weeks ago
#63021 reopened enhancement
Lazy load user meta
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | 4.4 |
| Component: | Users | Keywords: | has-patch has-unit-tests needs-testing needs-dev-note dev-feedback |
| Focuses: | performance | Cc: |
Description
Break out ticket from #58001.
WordPress already supports lazy loading for blog, comment, and term meta, significantly improving performance by reducing unnecessary database queries. However, user meta is still loaded immediately, which can lead to inefficiencies, especially on sites with large user bases or extensive user meta data.
Proposal:
This ticket proposes introducing a new wp_lazyload_user_meta() function to facilitate lazy loading of user meta. This function will be used within:
- cache_users() to defer loading user meta until it’s explicitly requested.
- The WP_User_Query class to optimise user queries and prevent unnecessary meta data retrieval.
Benefits:
- Reduces the number of unnecessary database queries.
- Improves performance, particularly for sites with many users.
- Aligns user meta handling with existing lazy-loaded metadata (blogs, comments, terms).
- Enhances consistency and efficiency across WordPress meta data handling.
Implementation Considerations:
- wp_lazyload_user_meta( array $user_ids ) will preload user meta into cache but defer its retrieval until explicitly accessed.
- Ensure backward compatibility by preserving expected behaviour for get_user_meta().
- Modify cache_users() and WP_User_Query to use wp_lazyload_user_meta() instead of immediately fetching meta.
- Evaluate and address potential edge cases, such as bulk user queries and caching mechanisms.
Attachments (2)
Change History (28)
This ticket was mentioned in PR #8411 on WordPress/wordpress-develop by @spacedmonkey.
9 months ago
#1
- Keywords has-patch has-unit-tests added
This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.
9 months ago
This ticket was mentioned in Slack in #core-performance by flixos90. View the logs.
9 months ago
#4
@
9 months ago
- Milestone changed from Awaiting Review to 6.8
- Owner set to flixos90
- Status changed from new to assigned
Assigning to @flixos90 for review.
#5
@
9 months ago
- Milestone changed from 6.8 to 6.9
Similar to the related #58001, this needs to be punted to the 6.9 milestone. I don't think it requires early though as there are fewer risks with this change.
This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.
8 months ago
This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.
7 months ago
#8
@
4 months ago
Introduce wp_lazyload_user_meta() and update cache_users() and WP_User_Query to defer loading user meta until needed, improving performance on large sites.
This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.
3 months ago
#10
@
3 months ago
Question: How often are users queried in core without also getting the meta? For example, things as basic to a user such as display_name are stored not in wp_users but wp_usermeta. If most every query will need wp_usermeta, then adding lazy-loading will end up causing more queries not fewer, right? If so, then should this lazy-loading be opt-in and disabled by default?
This ticket was mentioned in Slack in #core by welcher. View the logs.
3 months ago
This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.
3 months ago
#17
@
3 months ago
@westonruter I did some testing on some of block and classic themes. As you can see, if in 2019, I am seeing the user meta loaded if you have an author block in content. Once this block is removed, I do not see the author meta loading.
In short, if you do render display name or other data that is loaded from user meta, the lazy loading is working, otherwise it is not loaded.
This change, breaks nothing but it only of benefit if you do not load user capabilties or display name.
@westonruter commented on PR #8411:
2 months ago
#19
@spacedmonkey I'm trying to follow your testing instructions, but I'm having trouble.
- I've activated the Twenty Nineteen theme.
- I've created two pages: One without an Author block and another with an Author block.
- I only have Query Monitor active.
- I'm viewing Page template.
# Trunk
Without Author Block | With the Author Block
# PR
Without Author Block | With the Author Block
👉🏻 In all cases, I see the same count of 21 queries. What am I missing?
@spacedmonkey commented on PR #8411:
2 months ago
#20
@westonruter Sorry for the confused but to see the benefit, this must first be committed.
Now it is committed and this PR rebased, I am seeing the benefit.
Trunk | PR
73 queries |
72 queries
67 queries|
66 queries
I am seeing one less query on a number of different types of page load.
The query I am no longer seeing this one.
This means 28 less rows of user meta loaded. For sites with lots of user meta or multisite, this could be a massive win.
@spacedmonkey commented on PR #8411:
7 weeks ago
#23
Committed in [60989]
Introduce the
wp_lazyload_user_metafunction to queue user metadata for lazy-loading, optimizing user meta retrieval. Update related functions, tests, and metadata lazyloader to support this functionality, ensuring improved performance and consistency.Trac ticket: https://core.trac.wordpress.org/ticket/63021