Class: PgCanary::Rules::UnindexedJoin

Inherits:
Base
  • Object
show all
Includes:
IndexPredicates
Defined in:
lib/pg_canary/rules/definitions/unindexed_join.rb

Overview

join-condition columns with no index leading with them. Unlike active_record_doctor's static association check, this looks at joins that actually ran, so raw-SQL joins are covered too.

Constant Summary

Constants included from IndexPredicates

IndexPredicates::TRGM_ACCESS_METHODS

Instance Method Summary collapse

Methods inherited from Base

all, check, default_enabled, enabled?, #initialize, option, options, rule_name

Constructor Details

This class inherits a constructor from PgCanary::Rules::Base

Instance Method Details

#checkObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pg_canary/rules/definitions/unindexed_join.rb', line 15

def check
  detections = []
  each_scope do |scope|
    join_columns(scope).each do |table, column|
      next unless applicable_table?(table)
      next if index_leading_with?(table, column)

      detections << detection(
        table: table,
        columns: column,
        message: "Join condition on #{table}.#{column} has no index leading with it — " \
                 "joins on unindexed columns degrade as the tables grow.",
        suggestion: <<~SUGGESTION.chomp
          Consider indexing the join column:
            CREATE INDEX index_#{table}_on_#{column} ON #{table} (#{column});
        SUGGESTION
      )
    end
  end
  detections
end