Make WordPress Core

Opened 2 years ago

Closed 23 months ago

Last modified 19 months ago

#55716 closed enhancement (fixed)

Prime users in cache in WP_Query

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.1 Priority: normal
Severity: normal Version: 1.5
Component: Query Keywords: has-patch has-unit-tests has-dev-note
Focuses: performance Cc:

Description

In WP_Query, when a post is setup using setup_postdata / generate_postdata methods, the function get_userdata is called. If there are posts that are by multiple authors, this can result in a query per user / author.

The function cache_users can be called and caches primed for all users in a single request.

Change History (13)

This ticket was mentioned in PR #2722 on WordPress/wordpress-develop by spacedmonkey.


2 years ago
#1

  • Keywords has-patch has-unit-tests added

#2 @spacedmonkey
2 years ago

  • Version set to 1.5

#3 @spacedmonkey
2 years ago

I have created a patch for this, including unit tests.

Author data for not need to be primed until we get to loop around posts using the the_post method. Add a new _prime_user_caches function to ensure that user meta is correctly primed.

#4 @spacedmonkey
2 years ago

  • Owner set to spacedmonkey
  • Status changed from new to assigned

#5 @spacedmonkey
23 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 53482:

Prime users cache in WP_Query and post REST API controller.

For a call to WP_Query or a post REST API request that contains posts from multiple authors, call the cache_users function, to ensure that all user data for post authors is primed in
a single database query. This results in far fewer database queries on multiple author sites.

Props spacedmonkey, timothyblynjacobs, peterwilsoncc.
Fixes #55716.

spacedmonkey commented on PR #2722:


23 months ago
#6

Committed

#7 @SergeyBiryukov
23 months ago

In 53483:

Query: Some documentation and test improvements for update_post_author_caches():

  • Make the descriptions for update_post_author_caches() and update_post_caches() more specific.
  • Move the unit test into its own file, for consistency with update_post_cache() tests. This also allows for using shared fixtures in case more tests are added in the future.

Follow-up to [53482].

See #55716.

#8 @desrosj
22 months ago

  • Milestone changed from Future Release to 6.1

#9 @spacedmonkey
22 months ago

Follow on ticket #56100

#10 @desrosj
19 months ago

  • Keywords needs-dev-note added

There are a lot of caching related changes in 6.1 and a dev note should be written collecting them all. Marking this for inclusion.

#11 @spacedmonkey
19 months ago

Made a start of dev notes here.

This ticket was mentioned in Slack in #core-restapi by spacedmonkey. View the logs.


19 months ago

Note: See TracTickets for help on using tickets.