Artūras Šlajus
2009-Nov-02 22:56 UTC
How to force AR belongs_to include option to eager load?
Hello I have two classes class SolarSystem < ActiveRecord::Base has_many :fow_ss_entries, :dependent => :delete_all class FowSsEntry < ActiveRecord::Base belongs_to :galaxy belongs_to :solar_system belongs_to :player named_scope :for, Proc.new { |galaxy, player| galaxy = galaxy.id if galaxy.is_a? Galaxy player = player.id if player.is_a? Player { :conditions => {:galaxy_id => galaxy, :player_id => player}, :include => :solar_system } } Now the evil thing is, that even thou'' :include is specified, it does nothing. New AR eager loading resorts to doing n+1 sql queries in FowSsEntry.for(1,1).each { |fse| fse.solar_system } How do I force it into joining my table, without impacting my SQL performance? -- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Nov-03 00:18 UTC
Re: How to force AR belongs_to include option to eager load?
On Nov 2, 10:56 pm, Artūras Šlajus <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Now the evil thing is, that even thou'' :include is specified, it does > nothing. New AR eager loading resorts to doing n+1 sql queries in > FowSsEntry.for(1,1).each { |fse| fse.solar_system } > > How do I force it into joining my table, without impacting my SQL > performance?Is the issue the fact that :include does not always trigger a join based strategy ? What are the actual sql queries you see? Fred
Artūras Šlajus
2009-Nov-03 10:09 UTC
Re: How to force AR belongs_to include option to eager load?
Frederick Cheung wrote:> On Nov 2, 10:56�pm, Art�ras �lajus <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> Now the evil thing is, that even thou'' :include is specified, it does >> nothing. New AR eager loading resorts to doing n+1 sql queries in >> FowSsEntry.for(1,1).each { |fse| fse.solar_system } >> >> How do I force it into joining my table, without impacting my SQL >> performance? > > Is the issue the fact that :include does not always trigger a join > based strategy ? What are the actual sql queries you see?Yes. I see this: [2009-11-03 00:35:26|main|debug] FowSsEntry Load (0.0ms) SELECT * FROM `fow_ss_entries` WHERE (`fow_ss_entries`.`galaxy_id` = 1 AND `fow_ss_entries`.`player_id` = 1) [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM `solar_systems` WHERE (`solar_systems`.`id` = 57) [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM `solar_systems` WHERE (`solar_systems`.`id` = 157) [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM `solar_systems` WHERE (`solar_systems`.`id` = 313) [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM `solar_systems` WHERE (`solar_systems`.`id` = 314) Which is kind of bad ;( -- Posted via http://www.ruby-forum.com/.
chris johnson
2009-Nov-10 23:41 UTC
Re: How to force AR belongs_to include option to eager load?
Is there a method for forcing a join-based strategy? On Nov 3, 2:09 am, Artūras Šlajus <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Frederick Cheung wrote: > > On Nov 2, 10:56 pm, Art ras lajus <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > >> Now the evil thing is, that even thou'' :include is specified, it does > >> nothing. New AR eager loading resorts to doing n+1 sql queries in > >> FowSsEntry.for(1,1).each { |fse| fse.solar_system } > > >> How do I force it into joining my table, without impacting my SQL > >> performance? > > > Is the issue the fact that :include does not always trigger a join > > based strategy ? What are the actual sql queries you see? > > Yes. > > I see this: > [2009-11-03 00:35:26|main|debug] FowSsEntry Load (0.0ms) SELECT * FROM > `fow_ss_entries` WHERE (`fow_ss_entries`.`galaxy_id` = 1 AND > `fow_ss_entries`.`player_id` = 1) > [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM > `solar_systems` WHERE (`solar_systems`.`id` = 57) > [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM > `solar_systems` WHERE (`solar_systems`.`id` = 157) > [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM > `solar_systems` WHERE (`solar_systems`.`id` = 313) > [2009-11-03 00:35:26|main|debug] SolarSystem Load (0.0ms) SELECT * FROM > `solar_systems` WHERE (`solar_systems`.`id` = 314) > > Which is kind of bad ;( > > -- > Posted viahttp://www.ruby-forum.com/.