Class: PgCanary::Rules::UnindexedOrderByWithLimit
- Includes:
- IndexPredicates
- Defined in:
- lib/pg_canary/rules/definitions/unindexed_order_by_with_limit.rb
Overview
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
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
#check ⇒ 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 |
# File 'lib/pg_canary/rules/definitions/unindexed_order_by_with_limit.rb', line 15 def check detections = [] each_scope do |scope| next unless scope.limited? sort_by = scope.sort_items.first next unless sort_by column_ref = sort_by.node&.unwrap next unless column_ref.is_a?(PgQuery::ColumnRef) table, column = scope.resolve(column_ref) next unless table && column next unless applicable_table?(table) next if index_leading_with?(table, column) detections << detection( 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 |