Class: Benedictus::Heuristics::RowEstimateDrift

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

Constant Summary collapse

DEFAULT_FACTOR =
10

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

apply

Constructor Details

#initialize(threshold: DEFAULT_FACTOR) ⇒ RowEstimateDrift

Returns a new instance of RowEstimateDrift.



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

def initialize(threshold: DEFAULT_FACTOR)
  super()
  @factor = threshold
end

Class Method Details

.config_keyObject



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

def self.config_key
  :drift_factor
end

Instance Method Details

#apply(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/benedictus/heuristics/row_estimate_drift.rb', line 17

def apply(node)
  return [] unless node.actual_rows && node.plan_rows
  return [] if node.actual_rows.zero? && node.plan_rows.zero?

  actual = node.actual_rows
  plan   = [node.plan_rows, 1].max
  ratio  = [actual.to_f / plan, plan.to_f / [actual, 1].max].max

  return [] if ratio < @factor

  relation = node.relation_name
  suggestion = relation ? "Run `ANALYZE #{relation}` to refresh planner statistics." : nil

  [
    warning(
      severity: :warning,
      code: :row_estimate_drift,
      message: "Plan estimated #{plan} rows, actual was #{actual} (#{ratio.round(1)}x drift).",
      suggestion: suggestion
    )
  ]
end