WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#41197 closed enhancement (fixed)

Improved caching on WP_Site_Query

Reported by: spacedmonkey Owned by: flixos90
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.6
Component: Networks and Sites Keywords: has-patch has-unit-tests
Focuses: multisite Cc:

Description

In WP_Site_Query, queries are cached as a list of site ids. The key cache is generated using the query args. But even through the values in the cache are the same, if you query by 'fields' => 'all' and 'fields' => 'ids', two caches are generated. By unsetting the 'fields' arg before generating the cache key, both queries will use the same cache.

Attachments (2)

41197.diff (2.1 KB) - added by spacedmonkey 4 years ago.
41197.2.diff (3.1 KB) - added by spacedmonkey 4 years ago.

Download all attachments as: .zip

Change History (8)

@spacedmonkey
4 years ago

#1 @spacedmonkey
4 years ago

  • Keywords has-patch has-unit-tests added

#2 @flixos90
4 years ago

41197.diff looks good, there are just a few things that can be improved:

  • The $fields argument can always be unset, even if $count is true, as that parameter will already distinguish in the cache key between whether to store a count or not.
  • For the unit tests, it would be better to have three distinct tests (only one assertion per test):
    • Run the same query twice, once with $fields set to all, the other with $fields set to ids. $wpdb->num_queries should match.
    • Another one similar to the above, but with $count set to true in both queries. $wpdb->num_queries should match.
    • Run the same query twice (with the same $fields value!), but one time with the $count argument as true. $wpdb->num_queries should differ.
Last edited 4 years ago by flixos90 (previous) (diff)

#3 @flixos90
4 years ago

  • Milestone changed from Awaiting Review to 4.9
  • Owner set to spacedmonkey
  • Status changed from new to assigned

Per discussion with @spacedmonkey, he'll take care of what still needs to happen here.

@spacedmonkey
4 years ago

#4 @spacedmonkey
4 years ago

@flixos90 feedback completed as requested. Names of the tests might need changing.

#5 @flixos90
4 years ago

  • Owner changed from spacedmonkey to flixos90
  • Status changed from assigned to reviewing

Thanks @spacedmonkey. Will review soon.

#6 @flixos90
4 years ago

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

In 41059:

Multisite: Improve caching in WP_Site_Query by ignoring the $fields argument.

Prior to this change there were two different cache keys used for the same query. That is because regardless of the $fields argument, the query response will be the same.

Props spacedmonkey.
Fixes #41197.

Note: See TracTickets for help on using tickets.