Avoids querying the DB twice just to get the counts and explicitly use 'COUNT(DISTINCT for faster results#90
Merged
brunobasto merged 3 commits intoproductionfrom Aug 21, 2025
Conversation
…use `'COUNT(DISTINCT` for faster results
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes user query performance by avoiding WordPress's default dual-query pattern. Instead of letting WordPress run both a SELECT SQL_CALC_FOUND_ROWS() query and a separate count query, the code now manually constructs a single optimized query using COUNT(DISTINCT) when only counts are needed.
- Replaces
WP_User_Queryinstantiation with manual query preparation and construction - Removes the temporary hook-based solution for fixing found users queries
- Uses direct database queries with
COUNT(DISTINCT)for more efficient counting
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| utils/class-users-query-utils.php | Modified to manually construct optimized SQL queries instead of using WP_User_Query's dual-query pattern |
| modules/inactive-users/class-inactive-users.php | Removed temporary hook-based solution that was previously used to fix count queries |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
… and maintainability Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
kat3samsin
approved these changes
Aug 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
We already had a patch to use
COUNT(DISTINCTon ourfix_found_users_queryhook, but that didn't prevent WP from doing theSELECT SQL_CALC_FOUND_ROWS()first.WordPress will do two queries:
SELECT SQL_CALC_FOUND_ROWS(): https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-user-query.php#L844Our previous solution only changed the count query, but WP was still doing the first
SQL_CALC_FOUND_ROWSas usual.Pre-review checklist
Please make sure the items below have been covered before requesting a review:
Pre-deploy checklist
Steps to Test