3

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.

Community
  • 1
  • 1
user2113245
  • 31
  • 1
  • 4

2 Answers2

3

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

Community
  • 1
  • 1
Brad Johnson
  • 153
  • 9
0

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

jake
  • 2,371
  • 1
  • 20
  • 31