Created
March 26, 2014 15:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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