Class: Benedictus::Heuristics::NestedLoopBlowup

Inherits:
Base
  • Object
show all
Defined in:
lib/benedictus/heuristics/nested_loop_blowup.rb

Constant Summary collapse

DEFAULT_THRESHOLD =
10_000

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

apply

Constructor Details

#initialize(threshold: DEFAULT_THRESHOLD) ⇒ NestedLoopBlowup

Returns a new instance of NestedLoopBlowup.



12
13
14
15
# File 'lib/benedictus/heuristics/nested_loop_blowup.rb', line 12

def initialize(threshold: DEFAULT_THRESHOLD)
  super()
  @threshold = threshold
end

Class Method Details

.config_keyObject



8
9
10
# File 'lib/benedictus/heuristics/nested_loop_blowup.rb', line 8

def self.config_key
  :nested_loop_threshold
end

Instance Method Details

#apply(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/benedictus/heuristics/nested_loop_blowup.rb', line 17

def apply(node)
  return [] unless node.node_type == "Nested Loop"

  inner = node.children.find { |c| c.parent_relationship == "Inner" } || node.children.last
  return [] unless inner&.actual_loops && inner.actual_loops > @threshold

  [
    warning(
      severity: :warning,
      code: :nested_loop_blowup,
      message: "Nested Loop drove #{inner.actual_loops} iterations on the inner side.",
      suggestion: "Consider a Hash Join or Merge Join — try increasing `work_mem` or rewriting the join."
    )
  ]
end