ruby on rails - Why this won't get faster even if I solved n + 1 issue? -
i'm using gem called "bullet" in order avoid n + 1 issue.
my previous code
@communities = community.scoped.page(params[:page]).order("created_at desc")
then getting errors
n+1 query detected community => [:platform] add finder: :include => [:platform] n+1 query detected community => [:genre] add finder: :include => [:genre] n+1 query detected community => [:tags] add finder: :include => [:tags]
then taking 650ms show page on 70 sql.
changed this
@communities = community.scoped.page(params[:page]).per(10).order("created_at desc").includes(:platform, :genre, :tags)
now, alert of bullet gone it's taking 750ms , there still on 70 sql.
why that?
this result of how long it's taking show page(w/ rack-mini-profiler)
for example, it's taking around 30ms each communities/_community
it's because of count
each communities/_community
calls these 2 helpers. reason of large number of sql here?
def topic_button(community) last_post = community.community_topics.order('last_active_at desc').first if last_post && last_post.last_active_at.today? link_to sanitize( "topics ("+ community.community_topics.count.to_s+")" , community_community_topics_path(community) end end def uploader_button(community) last_post = community.community_uploaders.order('last_active_at desc').first if last_post && last_post.last_active_at.today? link_to sanitize("chat ("+ community.community_uploaders.count.to_s+")" , community_community_uploaders_path(community) end end
update
models/community.rb
paginates_per 10
controllers/communities_controller.rb
@communities = community.scoped.page(params[:page]).order("created_at desc")
/views/communities/index.html.erb
<span class='community'> <% @communities.each |community| %> <%= render 'communities/community', :community => community %> <% end %> </span>
/views/communities/_community.html.erb
<div class="box"> <div class="list"> <p class="name"><span><%= community.title %></span></p> <%= community.community_name %> <p class="img"> <% if community.community_icon? %> <%= link_to image_tag(community.community_icon.url(:medium), :alt => community.title, :style => "width: 250px; height: 250px", :class => 'img-polaroid' ), community_path(community.community_name) %> <% end %> </p> <div class="link"> <%= platform_search(community.platform.name, community.platform_id) %> <%= genre_search(community.genre.name, community.genre_id) %> </div> <div class="intro"> <table> <tr> <th>member</th> <td class="border"><%= link_to community.cached_votes_up.to_s , bookmarker_community_path(community.community_name) %></td> </tr> <tr> <th>publisher</th> <td class="border"><%= link_to community.publisher, communities_path(:publisher => community.publisher) if !community.blank? %></td> </tr> <tr> <th class="body">body</th> <td class="border"><%= community.body.slice(0,55) if !community.blank? %></td> </tr> <tr> <th class="tag">tags</th> <td class="border"> <% community.tags.each |tag| %> <span><%= link_to tag.name, {:controller=>'communities', :action=>'index', :tag=>tag.name} %></span> <% end %> </td> </tr> </table> </div> <div class="button"> <%= topic_button(community) %> <%= uploader_button(community) %> </div> <div class="button"> <%= chat_button(community) %> <%= link_to sanitize('codes ( ' + community.codes_count.to_s + ' )', community_codes_path(community), :class => 'btn' %> </div> <div class="follow"> <span class="bookmark_community" community-id="<%= community.id %>"> <%= render :partial => "communities/bookmark", :locals => {:community => community} %> </span> </div> </div> </div>
application_helper.rb
def topic_button(community) last_post = community.community_topics.order('last_active_at desc').first if last_post && last_post.last_active_at.today? link_to sanitize("forum ("+ community.community_topics.count.to_s+")" , community_community_topics_path(community), :class => 'red_button' else link_to sanitize( "forum ("+ community.community_topics.count.to_s+")" , community_community_topics_path(community), :class => 'button' end end def uploader_button(community) last_post = community.community_uploaders.order('last_active_at desc').first if last_post && last_post.last_active_at.today? link_to sanitize("uploader ("+ community.community_uploaders.count.to_s+")" , community_community_uploaders_path(community), :class => 'red_button' else link_to sanitize("uploader ("+ community.community_uploaders.count.to_s+")" , community_community_uploaders_path(community), :class => 'button' end end def chat_button(community) if !community.comment_threads.last.nil? && community.comment_threads.last.created_at.to_date == date.current.to_date link_to sanitize('chat', chat_community_path(community), :class => 'red_button' else link_to sanitize('chat', chat_community_path(community), :class => 'button' end end
/views/communities/_bookmark.html.erb
<% if user_signed_in? %> <% if current_user.voted_up_on? community %> <%= link_to(bookmark_community_path(community), :remote => true, :class => 'button') %> <i class="icon-remove"></i> un-bookmark <% end %> <% else %> <%= link_to(bookmark_community_path(community) ,:remote => true, :class => 'blue_button') %> <i class="icon-bookmark"></i> bookmark <% end %> <% end %> <% else %> <%= link_to(bookmark_community_path(community) , :class => 'blue_button') %> <i class="icon-bookmark"></i> bookmark <% end %> <% end %>
i suggest please go through development log. find how time taken process or partial. first clear development log. , refresh page/action.
another suggestion use class variable methods current_user,user_signed_in? . these executed many times.
@current_user = current_user
and no need use current_user anywhere. replace @current_user. in development.log find difference.
similarly, more speed can use caching.
Comments
Post a Comment