Commit cff39a54 authored by Andreas Brandl's avatar Andreas Brandl
Browse files

Apply query changes to snippets only.

This applies the changes introduced in `Project.public_or_visible_to_user`
to the snippets finder only.

We know that for `SnippetsFinder`, this change improves SQL timing from
5/23/25s to 0.7/2/4s (mean/p95/p99). At the same time, the scope was too
broad, (negatively) affecting SQL timings in various other places:

With this commit, the snippets dashboard stays usuable as we generally
don't run into statement timeouts. In contrast to the earlier change in
!17088, the scope of the change is limited to `SnippetsFinder` only,
thus not affecting other places.
parent 916087feb350
...@@ -58,12 +58,43 @@ def authorized_snippets ...@@ -58,12 +58,43 @@ def authorized_snippets
.public_or_visible_to_user(current_user) .public_or_visible_to_user(current_user)
end end
# Returns a collection of projects that is either public or visible to the
# logged in user.
# A caller may pass in a block to modify individual parts of
# the query, e.g. to apply .with_feature_available_for_user on top of it.
# This is useful for performance as we can stick those additional filters
# at the bottom of e.g. the UNION.
def projects_for_user(&block)
return unless current_user
# If the current_user is allowed to see all projects,
# we can shortcut and just return.
return if current_user.full_private_access?
authorized = current_user
.where('project_authorizations.project_id =')
authorized_projects ='EXISTS (?)', authorized))
levels = Gitlab::VisibilityLevel.levels_for_user(current_user)
visible_projects = levels))
# We use a UNION here instead of OR clauses since this results in better
# performance.
union =[''),'')])
Project.from("(#{union.to_sql}) AS #{Project.table_name}")
def feature_available_projects def feature_available_projects
# Don't return any project related snippets if the user cannot read cross project # Don't return any project related snippets if the user cannot read cross project
return table[:id].eq(nil) unless Ability.allowed?(current_user, :read_cross_project) return table[:id].eq(nil) unless Ability.allowed?(current_user, :read_cross_project)
projects = Project.public_or_visible_to_user(current_user) projects = projects_for_user do |part|
.with_feature_available_for_user(:snippets, current_user).select(:id) part.with_feature_available_for_user(:snippets, current_user)
arel_query = arel_query =
table[:project_id].in(arel_query) table[:project_id].in(arel_query)
--- ---
title: Revert Project.public_or_visible_to_user changes title: Revert Project.public_or_visible_to_user changes and only apply to snippets
merge_request: merge_request:
author: author:
type: fixed type: fixed
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment