Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55674 closed enhancement (fixed)

Improve performance Search REST API

Reported by: furi3r's profile furi3r Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.1 Priority: normal
Severity: normal Version: 5.0
Component: REST API Keywords: has-patch has-unit-tests commit early assigned-for-commit has-dev-note
Focuses: performance Cc:

Description

Search query rest endpoint will make a query per post_id for each post in the result.
A possible solution, Prime_post_cache needed.
https://github.com/WordPress/wordpress-develop/blob/4558de543c64c877ffea4cdfbdf67d743c3e4391/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php#L86

Attachments (3)

Screen Shot 2022-05-04 at 3.02.47 PM.png (54.4 KB) - added by furi3r 2 years ago.
api query result
search_before.png (192.9 KB) - added by furi3r 2 years ago.
Query result before patch
search_after.png (193.7 KB) - added by furi3r 2 years ago.
Query result after patch

Download all attachments as: .zip

Change History (23)

@furi3r
2 years ago

api query result

#1 @furi3r
2 years ago

working on it.

This ticket was mentioned in PR #2744 on WordPress/wordpress-develop by manuelRod.


2 years ago
#2

  • Keywords has-patch has-unit-tests added

Trac ticket: #55674

#3 @spacedmonkey
2 years ago

  • Keywords commit early added
  • Milestone changed from Awaiting Review to 6.1
  • Owner set to spacedmonkey
  • Status changed from new to assigned
  • Version set to 4.7

This ticket was mentioned in Slack in #core by costdev. View the logs.


2 years ago

namithj commented on PR #2744:


2 years ago
#5

Why was limiting the fields to ids avoided to fetch the whole post data and then pluck the ids?

peterwilsoncc commented on PR #2744:


2 years ago
#6

@namithj

Why was limiting the fields to ids avoided to fetch the whole post data and then pluck the ids?

Doing a query for the entire post object will warm the object cache for the get_post() function calls in the prepare_item() method. This prevents an additional database query for each found post.

You raise a good point though, it's probably worth adding a comment explaining this in the code so a future developer doesn't change the query and cause extra database queries.

@furi3r
2 years ago

Query result before patch

@furi3r
2 years ago

Query result after patch

#7 @furi3r
2 years ago

See attachment, before and after applying the patch, we save one query per result, and the time is considerably reduced.
For a 6 items result:
27 VS 22 queries.
0.0171s VS 0.0146s

Last edited 2 years ago by furi3r (previous) (diff)

#8 @TimothyBlynJacobs
2 years ago

  • Version changed from 4.7 to 5.0

This ticket was mentioned in Slack in #core by chaion07. View the logs.


2 years ago

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


2 years ago

#11 @audrasjb
2 years ago

  • Keywords assigned-for-commit added
  • Owner changed from spacedmonkey to audrasjb
  • Status changed from assigned to accepted

#12 @spacedmonkey
2 years ago

@audrasjb Are you good to commit this. It as been approved by two core committers ( me included ).

#13 @audrasjb
2 years ago

  • Owner changed from audrasjb to spacedmonkey
  • Status changed from accepted to assigned

Reassigned to @spacedmonkey who is taking care of this ticket from the beginning :)

#14 @spacedmonkey
2 years ago

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

In 53485:

REST API: Improve post cache priming in WP_REST_Post_Search_Handler class.

In the WP_REST_Post_Search_Handler class, ensure that post, post meta and term caches are correctly primed when performing a search.

Props furi3r, spacedmonkey, TimothyBlynJacobs, audrasjb, peterwilsoncc.
Fixes #55674.

spacedmonkey commented on PR #2744:


2 years ago
#15

Committed.

#16 @desrosj
2 years ago

In 53676:

Coding Standards: Apply some alignment fixes after composer format.

Follow up to [53485].

See #55674.

#17 @desrosj
2 years 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.

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


2 years ago

#19 @spacedmonkey
2 years ago

Early draft of a dev note can be found here.

Note: See TracTickets for help on using tickets.