2010-08-23 4 views
1

내 웹 사이트에서 내 AJAX 호출이 느리고, NewRelic RPM을보고있는 것을 발견했습니다. 대부분의 비용은 Authlogic/find에서 발생했습니다.Authlogic/find ajax 호출 비용 절감

나는 레일이 매번 (AJAX 호출에서도) 사용자를 검색해야하기 때문에, 147 마이크로 초는 너무 느린 것 같아요. 여기 또한

class LikesController < ApplicationController 
    ## commented this part out in the hopes of reducing authlogic needs, since it's simple for me to check for current_user myself. 

    # access_control do 
    # allow :admin 
    # allow logged_in, :to => [:toggle] 
    # end 
    before_filter :check_if_admin, :except => [:toggle] 

    def index 
    @likes = Like.all 
    end 

    def toggle 
    if !current_user 
     render :nothing => true and return 
    end 

    like = Like.find(:first, :conditions => "user_id = #{current_user.id} and likable_id = #{params[:likable_id]} and likable_type = '#{params[:likable_type]}'") 
    if like != nil 
     like.destroy 
     @current_user_likes_likable = false 
    else 
     like = Like.new(:likable_id => params[:likable_id], :likable_type => params[:likable_type], :user => current_user) 
     like.save 
     @current_user_likes_likable = true 
    end 
    @likable_id = params[:likable_id] 
    @likable_type = params[:likable_type] 
    #@likable = Kernel.const_get(params[:likable_type]).find(params[:likable_id]) 
    render "shared/like" 
    end 
... 

CURRENT_USER가와 ApplicationController에 정의되어 어디 :

Category Segment  % Time  Avg Calls (per Txn)  Avg Exclusive Time (ms) 
Custom  Authlogic/find  77  1.0  147 
Controller LikesController#toggle 13 1.0  25 
Database SQL - SHOW 4.9  1.5  9.3 
Database SQL - OTHER  1.1  3.7  2.1 
ActiveRecord User#find 0.8  1.0  1.5 
View application.html.haml Template 0.7  0.2  1.3 
View _like.html.haml Template 0.5  0.8  0.89 
ActiveRecord ActiveRecord::SessionStore::Session#find 0.4  1.0  0.69 

여기 내 코드의

class ApplicationController < ActionController::Base 
    helper :layout 
    helper_method :current_user_session, :current_user, :current_user_is_admin 

    private 
    def current_user_session 
     return @current_user_session if defined?(@current_user_session) 
     @current_user_session = UserSession.find 
    end 
    def current_user 
     return @current_user if defined?(@current_user) 
     @current_user = current_user_session && current_user_session.record 
    end 

답변

0

사용하고있는 데이터베이스? 느린 실행 쿼리가있는 경우 가장 먼저 보는 것은 indexes on your database입니다. find 메서드가 실행되는 SQL이 무엇인지 모르겠지만 시작하는 것이 좋습니다. 열에 올바른 색인 (둘 이상이 필요할 수도 있음)이 있는지 확인하십시오.

0

다른 앱에서도 같은 문제가 있습니다. authlogic을 사용하여 깨끗한 프로젝트를 만들었습니다. http://github.com/josei/authlogic_rpm

Newrelic이 설치되어 작동하므로 authlogic의 성능을 빠르게 확인할 수 있습니다. 당신이 볼 수 있듯이, 성능 (sqlite3 약 50ms) 꽤 괜찮습니다, 그래서 오버 헤드가 어딘가에 (어쩌면 플러그인) 있어야합니다.