rainforestapp/preload_counts

Name: preload_counts

Owner: Rainforest QA

Description: Preload association or named_scope counts for ActiveRecord

Forked from: marianafranco/preload_counts

Created: 2017-08-10 22:16:14.0

Updated: 2017-08-10 22:16:15.0

Pushed: 2017-05-09 14:22:33.0

Homepage: https://github.com/smathieu/pleload_counts

Size: 21

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build Status

Preload Counts

Have you ever built an index page that displays the count of an association? Often this is a very expensive query, requiring database table joins. That's where preload_counts comes in: using this gem will simplify those lookups into a single optimized query.

Consider the following example:

dels
s Post < ActiveRecord::Base
s_many :comments


s Comment < ActiveRecord::Base
longs_to :post

ope :by_moderators, -> { where(by_moderator: true) }


ntroller
index
osts = Post.all


ew
posts.each do |post| %>
= post.name %>
%= post.comments.count %>)
%= post.comments.by_moderators.count %>)
nd %>

This will create two SQL COUNT queries to the database, for each post each time the view is rendered. This is really slow. Preload counts helps you minimize the number of calls to the database with a simple one-line code change.

Usage
del
s Post < ActiveRecord::Base
create a named_scope to preload the comments count as well as the comments
by_moderators. 
eload_counts :comments => [:by_moderators]

preload_counts :comments would have only, preloaded the comments which
would have been slightly faster. You can preload any number of scopes, but
the more to preload, the more complex and slow the request will be.
s_many :comments


ntroller
index
Tell AR to preload the counts in the select request. If you don't specify
this, the behaviour is to fallback to the slow count.
osts = Post.preload_comment_counts


ew
u have different accessor to get to the count. Beware of methods like
mpty? that has an internal call to count.
posts.each do |post| %>
= post.name %>
%= post.comments_count %>)
%= post.by_moderators_comments_count %>)
nd %>
Installation

Using Bundler:

mfile
'preload_counts'

Manually from RubyGems.org:

m install preload_counts
Benchmarks

More in depth benchmarking is needed, but here's some annecdotal evidence:

That's right! This saved me 200ms per request, even with a very minimal code change.

Support

This gem has been sanity tested on the following Rails versions:

Help

Q. I'm using preload_counts, but my requests are still slow!

A. It's possible that this isn't the fix you need. Sometimes multiple small requests might be faster than one large request. Always benchmark your page before using this gem. Another thing you might want to look into is adding an index to speed up the query that is being generated by preload_counts.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.