Skip to content

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
fix/capability-count-query-performance
Aug 21, 2025
Merged

Avoids querying the DB twice just to get the counts and explicitly use 'COUNT(DISTINCT for faster results#90
brunobasto merged 3 commits intoproductionfrom
fix/capability-count-query-performance

Conversation

@brunobasto
Copy link
Copy Markdown
Contributor

@brunobasto brunobasto commented Aug 21, 2025

Description

We already had a patch to use COUNT(DISTINCT on our fix_found_users_query hook, but that didn't prevent WP from doing the SELECT SQL_CALC_FOUND_ROWS() first.

WordPress will do two queries:

  1. To get the results with SELECT SQL_CALC_FOUND_ROWS(): https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-user-query.php#L844
  2. To get the count: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-user-query.php#L861

Our previous solution only changed the count query, but WP was still doing the first SQL_CALC_FOUND_ROWS as usual.

Pre-review checklist

Please make sure the items below have been covered before requesting a review:

  • This change works and has been tested locally or in Codespaces (or has an appropriate fallback).
  • This change has relevant unit tests (if applicable).
  • This change has relevant documentation additions / updates (if applicable).
  • I've created a changelog description that aligns with the provided examples.

Pre-deploy checklist

  • VIP staff: Ensure any alerts added/updated conform to internal standards (see internal documentation).

Steps to Test

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_Query instantiation 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.

Comment thread utils/class-users-query-utils.php Outdated
brunobasto and others added 2 commits August 21, 2025 15:21
… and maintainability

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@kat3samsin kat3samsin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏🏻

@kat3samsin kat3samsin self-requested a review August 21, 2025 20:20
@brunobasto brunobasto added this pull request to the merge queue Aug 21, 2025
Merged via the queue into production with commit d13846c Aug 21, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants