Rails连接表渲染坏sql

时间:2009-03-27 19:13:19

标签: mysql ruby-on-rails activerecord

更新:包含模型关系

我在Bands and Events之间有多对多的连接表(一个事件有很多乐队,一个乐队有很多事件......)

我有一个列出所有事件的主页面,以及一个显示过去7天内更新事件的最新页面。

有人可以解释为什么最近的方法生成包含所有连接的完美sql

@events = Event.find(:all, :include => [:venue, :bands], :conditions => {:updated_at => d1..DateTime.now}, :order => 'events.updated_at desc')

主方法会生成可怕的sql,用于查询每个事件的波段表。

@events = Event.find(:all, :include => [:venue, :bands], :order => 'events.day')

他们都使用相同的视图,没有其他处理/查询正在进行。两者之间的唯一区别是:conditions参数和它排序的列。这让我发疯了!

对不起,这是我的模特和他们的关系

event.rb

has_many :event_bands
has_many :bands, :through => :event_bands
belongs_to :venue

band.rb

has_many :event_bands
has_many :events, :through => :event_bands

event_bands.rb

belongs_to :event
belongs_to :band

最近生成的sql:

Processing EventlistController#recent (for 127.0.0.1 at 2009-03-27 14:58:10) [GET]
  [4;35;1mSQL (0.0ms)[0m   [0mSET SQL_AUTO_IS_NULL=0[0m
  [4;36;1mEvent Columns (16.0ms)[0m   [0;1mSHOW FIELDS FROM `events`[0m
  [4;35;1mVenue Columns (15.0ms)[0m   [0mSHOW FIELDS FROM `venues`[0m
  [4;36;1mBand Columns (16.0ms)[0m   [0;1mSHOW FIELDS FROM `bands`[0m
  [4;35;1mEvent Load Including Associations (31.0ms)[0m   [0mSELECT `events`.`id` AS t0_r0, `events`.`is_local` AS t0_r1, `events`.`link` AS t0_r2, `events`.`notes` AS t0_r3, `events`.`band_id` AS t0_r4, `events`.`venue_id` AS t0_r5, `events`.`created_at` AS t0_r6, `events`.`updated_at` AS t0_r7, `events`.`day` AS t0_r8, `events`.`band_list` AS t0_r9, `venues`.`id` AS t1_r0, `venues`.`name` AS t1_r1, `venues`.`link` AS t1_r2, `venues`.`map` AS t1_r3, `venues`.`notes` AS t1_r4, `venues`.`created_at` AS t1_r5, `venues`.`updated_at` AS t1_r6, `bands`.`id` AS t2_r0, `bands`.`name` AS t2_r1, `bands`.`link` AS t2_r2, `bands`.`notes` AS t2_r3, `bands`.`is_local` AS t2_r4, `bands`.`created_at` AS t2_r5, `bands`.`updated_at` AS t2_r6 FROM `events` LEFT OUTER JOIN `venues` ON `venues`.id = `events`.venue_id LEFT OUTER JOIN `event_bands` ON (`events`.`id` = `event_bands`.`event_id`) LEFT OUTER JOIN `bands` ON (`bands`.`id` = `event_bands`.`band_id`) WHERE (`events`.`updated_at` BETWEEN '2009-03-20 14:58:10' AND '2009-03-27 14:58:10')

由main生成的SQL

Processing EventlistController#index (for 127.0.0.1 at 2009-03-27 14:15:47) [GET]
  [4;35;1mSQL (0.0ms)[0m   [0mSET SQL_AUTO_IS_NULL=0[0m
  [4;36;1mEvent Load (0.0ms)[0m   [0;1mSELECT * FROM `events` ORDER BY events.day[0m
  [4;35;1mEvent Columns (15.0ms)[0m   [0mSHOW FIELDS FROM `events`[0m
  [4;36;1mVenue Columns (16.0ms)[0m   [0;1mSHOW FIELDS FROM `venues`[0m
  [4;35;1mVenue Load (0.0ms)[0m   [0mSELECT * FROM `venues` WHERE (`venues`.`id` IN (6,7,8,9,1,2,3,4,5)) [0m
  [4;36;1mEventBand Load (0.0ms)[0m   [0;1mSELECT `event_bands`.* FROM `event_bands` WHERE (`event_bands`.event_id IN (1,24,74,2,75,11,12,3,13,129,76,14,77,78,145,4,146,120,15,130,79,147,5,80,148,144,131,81,132,16,28,82,121,133,83,17,134,84,149,6,18,135,85,122,19,7,136,86,20,9,87,25,137,150,22,138,88,151,124,123,139,89,152,140,90,141,91,8,153,21,142,154,125,143,92,126,10,93,94,155,127,95,128,23,96,97,26,98,99,27,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119)) [0m
  [4;35;1mEventBand Columns (0.0ms)[0m   [0mSHOW FIELDS FROM `event_bands`[0m
  [4;36;1mBand Columns (16.0ms)[0m   [0;1mSHOW FIELDS FROM `bands`[0m
  [4;35;1mBand Load (0.0ms)[0m   [0mSELECT * FROM `bands` WHERE (`bands`.`

1 个答案:

答案 0 :(得分:3)

他们生成两个不同的SQL查询的原因是,默认情况下,Rails尝试优化SQL查询以减少连接。当您在查找中包含需要使用其他表的条件列表时,当然结果SQL必须包含连接。但是,如果你要做的就是预先填充与你正在检索的对象有关系的对象,那么Rails认为执行几个快速查询而不是一个大的慢查询会更有效。

请注意,这不会导致N + 1问题,因为Rails会尝试在单个查询中加载所有关联的记录。