在rails sql查询上优化ruby以进行预先加载

时间:2014-08-05 16:34:01

标签: sql ruby-on-rails

我有以下表格strcuture的情况:

a = table_a.includes(...{set of tables referenced, nested}...).where(key = (:key),:key => key_list).all

a.ref_tableB.ref_tableC.each do |x1| #ref tableB and C are part of includes for eager loading

      x2 = tableD.where('id1 = ? and id2 = ?',x1[:id],a[:id]) # Need to optimize this and eliminate where

      x2.each do |x3|  

我试图利用tableD可以这样引用的事实

   a.ref_tableB.ref_tableD.where('id1=?',x1[:id]) #this works but there is no decrease in the number of queries possible i need to eliminate the where clause

但我需要在哪里满足这些条件。总而言之,我需要一种更好的方式来表示where条件,以便我可以使用预先加载关联来减少查询数量

如果有些事情不清楚会很高兴澄清它。

0 个答案:

没有答案