Optimizer Hints
class ApplicationRecord < ActiveRecord::Base | |
self.abstract_class = true | |
def self.optimizer_hints(hints) | |
hints.present? ? extending(OptimizerHints.new(hints.to_s)) : all | |
end | |
class OptimizerHints < Module | |
def initialize(hints) | |
define_method(:build_arel) do |*args| | |
super(*args).tap do |arel| | |
ctx = arel.instance_variable_get(:@ctx) | |
if ctx.set_quantifier | |
ctx.set_quantifier = Arel.sql("/*+ #{hints} */ DISTINCT") | |
else | |
ctx.set_quantifier = Arel.sql("/*+ #{hints} */") | |
end | |
end | |
end | |
end | |
end | |
private_constant :OptimizerHints | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment