Module: FastCount::Extensions::ModelExtension
- Defined in:
- lib/fast_count/extensions.rb
Instance Method Summary collapse
-
#fast_count(threshold: FastCount.threshold) ⇒ Object
Returns an estimated number of rows in the table.
-
#fast_distinct_count(column:) ⇒ Object
Returns an exact number of distinct values in a column.
Instance Method Details
#fast_count(threshold: FastCount.threshold) ⇒ Object
Returns an estimated number of rows in the table. Runs in milliseconds.
13 14 15 16 |
# File 'lib/fast_count/extensions.rb', line 13 def fast_count(threshold: FastCount.threshold) adapter = Adapters.for_connection(connection) adapter.fast_count(table_name, threshold) end |
#fast_distinct_count(column:) ⇒ Object
Returns an exact number of distinct values in a column. It is suited for cases, when there is a small amount of distinct values in a column compared to a total number of values (for example, 10M rows total and 500 distinct values).
Runs orders of magnitude faster than ‘SELECT COUNT(DISTINCT column) …’.
Note: You need to have an index starting with the specified column for this to work.
Uses an “Loose Index Scan” technique (see wiki.postgresql.org/wiki/Loose_indexscan).
33 34 35 36 37 38 39 40 |
# File 'lib/fast_count/extensions.rb', line 33 def fast_distinct_count(column:) if column.to_s == primary_key raise "Use `#fast_count` when counting primary keys." end adapter = Adapters.for_connection(connection) adapter.fast_distinct_count(table_name, column) end |