Make WordPress Core

Opened 5 weeks ago

Last modified 3 weeks ago

#62801 new enhancement

Improve performance of X-WP-TotalPages queries

Reported by: spacedmonkey's profile spacedmonkey Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.7
Component: REST API Keywords: good-first-bug has-patch reporter-feedback
Focuses: performance Cc:

Description

In the REST API, there is a header field called X-WP-TotalPages. This field is used to understand how many total pages there are to a REST API call.

This does a call to WP_Query / WP_Comment_Query / WP_User_Query and gets the total of number of results for a query. Here are some examples found in core.

These queries could be improved, by passing different arguments, to the query classes.

For WP_Query requesting only fields 'ids', limiting the query to 1 result, and disable priming of term / meta caches.
For WP_Comment_Query, passing count parameter and disable comment meta priming.
For WP_User_Query, questing only fields 'ids', limiting the query to 1 result and disable priming of user caches.

This change should be simple, as total counts should be covered by existing unit tests.

Change History (10)

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


5 weeks ago
#1

  • Keywords has-patch added

#2 @johnbillion
5 weeks ago

Which condition does the $total_posts < 1 logic handle? ie. why is the found_posts property not populated in the main query?

#3 @spacedmonkey
3 weeks ago

@johnbillion I don't have any context on this one. Maybe @TimothyBlynJacobs @kadamwhite or @rachelbaker would know why this was done.

#4 @joehoyle
3 weeks ago

Could it be that an out of bounds query means found_posts is 0? I.e. if I hit ?paged=100 when there is only 10 pages, I guess found_posts even with the FOUND_ROWS() returns 0?

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


3 weeks ago

#6 follow-up: @adamsilverstein
3 weeks ago

  • Keywords reporter-feedback added

Thanks for the PR @Sukhendu2002 - is this ready for review (I see it is draft), or do you have any questions? cc: @spacedmonkey

#7 @kadamwhite
3 weeks ago

I believe @joehoyle is correct, if you have (say) 12 items and you did page=3, you'd get 0 results back but it's not clear if you've just paged beyond the maximum or if there's no items at all.

#8 @kadamwhite
3 weeks ago

If it's considered ready for review by @sukhendu2002 I'm +1 on the patch, it appears to achieve @spacedmonkey's desired improvements

#9 in reply to: ↑ 6 @sukhendu2002
3 weeks ago

Replying to adamsilverstein:

Thanks for the PR @Sukhendu2002 - is this ready for review (I see it is draft), or do you have any questions? cc: @spacedmonkey

Sorry for the delay! I've now marked the PR as ready for review. Let me know if you have any feedback.

@sukhendu2002 commented on PR #8113:


3 weeks ago
#10

Hey @spacedmonkey, Thanks for the review! I have also applied the change in the WP_REST_Revisions_Controller class. Take a look when you get a chance. Thanks!

Note: See TracTickets for help on using tickets.