Sha256: e626a56f0797d9ab1b6725ef5e0391f74d6e6c6eab9a2d09d17f1eb6db0e497f

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

$LOAD_PATH.unshift File.expand_path('../test', __FILE__)

require 'benchmark/ips'
require 'cases/db_config'
require 'models/favorite'
require 'models/tweet'

join_relation = Tweet.joins('LEFT JOIN favorites ON tweets.id = favorites.tweet_id').
  select('tweets.*, COUNT(favorites.id) AS joined_count').group('tweets.id')

def prepare_records(tweets_count, favorites_count)
  Tweet.delete_all
  Favorite.delete_all

  tweets_count.times do
    t = Tweet.create(favorites_count_cache: 0)
    favorites_count.times { Favorite.create(tweet: t) }
  end
end

test_cases = [
  [30, 100],
]

test_cases.each do |tweets_count, favorites_count|
  prepare_records(tweets_count, favorites_count)

  puts "N = #{tweets_count}, count = #{favorites_count}"
  Benchmark.ips do |x|
    x.report('counter_cache')    { Tweet.all.map(&:favorites_count_cache) }
    x.report('LEFT JOIN')        { Tweet.joins('LEFT JOIN favorites ON tweets.id = favorites.tweet_id').
                                   select('tweets.*, COUNT(favorites.id) AS joined_count').
                                   group('tweets.id').map(&:joined_count) }
    x.report('eager_count')      { Tweet.eager_count(:favorites).map(&:favorites_count) }
    x.report('precount')         { Tweet.precount(:favorites).map(&:favorites_count) }
    x.report('slow eager_count') { Tweet.eager_count(:favorites).map { |t| t.favorites.count } }
    x.report('slow precount')    { Tweet.precount(:favorites).map { |t| t.favorites.count } }
    x.report('preload')          { Tweet.preload(:favorites).map{ |t| t.favorites.size } }
    x.report('N+1 COUNT')        { Tweet.all.map{ |t| t.favorites.count } }
    x.compare!
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
activerecord-precount-0.7.0 benchmark.rb
activerecord-precount-0.7.0.beta1 benchmark.rb
activerecord-precount-0.6.3 benchmark.rb
activerecord-precount-0.6.2 benchmark.rb
activerecord-precount-0.6.1 benchmark.rb
activerecord-precount-0.6.0 benchmark.rb