Is it possible to pass hints to database?
Using rails 2.3.4, is it possible to pass a hint to the database optimizer? I would like 开发者_如何转开发to be able to force the use of a particular index for testing purposes.
I have (legacy code, yes it smells) :
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')
}
and would like to be able to tack on force index for join(profile_date_state_activity)
to the end of the FROM
clause.
Failing that, I need an average_by_sql
method...
You can use the :from attribute to modify the SQL statement that is generated. I'm not sure what your table name is here, so in the snippet below replace "table" with that table name.
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 in my environment the SQL statement was generated correctly and ran successfully, returning expected data, but I don't have conclusive PROOF that the hint is actually being used.
精彩评论