views:

29

answers:

0

I have model called VoteTopic with the following associations

belongs_to :user
belongs_to :category
has_many :comments
has_many :vote_items, :dependent => :destroy
has_many :votes, :through => :vote_items

I use Searchlogic gem to query for @vote_topics in the index action as follows..

scope_procedure :all_approved, lambda {status_equals(STATUS['approved']).descend_by_created_at(:include => [{:vote_items => :votes}, :user, :category])}

There are currently 135 records that match this and it takes about 2000ms to load it from a cold start and about 850 the second time onwards. I am trying to fine tune the performance and it seems the eager loading doesn't quite work or may be my understanding is incorrrect.

Here is the partial that presents the results,

http://pastie.org/1062941

when I look at rails-footnotes, it says there are 66 queries being issued (output included in pastie), which is not quite what i expect, since I am hoping to eager load things.

a - What am I doing wrong?

b - Also I notice that the partials take up quite sometime, Is there a better way to print them?

c - If I supply a :select clause to the searchlogicpl scope procedure it is ignored. I installed the http://github.com/blythedunham/eload-select plugin, but that didn't help.

d - I also noticed that instead of using searchlogic if I specify a :joins clause in a regular named_scope, the queries take 15 times longer to execute.

Please help!

Thanks

Update More stats:

If I do

@vote_topics = VoteTopic.status_equals('a').descend_by_created_at(:joins => [{:vote_items => :votes}, :user, :category]).paginate(:page => params[:page], :per_page => Constants::LISTINGS_PER_PAGE) #=> 66 queries, 2132 ms

 @vote_topics = VoteTopic.find(:all, :conditions => ['status = ?', 'a'], :order => 'created_at DESC', :include => [{:vote_items => :votes}, :user, :category]).paginate(:page => params[:page], :per_page => Constants::LISTINGS_PER_PAGE) #=> 12 queries 2192 ms


@vote_topics = VoteTopic.find(:all, :conditions => ['status = ?', 'a'], :order => 'created_at DESC', :joins => [{:vote_items => :votes}, :user, :category]).paginate(:page=> params[:page], :per_page => Constants::LISTINGS_PER_PAGE) #=> 39 queries, 2000 ms.

It seems just doing a find and :include reduces the number of queeries though I still can't reduce the time. Am I missing something here? end