gpt4 book ai didi

html - Rails 4 - 由于关联,渲染 View 很慢

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:55 25 4
gpt4 key购买 nike

我有这个问题:

  @companies = Company.includes(:car, :member).where('companies.status != 3 AND companies.status != 4 AND companies.status != 5').order(sort_column + " " + sort_direction) 

观点:all_companies.html.erb:

  <div id="cars_list">
<%= render :partial => 'companies_list', locals: { companies: @companies } %>
</div>

_companies_list.html.erb:

<div class="tr">
<span class="td">ID</span>
<span class="td">Registration</span>
...
</div>
<% companies.each do |company| %>
<%= form_tag edit_inline_company_path(:id => company.id), :method => "post", :id => "id#{company.id}", :remote => true do %>
<%= render :partial => 'edit_inline_company', locals: { company: company } %>
<% end %>
<% end %>

_edit_inline_company.html.erb:

<span class="td">
<span class="td">
<%= company.id %>
</span>
<span class="td">
<%= company.location %>
</span>
<span class="td">
<%= company.registered_at %>
</span>
...
<span class="td">
<%= company.member.full_name %>
</span>
<span class="td">
<%= company.car.code %>
</span>
<span class="td">
<%= company.car.driver %>
</span>
...
</span>

问题 - View 需要 1800 毫秒来呈现(只有大约 40-50 个项目要显示)。

我在查询中添加了.includes(:car, :member)来避免N+1问题(但实际上,它根本不影响速度)。

经过一些调试,我试图从 _edit_inline_company.html.erb 中删除与不同模型相关的项目:

  <span class="td">
<%= company.member.full_name %>
</span>
<span class="td">
<%= company.car.code %>
</span>
<span class="td">
<%= company.car.driver %>
</span>

渲染数据所需的时间大约下降了。 700 毫秒,但是,我在输出中也需要这些字段。

如何优化呢?我可能忽略了什么?

提前谢谢你。

编辑:

我尝试按照@brenzy 的建议添加索引,如下所示:

add_index :companies, :car_id, :name => 'car_id_ix'
add_index :companies, :member_id, :name => 'memberr_id_ix'

+ rake db:migrate,但加载时间保持不变,这里没有加速。

最佳答案

尝试为您的联想建立索引。这是一个可能有用的链接:https://tomafro.net/2009/08/using-indexes-in-rails-index-your-associations

关于html - Rails 4 - 由于关联,渲染 View 很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35259960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com