2011-06-06 90 views
2

使用rails 2.3.4,是否可以将提示传递给数据库优化器?我希望能够强制使用特定的索引进行测试。是否可以将提示传递给数据库?

我有(遗留代码,是它的气味):

with_exclusive_scope { 
    succeeded.average(:elapsed, 
        :conditions => ["date between ? and ? ", month_start, month_end], 
        :joins =>["LEFT OUTER JOIN update_phases proot on (updates.id=proot.update_id AND proot.phase_id=#{UpdatePhaseType.find_by_name("update").id} and proot.started_at between '#{month_start.to_s(:db)}' and '#{month_end.to_s(:db)}')", 
           "INNER JOIN profiles ON updates.profile_id = profiles.id and profiles.customer_instance_id=#{customer_instance.id}"], 
        :group =>'date', :order =>'date') 
} 

,并希望能够在force index for join(profile_date_state_activity)钉到FROM条款结束。

做不到这一点,我需要一个average_by_sql方法...

+0

您的意思是传递SELECT语句的提示,但仍然使用ActiveRecord查询语法? – Zabba 2011-06-06 19:40:58

+0

已更新,以阐明我的需求 – 2011-06-06 20:14:33

回答

1

您可以使用:从属性来修改生成的SQL语句。我不确定你的表名在这里,所以在下面的代码片段中用表名替换“table”。

with_exclusive_scope { 
    succeeded.average(:elapsed, 
        :from => "table force index for join(profile_date_state_activity)", 
        :conditions => ["date between ? and ? ", month_start, month_end], 
        :joins =>["LEFT OUTER JOIN update_phases proot on 
        etc. 

FYI在我的环境中正确生成SQL语句,并成功运行,返回预期的数据,但我没有确凿的证据,实际上是正在使用的提示。

相关问题