optimize the query using acts
I am using acts-as-taggable-on gem on my project. However, it takes about 80s to load all the tags.
I have added eager loading in the run_controller, but it is not working. Here is the piece of code:
def index
@runs = Measurement.includes(:tags).order( "run_name DESC" ).group( :run_name )#.descending #.order_by( :run => 'desc' )
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @runs }
end
end
I am using tag_list, which is supported by acts-as-taggable-on, to display tags. So using eager loading on Measurements has no impact on the performance. The following two are the related issues in stackflow: 1. acts_as_taggable_on: how to optimize the query? 2. Optimizing queries with acts_as_taggable_on
I looked at the log file and found that the most time-costing part is loading the tags, like
ActsAsTaggableOn::Tag Load (69.9ms) SELECT tags
.* FROM tags
INNER JOIN taggings
ON tags
. id
= taggings
. tag_id
WHERE taggings
. taggable_id
= 223866 AND taggings
. taggable_type
= 'Measurement' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
ActsAsTaggableOn::Tag Load (70.2ms) SELECT tags
.* FROM tags
INNER JOIN taggings
ON tags
. id
= taggings
. tag_id
WHERE taggings
. taggable_id
= 223854 AND taggings
. taggable_type
= 'Measurement' AND (taggings.context = 'tags'
It got thousands of queries to load the tag and each query cost about 0.07s. Those following codes are using for displaying tags.
= form_for (run), :remote => true, :method => :put, :html => { :class => "myform"} do |f|
=f.text_field :tag_list, :class => "tags"
Any help? Thanks.
Have considered switching to https://github.com/tmiyamon/acts-as-taggable-array-on . You will need to be using postgres as your database.
Take a look at this http://adamnengland.wordpress.com/2014/02/19/benchmarks-acts-as-taggable-on-vs-postgresql-arrays
I just came across this today. You can eager load through the tags
association, as long as you don't use the tag_list
method.
Mentioned here and here
链接地址: http://www.djcxy.com/p/35938.html下一篇: 使用行为优化查询