Created
March 14, 2019 22:20
-
-
Save kamipo/4c8539f0ce4acf85075cf5a6b0d9712e to your computer and use it in GitHub Desktop.
Optimizer Hints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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