Make WordPress Core

Opened 10 years ago

Closed 4 years ago

Last modified 6 weeks ago

#39090 closed enhancement (fixed)

Remove duplicate query from posts page in the admin

Reported by: rodrigosprimo Owned by:
Priority: normal Milestone: 6.3
Component: Users Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description

The posts page in the admin (/wp-admin/edit.php) is running twice the query below. This is a problem specially for sites with a significant number of users since this query can be super slow (see #19867 and #28160).

SELECT wp_users.ID,wp_users.user_login,wp_users.display_name
FROM wp_users
INNER JOIN wp_usermeta
ON ( wp_users.ID = wp_usermeta.user_id )
WHERE 1=1
AND ( ( wp_usermeta.meta_key = 'wp_user_level'
AND wp_usermeta.meta_value != '0' ) )
ORDER BY display_name ASC

The query is executed by WP_User_Query::query() and, in this particular page, is called by the function wp_dropdown_users().

The attached patch fixes this problem by adding the result to the cache when the query is run the first time. Preventing multiple executions of the same query.

Attachments (1)

39090.patch​ (1.0 KB ) - added by rodrigosprimo 10 years ago.

Download all attachments as: .zip

Change History (5)

#1 @boonebgorges
10 years ago

  • Component Posts, Post Types → Users
  • Focuses administration removed
  • Keywords needs-patch needs-unit-tests added
  • Milestone Awaiting Review → Future Release

@rodrigosprimo Thanks for the patch!

As written, the patch won't work - it caches queries indefinitely. This means that cached data will be returned even after (say) a new user registers. So we need some invalidation built in. The strategy we use in other query classes is an incrementor hashed with the cache key:

$last_changed = wp_cache_get_last_changed( 'users' );
$hashed_args = md5( $args );
$cache_key = "get_users:$hashed_args:$last_changed";

Then, on any action that will result in user query results being different (new user creation, user edit, etc), invalidate the cache: wp_cache_set( 'last_changed', microtime(), 'users' )

This may be a good time to break the user query into two separate queries: one for IDs only (which will be cached as described above), and one for user objects (which are already separately cached).

We'll need unit tests demonstrating invalidation in order to move forward.

#2 @spacedmonkey
4 years ago

  • Resolution → fixed
  • Status new → closed

This is now fixed, now that #40613 is committed.

#3 @joemcgill
3 years ago

  • Milestone Future Release → 6.3

This ticket was mentioned in ​PR #14013 on ​WordPress/wordpress-develop by ​@irozum.


6 weeks ago
#4

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

Adds regression coverage for the duplicate-query problem described in #39090: calling wp_dropdown_users() more than once with identical arguments (as happens on /wp-admin/edit.php) used to re-run the same SELECT ... FROM wp_users INNER JOIN wp_usermeta ... query every time.

The behaviour is already correct in trunk, as a side effect of the generic WP_User_Query result caching added in [55657] (fixes #40613). This PR only adds tests pinning that behaviour down for wp_dropdown_users() specifically, since the needs-unit-tests keyword was never cleared on #39090 and no test exercised the public wp_dropdown_users() entry point for this scenario.

Tests added (tests/phpunit/tests/user/wpDropdownUsers.php):

  • test_repeated_identical_calls_do_not_run_duplicate_queries — asserts a second call to wp_dropdown_users() with identical arguments runs zero additional database queries (the core regression from #39090).
  • test_cache_is_invalidated_when_a_user_is_created_between_calls — asserts the opposite case: creating a user between two calls does invalidate the cache, so the dropdown reflects the new user and a new query does run. This guards the specific concern raised in the ticket discussion that blocked the original naive patch (unbounded caching with no invalidation).

Both tests were verified to fail when the underlying behaviour is broken (disabling cache_results by default for the first test, and stubbing out wp_cache_set_users_last_changed() for the second) and pass again once reverted.

This is a test-only change — it adds no production behaviour and touches no files under src/.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Surveying Trac for a tractable coverage ticket, reading the implementation and ticket history to determine the correct assertions, writing the tests, and verifying them (including temporarily breaking the implementation to confirm each test fails for the right reason). Reviewed and verified by me before submission.

Note: See TracTickets for help on using tickets.