Class: PgCanary::Rules::UnindexedOrderByWithLimit
- Includes:
- IndexPredicates
- Defined in:
- lib/pg_canary/rules/definitions/unindexed_order_by_with_limit.rb
Overview
Tier 2 (opt-in): ORDER BY x LIMIT n without an index led by x forces a full sort before the limit can apply. Size-dependent, so disabled by default.
Constant Summary
Constants included from IndexPredicates
IndexPredicates::TRGM_ACCESS_METHODS
Constants included from PgQuerySupport
PgQuerySupport::COMPARISON_OPS
Instance Method Summary collapse
Methods inherited from Base
all, #enabled?, options, rule_name
Instance Method Details
#check(query) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pg_canary/rules/definitions/unindexed_order_by_with_limit.rb', line 15 def check(query) detections = [] query.each_scope do |scope| next unless scope.limited? sort_by = scope.sort_items.first next unless sort_by column_ref = unwrap_node(sort_by.node) next unless column_ref.is_a?(PgQuery::ColumnRef) table, column = scope.resolve(column_ref) next unless table && column next unless applicable_table?(query, table) next if index_leading_with?(query, table, column) detections << detection( query, table: table, columns: column, message: "ORDER BY #{column} LIMIT sorts every row before the limit can apply " \ "(no index on #{table} leads with #{column}).", suggestion: <<~SUGGESTION.chomp Consider indexing the sort key: CREATE INDEX index_#{table}_on_#{column} ON #{table} (#{column}); SUGGESTION ) end detections end |
#default_enabled ⇒ Object
11 12 13 |
# File 'lib/pg_canary/rules/definitions/unindexed_order_by_with_limit.rb', line 11 def default_enabled false end |