Re: Why is Rails eager loading most of my database?

Frederick Cheung
2011-03-25T18:41:59Z

On 25 Mar 2011, at 18:34, Taylor Strait
<lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:

> I have a highly-normalized user model called Respondent. It has many
> join fields such as "ethnicity_id", "education_id",
etc. that define
> relational categories. Most of them are indexed. For reasons unknown,
> Rails is hitting a huge number of the tables when accessing any
> Respondent objects. For example, on a simple SHOW action:
>

Looks to me that it''s because of some of your validations, that seem to
be validating that (eg) ethnicity_id is in the list of all ethnicities etc.

Fred

> ==CONTROLLER== (from basic scaffold)
>  def show
>    @respondent = Respondent.find(params[:id])
>
>    respond_to do |format|
>      format.html # show.html.erb
>      format.xml  { render :xml => @respondent }
>    end
>  end
>
> ==VIEW== (note - nothing is even being rendered!)
> <%= notice %>
>
> <%= link_to ''Edit'', edit_respondent_path(@respondent)
%> |
> <%= link_to ''Back'', respondents_path %>
>
> ==RESULTING SERVER LOG=> Started GET "/respondents" for
127.0.0.1 at 2011-03-25 14:12:50 -0400
>  Processing by RespondentsController#index as HTML
> Geokit is using the domain: localhost
>  ←[1m←[35mPostalCode Load (101.0ms)←[0m  SELECT `postal_codes`.* FROM
> `postal_codes`
>  ←[1m←[36mCountry Load (1.0ms)←[0m  ←[1mSELECT `countries`.* FROM
> `countries`←[0m
>  ←[1m←[35mEthnicity Load (1.0ms)←[0m  SELECT `ethnicities`.* FROM
> `ethnicities`
>  ←[1m←[36mEducation Load (0.0ms)←[0m  ←[1mSELECT `educations`.* FROM
> `educations`←[0m
>  ←[1m←[35mJobStatus Load (0.0ms)←[0m  SELECT `job_statuses`.* FROM
> `job_statuses`
>  ←[1m←[36mRespondent Load (0.0ms)←[0m  ←[1mSELECT `respondents`.* FROM
> `respondents`←[0m
> Rendered layouts/_navigation.html.erb (1.0ms)
> Rendered respondents/index.html.erb within layouts/application (190.0ms)
> Completed 200 OK in 3809ms (Views: 205.0ms | ActiveRecord: 103.0ms)
>
> ==QUESTION=> Why in the world could the server need to hit these tables
and why is it
> taking so long? Some of these models are related -- for example,
> postal_code BELONGS_TO country, etc. It does not do this for any other
> models. I have attached the model and migration file for reference since
> they are so long.
>
> Attachments:
> http://www.ruby-forum.com/attachment/6083/respondent.rb
>
http://www.ruby-forum.com/attachment/6084/20110226202511_create_respondents.rb
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
>

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.