On 7 Aug 2007, at 21:55, Jeremy Evans wrote:
The main issue you are running into is that Rails' SQL queries for multiple included has_many associations return the cartesian product of the has_many_associations. Ideally, the best way to handle this is to send two or three separate SQL statements. You'd have one statement for each association, and then combine them together. The most efficient way is probably n+1 queries where n is the number of has_many associations, with one query to get the information on the main object, and one query for each has_many association, that only includes the association information and the main object's id (in order to associate it). That would shorten the number of rows returned for the queries you mention from 12,000 to 231 and from 27000 to 346. It's more complex than the current implementation, but it will preform much better. I'm not volunteering to implement it, though. :)
That would be the ideal
A bit fiddly for me. For now I'm going to junk one of the eager loads ( I do have a frequent use case where I display a list of questions), foregoing some eager loading seems to be the way forward for now.As a workaround, how about:question = Question.find_by_id(big_question.id, :include => :incoming_messages)question.instance_variable_set('@outgoing_messages', Question.find_by_id(big_question.id, :include => :outgoing_messages).outgoing_messages)
Also, note that for a single object, you are probably better off using lazy loading has_many associations (eager loading belongs_to associations is fine). Eager loading has_many associations should only be done if you are getting multiple objects at once (i.e. find :all).
Yes, I'm coming round to that view
Jeremy --~--~---------~--~----~------------~-------~--~----~You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.To post to this group, send email to [email protected]To unsubscribe from this group, send email to rubyonrails-core- [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/ group/rubyonrails-core?hl=en-~----------~----~----~----~------~----~------~--~---
smime.p7s
Description: S/MIME cryptographic signature
