I'm totaling the rows in a one of my models using Model.count
and am a bit concerned about performance, as eventually, this model will get very large. Is there a way to use counter_cache
w/o the :belongs_to relationship? Or another performance-friendly way of counting the rows? I thought about making another model, just one where I store calculations like this but not sure that's the best way.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- Is there a way to remove IDV Tags from an AIFF fil
- Rails how to handle error and exceptions in model
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
- Rspec controller error expecting <“index”> but
- Rspec controller error expecting <“index”> but
Take a look at http://guides.rubyonrails.org/caching_with_rails.html Specifically, you'll want to take a look at the section regarding cache stores. Using cache stores, you can store values into cache for arbitrary things.
For example, you could have a method called on the Model called get_count which would be filled initially by the count but incremented by 1 with an after_create callback. If it's not necessary to keep it up to date, you can update this every x minutes so that you're mostly accurate.
I personally use memcache as a store for things like this. Just make sure you keep the cache up to date according to your needs.
Even more trivial than making a
Cache
model is to just useRails.cache
.Rails uses a file store by default (tmp/cache).
Then you could just place a Rails.cache.write increment and decrement into your model's
after_create
andafter_destroy
hooks, and overrideModel.size
with a call to Rails.cache.read.You could initialize the cache whenever Rails first initializes by placing a file named something like
initialize_cache.rb
in config/initializers containing:If you want to have a maintained counter at all, whether using
counter_cache
or doing it manually, Rails will maintain your counters using callbacks, which will increase/decrease the counter when a new descendant is created/destroyed.I am not aware of a means to store a
counter_cache
without using thebelongs_to
relationship, because only the parent can store the count of the children.Weighing Performance
If your table is going to get 'large', populate your test database with a large number of rows then start running some SQL queries using
EXPLAIN
to get the performance of your database queries. See if the performance hit in doing record creation/destruction withcounter_cache
is offset by how often you need to access these counters in the first place.If the counter does not need to be 100% accurate at all times, you can instead update the caches periodically using a
cron
job or background worker.In summary:
counter_cache
vs a manual alternative that uses callbacks is, as far as I am aware, unlikely to result in much of a detriment to performance.