[PLTFRM-1832] Improve query count performance to fix MySQL gone away error#102
[PLTFRM-1832] Improve query count performance to fix MySQL gone away error#102andrea-sdl wants to merge 10 commits into
Conversation
This reverts commit dd72311.
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the Users_Query_Utils::query_users_with_capability_filtering method by extracting query preparation logic into a new reusable helper method get_prepared_user_query_data. Additionally, it optimizes the MFA disabled user count query in the Forced_MFA_Users module by using native meta_query instead of fetching all user IDs and checking them individually.
- Extracts query preparation logic into
get_prepared_user_query_data()for better code reusability - Optimizes MFA disabled count by using
meta_queryto filter users at the database level - Adds PHPStan configuration to exclude e2e test node_modules
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 | Refactored by extracting query preparation into get_prepared_user_query_data() method and reorganized query_users_with_capability_filtering() to use the extracted logic |
| modules/forced-mfa-users/class-forced-mfa-users.php | Optimized by adding get_users_without_two_factor_meta_query() method and using it with meta_query to filter users at database level instead of post-query PHP filtering |
| phpstan.neon | Added excludePaths configuration to skip e2e node_modules during static analysis |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@Automattic/vip-platform-cantina It would be awesome if you could take a moment to check this out, to spot possible issues with this approach, specifically for the SQL changes I did on the inactive users. Happy to add any context if the PR is not clear enough 🙇 |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@Automattic/vip-platform-cantina After some tests looks like the inactive user fixes are still not enough for when a multisite has so many records in the |
Description
This PR completely revamps the query logic for counting users.
The goal is to make the query lighter on the database, preventing issues when querying larger sites with many users.
Here are the specific changes in how we're querying
Users without 2FA
Previously we'd rely on the
Two_Factor_Core::is_user_using_two_factor( $user_id )count how many users where not using 2FA which was inefficient.To make the query lighter, I changed it to use user meta contents. If a user has 2FA enabled, the plugin will set the provider’s user method.
The change is that it no longer uses the plugin to determine which users have 2FA enabled. Instead, it reads the user meta field directly and searches for those who do not have any 2FA set up. In this case, we search for an empty list, false, or null.
This approach is much quicker and should reduce database usage compared to what we had before.
[Not in the PR anymore] Inactive Users
Note: I'm keeping the old description to document my approach, but this was removed in 7ec2a0c and the PR now only focuses on counting the users with 2FA disabled.
Pre-review checklist
Please make sure the items below have been covered before requesting a review:
Pre-deploy checklist
Steps to Test
To test this locally, please call
Note: The inactive user module is not part of this PR anymore, ignore the following part.