Skip to content

Commit

Permalink
[api] Fix huge bottleneck in notification emails
Browse files Browse the repository at this point in the history
The way source_watchers and target_watchers were implemented was pretty
suboptimal. It took >20 seconds to dig out 128 users watching
openSUSE:Factory by going through the WatchedProject objects connected
to these 128 users and listed the users for these, which resulted in
128x128 SELECT user ... - but all we want is the list of 128 User
objects (to filter subscriptions for them)

So the new query results in one SQL statement taking the database a
friction of a second
  • Loading branch information
coolo committed Dec 11, 2017
1 parent 4e6efd8 commit b0d7d26
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/api/app/models/event/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def source_watchers

def find_watchers(project_key)
project_names = payload['actions'].map { |action| action[project_key] }.uniq
projects = Project.where(name: project_names).joins(watched_projects: :user)
projects.flat_map { |project| project.watched_projects.map(&:user) }
watched_projects = WatchedProject.where(project: Project.where(name: project_names))
User.where(id: watched_projects.select(:user_id))
end
end
end
end

0 comments on commit b0d7d26

Please sign in to comment.