Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created March 14, 2019 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kamipo/4c8539f0ce4acf85075cf5a6b0d9712e to your computer and use it in GitHub Desktop.
Save kamipo/4c8539f0ce4acf85075cf5a6b0d9712e to your computer and use it in GitHub Desktop.
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