我正在尝试学习如何提高 Rails 应用程序的性能,而我正在研究的第一步是急切加载。
我已经配置了子弹 gem,它显示了我可以在哪里使用急切加载,但是,我不太确定如何使用帮助!
一个示例日志是:
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
跳出来的行是:
N+1 Query detected
Animal => [:client]
Add to your finder: :include => [:client]
和
Unused Eager Loading detected
Animal => [:id, :AnimalName, :Species]
Remove from your finder: :include => [:id, :AnimalName, :Species]
我不确定的是,“发现者”的定义是什么。它是视图中的块还是控制器中的块。
以我跳出的第一部分日志为例,控制器如下:
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
该模型:
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
请注意,我使用的是旧数据库,因此出现了奇怪的列名等。
该视图包含以下内容:
<% @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 %>
那么,我应该将其他推荐的列添加到视图“查找器”吗?或者别的地方?
任何帮助,将不胜感激!