2010-08-23 69 views
1

我注意到我的网站上的AJAX调用速度很慢,看着NewRelic RPM我注意到大部分的成本来自Authlogic/find降低authlogic/ajax调用的成本

我想这是因为rails需要每次查看用户(即使在AJAX调用中),但147微秒似乎非常慢。

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 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中定义:

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),所以开销必须在别的地方(也许是一个插件)。