summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexis Reigel <alexis.reigel.ext@siemens.com>2018-11-28 09:52:02 +0100
committerAlexis Reigel <alexis.reigel.ext@siemens.com>2019-02-27 20:19:50 +0100
commitcd063eec32a8b32d9b118f6cbdb0e96de0d0ec51 (patch)
tree987751c863c042d391f8cb061a160cab11ce9ecf
parent1edeecb0940254d9a8e6e01cb8029181a5b1f440 (diff)
downloadgitlab-ce-cd063eec32a8b32d9b118f6cbdb0e96de0d0ec51.tar.gz
optimize sql query to get tags related to runners
The query generated by ActsAsTaggableOn `@taggable_type.all_tags` is very inefficient (joins too much, grouping, inner select, ...).
-rw-r--r--app/finders/autocomplete/acts_as_taggable_on/tags_finder.rb14
1 files changed, 12 insertions, 2 deletions
diff --git a/app/finders/autocomplete/acts_as_taggable_on/tags_finder.rb b/app/finders/autocomplete/acts_as_taggable_on/tags_finder.rb
index 65ae90c08b4..81e22c85d34 100644
--- a/app/finders/autocomplete/acts_as_taggable_on/tags_finder.rb
+++ b/app/finders/autocomplete/acts_as_taggable_on/tags_finder.rb
@@ -11,21 +11,31 @@ module Autocomplete
end
def execute
- @tags = @taggable_type.all_tags
+ @tags = ::ActsAsTaggableOn::Tag.all
+ filter_by_taggable_type!
search!
limit!
@tags
end
+ def filter_by_taggable_type!
+ # rubocop: disable CodeReuse/ActiveRecord
+ @tags = @tags
+ .joins(:taggings)
+ .where(taggings: { taggable_type: @taggable_type.name })
+ .distinct
+ # rubocop: enable CodeReuse/ActiveRecord
+ end
+
def search!
search = @params[:search]
return unless search
if search.empty?
- @tags = @taggable_type.none
+ @tags = ::ActsAsTaggableOn::Tag.none
return
end