railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.9k stars 2.26k forks source link

Dashboard counting strategy (timeout error) #3159

Open Sergezdv opened 5 years ago

Sergezdv commented 5 years ago

Using standard counting can be quite time consuming for large tables. I use Postgres, and after lowering the db plan, this is acceptable for the project, but the counting items for the Dashboard page increased slightly (for some large tables (>8kk) it now takes >15s) that in sum will lead timeout error, and in order not to turn off the statistics it would be nice to be able to change the strategy, for example, in a model I could redefine it like this:

  def self.count(arg=nil)
    can_be_approximate = !arg && !try(:to_sql)
    return super(arg) unless can_be_approximate
    ActiveRecord::Base.connection.exec_query("SELECT reltuples AS approximate_row_count FROM pg_class WHERE relname = '#{table_name}'")[0]['approximate_row_count'].to_i    
  end

under certain conditions (like mine) for some large tables this can work >100 times faster. (>15s => ~0.15s)

mshibuya commented 5 years ago

Adding such a feature can be useful, but it will be a bit hard to implement it in a RDBMS-agnostic manner.