Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#41197 closed enhancement (fixed)

Improved caching on WP_Site_Query

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


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 7 years ago.
41197.2.diff (3.1 KB) - added by spacedmonkey 7 years ago.

Download all attachments as: .zip

Change History (8)

7 years ago

#1 @spacedmonkey
7 years ago

  • Keywords has-patch has-unit-tests added

#2 @flixos90
7 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 7 years ago by flixos90 (previous) (diff)

#3 @flixos90
7 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.

7 years ago

#4 @spacedmonkey
7 years ago

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

#5 @flixos90
7 years ago

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

Thanks @spacedmonkey. Will review soon.

#6 @flixos90
7 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.