The hotspots (most are already covered):

<% for po in @po_renewals %>
      <tr>

          <%= fields_for "po_renewals[]", po do |f| %>
           # [...]
          <td><%= f.collection_select :clerk_id, User.all, :id, :fullname,
:include_blank => true %></td>
      <td><%= f.collection_select :gr_id, User.all, :id, :fullname,
:include_blank => true %></td>


This code looks through all the instances contained in your @po_renewals
instance variable and for each field for each each renewal,  it generates 2
queries fetching all the users. (so you are doing # renewals * # fields * 2
queries that fetch all your users)

( From a convention view point, you might want to use <% @po_renewals.each
do |po| %> but that's a different issue. )

To avoid making these queries you could make the query in your controller
and store the result in an instance variable:

@all_users = User.all(:select => [:id, :fullname])

and then in your view:

<% @po_renewals.each do |po| %>
      <tr>

          <%= fields_for "po_renewals[]", po do |f| %>
           # [...]
          <td><%= f.collection_select :clerk_id, @all_users, :id, :fullname,
:include_blank => true %></td>
      <td><%= f.collection_select :gr_id, @all_users, :id, :fullname,
:include_blank => true %></td>


You should also do the same thing for
  <td><%= f.collection_select :state_id

, State.where("id IN (5,6)"), :id, :state, :include_blank => true %></td>

You are generating a lot of DB queries when the data really doesn't change.
(I think there is a way for ActiveRecord to use an identity map but I'm not
quite sure how)


If you just do that, the load time should be drastically reduced.
Then I would look at what Ben was suggesting in regards to eager loading.

Let us know how you solve your challenge.

- Matt


On Wed, Mar 13, 2013 at 12:50 PM, Scott Olmsted <[email protected]> wrote:

> The tablesort Javascript might also contribute to the delay since it must
> build 23 indexes (assuming it makes indexes, which it might not).
>
> I would rethink whether having such a huge number of records instantly
> sortable on every column is really called for. I have had clients ask for
> similar displays and when we get into what they really want to do we find
> their need can be met with something not nearly so bandwidth, memory, and
> compute intensive.
>
> -Scott
>
>
> On Wednesday, March 13, 2013 12:02:39 PM UTC-7, KT wrote:
>>
>> Hi all - can anyone tell me what I'm doing so wrong that it takes 7-9
>> minutes to load this form view?
>>
>> https://github.com/sdstoic/**vits/blob/master/app/views/po_**
>> renewals/index.html.erb<https://github.com/sdstoic/vits/blob/master/app/views/po_renewals/index.html.erb>
>>
>> The table holds about 2700 records, and has many columns, but still...
>> should it take that long?
>>
>> Ruby 1.9.3 / Rails 3.0.3
>> Apache / Passenger / MySQL
>>
>> Thanks in advance for any help.
>> Katie
>>
>  --
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
> ---
> You received this message because you are subscribed to the Google Groups
> "SD Ruby" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to