html - Rails 4 - rendering views is slow due associations -


i have query:

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

views: 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 |company| %>   <%= form_tag edit_inline_company_path(:id => company.id), :method => "post", :id => "id#{company.id}", :remote => true %>     <%= 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> 

problem - view takes 1800ms render (there's 40-50 items display).

i added query .includes(:car, :member) avoid n+1 problem (but in fact, didn't affect speed @ all).

after debugging, tried to remove _edit_inline_company.html.erb items associated different models these ones:

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

and time needed render data dropped on approx. 700ms, however, need these fields in output.

how optimize it? might have overlooked?

thank in advance.

edit:

i tried add indexes @brenzy suggested, this:

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

+ rake db:migrate, loading times stay same, no speeding here.

try indexing associations. here link may helpful: https://tomafro.net/2009/08/using-indexes-in-rails-index-your-associations


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -