Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created March 26, 2014 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottwater/9786545 to your computer and use it in GitHub Desktop.
Save scottwater/9786545 to your computer and use it in GitHub Desktop.
Get a rough count on the number of records in a postgres table via active record.
module EstimateCount
extend ActiveSupport::Concern
module ClassMethods
def estimate_count
sql = "SELECT reltuples FROM pg_class WHERE relname = '#{self.table_name}'"
query = self.connection.execute(sql)
if result = query.first
("%f" % result['reltuples']).to_i
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment