Class: PgCanary::Rules::CountStarWithoutWhere
- Defined in:
- lib/pg_canary/rules/definitions/count_star_without_where.rb
Overview
SELECT COUNT(*) without WHERE. Because of MVCC, PostgreSQL has no O(1) row count — this scans the whole table.
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
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pg_canary/rules/definitions/count_star_without_where.rb', line 12 def check detections = [] each_scope do |scope| stmt = scope.stmt next if stmt.where_clause || stmt.group_clause.any? next unless count_star?(stmt) tables = scope.tables next unless tables.length == 1 table = tables.first next unless applicable_table?(table) detections << detection( table: table, message: "COUNT(*) without WHERE scans the whole #{table} table — PostgreSQL's MVCC " \ "has no O(1) row count.", suggestion: <<~SUGGESTION.chomp If an approximation is acceptable, use the planner's estimate: SELECT reltuples::bigint FROM pg_class WHERE relname = '#{table}'; For exact counts displayed frequently, maintain a counter cache. SUGGESTION ) end detections end |