Ruby on rails Spree扩展中获取关键字分类单元的查询性能较慢

Ruby on rails Spree扩展中获取关键字分类单元的查询性能较慢,ruby-on-rails,ajax,performance,spree,select2-rails,Ruby On Rails,Ajax,Performance,Spree,Select2 Rails,我正在为现有的Spree定制照片打印商店添加功能,允许摄影师上传他们的作品集并通过网站出售照片。我使用Spree::Taxon(s)创建了一个select2文本字段,用于向产品添加关键字,效果很好。我有用于添加网站支持的每种语言(英语和法语)的关键字的字段 但是,ajax查询需要非常长的时间才能完成(平均5-15秒)。ActiveRecord查询需要5-150毫秒才能完成,视图渲染不超过60毫秒。我无法解释剩下的装载时间。有没有人对加快返回结果有什么建议,或者在需要额外时间才能完成的情况下有什么

我正在为现有的Spree定制照片打印商店添加功能,允许摄影师上传他们的作品集并通过网站出售照片。我使用Spree::Taxon(s)创建了一个select2文本字段,用于向产品添加关键字,效果很好。我有用于添加网站支持的每种语言(英语和法语)的关键字的字段

但是,ajax查询需要非常长的时间才能完成(平均5-15秒)。ActiveRecord查询需要5-150毫秒才能完成,视图渲染不超过60毫秒。我无法解释剩下的装载时间。有没有人对加快返回结果有什么建议,或者在需要额外时间才能完成的情况下有什么建议

数据库使用MySQL、Ruby 2.2.1和Rails 4.2.1。我的开发环境是:MacMini(8GBRAM,HDD),AptanaStudio IDE,在本地主机上运行的服务器:3000

请不要犹豫,要求更多的澄清信息!我不确定我到底需要发布什么来帮助解决我的问题

要为ajax请求返回JSON的控制器:


事实证明,获取查询结果的速度很慢,因为我在开发环境中。在生产中,它以人们预期的速度工作。我发布这个答案,以防其他人有同样的问题

class KeywordTagsController < Spree::StoreController
  respond_to :json
  def find_keywords
    term = params[:q]
    Rails.logger.debug params.inspect
    if params[:locale_str]
      query = []
      query = ["spree_taxons.name like ?", term + '%'] if term

      locale = params[:locale_str].to_sym
      keyword_taxonomy = Spree::Taxonomy.find_by(name: Spree.t("pi-taxonomy-keyword"))
      keyword_taxons = keyword_taxonomy.taxons.with_translations(locale).where(query).order('name asc').select{ |t| t.parent_id != nil} if locale
      respond_with keyword_taxons if keyword_taxons
    end
  end

end
$("#keywords_en").select2({
          createSearchChoice: function(term, data) {
             if ($(data).filter(function() {
                return this.text.localeCompare(term) === 0;
                }).length === 0) {
                    return {
                        id: term,
                        text: term
                    };
                }
          },
          multiple: true,
          ajax: {
              url: '/keywords/en',
              dataType: 'json',
              data: function (params) {
                        return {
                            q: params // search term
                        };
              },
              results: function(data){
                  return { results: $.map( data, function (keyword, i) {
                      return {id: keyword.id, text: keyword.name }
                  })}
              }
          },
          tags: true,
          tokenSeparators: [','],
          placeholder: '<%= Spree.t('pi-keywords-placeholder-en') %>',
          initSelection: function (element, callback) {
            var data = [];

            function splitVal(string, separator) {
                var val, i, l;
                if (string === null || string.length < 1) return [];
                val = string.split(separator);
                for (i = 0, l = val.length; i < l; i = i + 1) val[i] = $.trim(val[i]);
                return val;
            }

            $(splitVal(element.val(), ",")).each(function () {
                data.push({
                    id: this,
                    text: this
                });
            });

            callback(data);
         }
        });
15:00:51 INFO: Processing by KeywordTagsController#find_keywords as JSON 
15:00:51 INFO:   Parameters: {"q"=>"", "_"=>"1436986845195", "locale_str"=>"fr"} 
15:00:54 INFO: Completed 200 OK in 2870ms (Views: 40.6ms | ActiveRecord: 5.2ms) 
15:33:45 INFO: Started GET "/keywords/fr?q=mer&_=1436986845196" for 127.0.0.1 at 2015-07-15 15:33:45 -0400 
15:33:48 INFO: Processing by KeywordTagsController#find_keywords as JSON 
15:33:48 INFO:   Parameters: {"q"=>"mer", "_"=>"1436986845196", "locale_str"=>"fr"} 
15:33:50 INFO: Completed 200 OK in 2136ms (Views: 5.4ms | ActiveRecord: 113.4ms) 
15:33:58 INFO: Started GET "/keywords/fr?q=&_=1436986845197" for 127.0.0.1 at 2015-07-15 15:33:58 -0400 
15:33:58 INFO: Processing by KeywordTagsController#find_keywords as JSON 
15:33:58 INFO:   Parameters: {"q"=>"", "_"=>"1436986845197", "locale_str"=>"fr"} 
15:34:00 INFO: Completed 200 OK in 1885ms (Views: 38.7ms | ActiveRecord: 4.6ms)