charlotte-ruby / impressionist

Rails Plugin that tracks impressions and page views
MIT License
1.54k stars 313 forks source link

Performance issues using counter_cache #192

Open BenevidesLecontes opened 9 years ago

BenevidesLecontes commented 9 years ago

i'm using impressionist to show most viewed videos on my app, on my model i have: is_impressionable :counter_cache => true, :column_name => :views With: scope :most_viewed, ->{order('views DESC')}.

And on my controller i have @most_viewed = Task.most_viewed.limit(8)

When i load the app in a browser and list the videos, my computer go unresponsive and slow. Please if you have any advice, you're welcome. Sorry for my English.

ykshev commented 9 years ago

Guess have the similarly issue. When I tried to order by counter_cache column it works, but my ruby task takes 40% from my processor on the macbook and google chrome goes crazy.

And why when I'm using counter cache it's starting to sort my model using counter cache?

class Author < ActiveRecord::Base
    has_many :videos
    has_many :conferences, through: :videos
    acts_as_votable
    is_impressionable :counter_cache => true, :column_name => :page_views, :actions=> :show, :unique => :session_hash
    # When I commented the line above and uncommented the line below, then everything goes well
    # is_impressionable 
    scope :priority, -> {order(sum_rating: :desc)}
end
    def index
        @author = Author.priority.page(params[:page]).per(60)
    end