【问题标题】:Is it possible to pass hints to database?是否可以将提示传递给数据库?
【发布时间】:2011-06-06 18:53:25
【问题描述】:

使用 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 方法...

【问题讨论】:

  • 您的意思是传递 SELECT 语句的提示但仍使用 ActiveRecord 查询语法?

标签: ruby-on-rails ruby-on-rails-2


【解决方案1】:

您可以使用 :from 属性来修改生成的 SQL 语句。我不确定你的表名是什么,所以在下面的 sn-p 中将“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.

仅供参考,在我的环境中,SQL 语句已正确生成并成功运行,返回了预期的数据,但我没有确凿的证据证明该提示实际上正在被使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2019-12-16
    相关资源
    最近更新 更多