optimize countUsersByQuery when not querying for specific permissions#3429
Open
thomasgl-orange wants to merge 1 commit intoSonarSource:masterfrom
Open
Conversation
SummaryConditional SQL optimization for counting users without permission filters. The What reviewers should knowReview focus:
Testing:
|
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.
See support ticket
#69600.This PR is an optimisation (800ms down to a handful of ms) for a slow SQL query (
countUsersByQuery), when counting users without any additional filtering criteria (no specific permission).The original SQL query, as spotted in our
pg_stat_statements:A closer look shows that parameters $1, $2, $3 and $4 only affect values of some sub-queries results which are not actually used in the parent query, so it can first be simplified like this:
Furthermore, because the selected result is only a
count(distinct(users.uuid)), and the main querywherecondition is only about thisuserstable, whatever is left-outer joined don't actually influence the final result!So an equivalent much simpler query goes like this:
In terms of exec plan, the original query is indeed a bit crazy for what it actually does: https://explain.dalibo.com/plan/5245ac7bg4a93b9g
The proposed change get rid of these unnecessary
left outer joinwhen NOTquery.withAtLeastOnePermission(), meaning when the left-outer joined tables can't contribute anything which would be used for filtering, and thus which could influence thecount(distinct)result. (A more verbose version of this explanation is in the referenced support ticket.)This SQL query is involved when processing
GET api/permissions/users?projectKey=...requests (with no additionalpermissionfilter). It's something which happens from the project permissions page in the SonarQube UI, where we've indeed observed a slow (800ms) reply from the server in our browser dev tools.Furthermore, but that's a very specific use-case, we happen to periodically execute a script which exports users permissions for all our SonarQube projects, as part of our monitoring/auditing tools for SQ. That's thousands of executions of this query every day, and the main reason we've seen it at a top spot in our
pg_stat_statements.Provide a unit test for any code you changed./gradlew server:sonar-db-dao:testis still passingIf there is a JIRA ticket available, please make your commits and pull request start with the ticket ID (SONAR-XXXX)SONAR-XXXXticket in Jira; if you create one, let me know and I will amend the commit title (or feel free to amend the commit yourself)