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, ...).
parent 1edeecb0
......@@ -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
......
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