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, ...).
上级 1edeecb0
...@@ -11,21 +11,31 @@ module Autocomplete ...@@ -11,21 +11,31 @@ module Autocomplete
end end
def execute def execute
@tags = @taggable_type.all_tags @tags = ::ActsAsTaggableOn::Tag.all
filter_by_taggable_type!
search! search!
limit! limit!
@tags @tags
end 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! def search!
search = @params[:search] search = @params[:search]
return unless search return unless search
if search.empty? if search.empty?
@tags = @taggable_type.none @tags = ::ActsAsTaggableOn::Tag.none
return return
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册