Question

I am attempting to learn how to increase the performance of Rails applications and the first step I am looking at is eager loading.

I've configured the bullet gem which shows where I can make use of eager loading, however, I'm not too sure how to make use of the help!

An example log is:

2012-04-26 15:59:34
0.0.0.0:3000http://0.0.0.0:3000/animals
N+1 Query detected
  Animal => [:client]
  Add to your finder: :include => [:client]
N+1 Query method call stack
  N+1 Query method call stack
  /Users/dannymcclelland/Projects/premvet/app/views/animals/index.html.erb:26:in `block in _app_views_animals_index_html_erb__2796162405947806753_70316525286320'
  /Users/dannymcclelland/Projects/premvet/app/views/animals/index.html.erb:22:in `_app_views_animals_index_html_erb__2796162405947806753_70316525286320'
  /Users/dannymcclelland/Projects/premvet/app/controllers/animals_controller.rb:7:in `index'2012-04-26 15:59:34[WARN] user: dannymcclelland
0.0.0.0:3000http://0.0.0.0:3000/animals
Unused Eager Loading detected
  Animal => [:id, :AnimalName, :Species]
  Remove from your finder: :include => [:id, :AnimalName, :Species]2012-04-26 16:00:56

The lines that jump out are:

N+1 Query detected
Animal => [:client]
Add to your finder: :include => [:client]

and

Unused Eager Loading detected
Animal => [:id, :AnimalName, :Species]
Remove from your finder: :include => [:id, :AnimalName, :Species]

What I am unsure of, is what is the definition of the 'finder'. Is it the block in the view or is it in the controller.

Taking the first section of log that jumps out at me the controller is as follows:

def index
@animals = Animal.page(params[:page]).per_page(15)

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @animals }
end
end

the model:

class Animal < ActiveRecord::Base
  self.table_name = 'animal'
  self.primary_key = 'PVID'

  attr_accessible :AddedBy, :Age, :AnimalBFAmount, :AnimalBalance, :AnimalName, :Archive,    :BillType, :Breed, :ChronicStatus, :Class, :Classification, :ClientKey, :Colour, :Date1, :DateOfBirth, :DateofBirth, :Dead, :DiaryQueue, :DiscField, :DrugsAtCost, :DrugsNoVAT, :ESDAmount, :ESDType, :FNote, :FirstRegisteredDate, :Height, :IDNumber, :Insured, :InsuredWith, :IsClient, :IsClientDate, :IsMaster, :LastBilledAmount, :LastBilledDate, :LastConsDate, :LastContributionDate, :LastPaidDate, :LastWeightDate, :Locked, :LoyaltyMultiplier, :LoyaltyPoints, :MR_Flag_0, :MR_Flag_1, :MR_Flag_10, :MR_Flag_11, :MR_Flag_12, :MR_Flag_13, :MR_Flag_14, :MR_Flag_15, :MR_Flag_2, :MR_Flag_3, :MR_Flag_4, :MR_Flag_5, :MR_Flag_6, :MR_Flag_7, :MR_Flag_7, :MR_Flag_8, :MR_Flag_9, :Mileage, :Neutered, :NextApptDate, :ORT, :OldSex, :Opt_Flag_0, :Opt_Flag_1, :Opt_Flag_2, :Opt_Flag_3, :Opt_Flag_4, :Opt_Flag_5, :Opt_Flag_6, :Opt_Flag_7, :PVID, :PreferredContact, :PreferredUser, :Ref1, :RefPrac, :ReferredBy, :SSDType, :SeenInPeriod, :SendBill, :Sex, :SiteAnimal, :Species, :Status, :SurcAmount, :SurcType, :SurgeryNumber, :TBU, :TOSAmount, :TOSDrugs, :TOSFees, :TOSType, :Weight

  belongs_to :client, :foreign_key => 'ClientKey'
  has_many :clinicals, :foreign_key => 'PVID'
  has_many :labs, :foreign_key => 'PVID'
  has_many :consults, :foreign_key => 'pvid'
  has_many :sheets, :foreign_key => 'PVID'

  default_scope :order => "Dead ASC, AnimalName ASC"
end

Note that I'm using a legacy database hence the strange column names etc.

The view contains the following:

<% @animals.includes(:client).each do |animal| %>
  <tr>
    <td><%= animal.id %></td>    
    <td><%= animal.AnimalName %></td>
    <td><%= link_to animal.client.Surname, animal.client %></td>    
    <td><%= animal.Species %></td>
    <td><%= animal.Breed %></td>
    <td><%= animal.Dead %></td>    
    <td><%= link_to 'Show', animal %></td>
  </tr>
<% end %>

So, should I be adding the other recommended columns to the view 'finder'? Or somewhere else?

Any help would be appreciated!

Was it helpful?

Solution

In your controller, you want to update your query to include the client:

@animals = Animal.includes(:client).page(params[:page]).per_page(15)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top