渴望在Rails 3中加载吗?
作者:互联网
我正在尝试学习如何提高Rails应用程序的性能,而我正在寻找的第一步是渴望加载.
我已经配置了bullet 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 %>
因此,我应该将其他推荐的列添加到“查找器”视图中吗?或者别的地方?
任何帮助,将不胜感激!
解决方法:
在您的控制器中,您想要更新查询以包括客户端:
@animals = Animal.includes(:client).page(params[:page]).per_page(15)
标签:eager-loading,rails-bullet,ruby-on-rails-3,mysql 来源: https://codeday.me/bug/20191201/2080580.html