Ruby on rails 3 使用rails和mongo提高查询性能

Ruby on rails 3 使用rails和mongo提高查询性能,ruby-on-rails-3,performance,mongodb,mongomapper,Ruby On Rails 3,Performance,Mongodb,Mongomapper,假设我有两个模型: class Radar include MongoMapper::Document plugin MongoMapper::Plugins::IdentityMap key :name, String, :required => true key :slug, String, :required => true many :stores, index: true def self.retrieve_radars_and_st

假设我有两个模型:

class Radar
  include MongoMapper::Document
  plugin MongoMapper::Plugins::IdentityMap

  key :name,    String, :required => true
  key :slug,    String, :required => true
  many :stores, index: true

  def self.retrieve_radars_and_stores
    radars = Radar.all
    radars.map { |r|
      {
        name:   r.name,
        ip:     r.ip,
        stores: r.serializable_stores
      }
    }
  end

  def serializable_stores
    stores.map { |store|
      {
        name:     store.try(:name),
        location: store.try(:location)
      }
    }
  end
end

class Store
  include MongoMapper::Document

  key :name, String,     :required => true
  key :location, String, :required => true

  ensure_index :name
  ensure_index :location
end
因此,我有一个控制器方法调用
Radar.retrieve\u radars\u和\u stores
,得到结果并作为json返回

该代码工作得很好,但是,它有超过20000条记录,它花费大约1分钟来处理该方法。注释掉
存储:r.serializable_存储
行,该方法只需几秒钟

我的问题是,如何改进此查询,减少所用时间


谢谢

问题不在于查询性能,而在于将20k个文档加载到内存中。您需要一次加载所有这些文件吗?分页是一种选择吗?当然。。分页是一个好的选择吗。。然而,我很好奇,如果不解决它,我如何改进它……)你不能。您的查询执行得很好,因为您正在执行“全部查找”。