具有时间条件的mongoid / mongodb查询非常慢

时间:2015-01-09 15:55:01

标签: mongodb ruby-on-rails-4 mongoid

我想选择ts(时间戳)小于特定时间的所有数据

last_record = History.where(report_type: /#{params["report_type"]}/).order_by(ts: 1).only(:ts).last
History.where(:ts.lte => last_record.ts)

这个查询似乎需要超长时间

我不明白为什么,有没有快速的方法来进行这种查询?

class History
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Attributes::Dynamic
  field :report_type, type: String
  field :symbol, type: String
  field :ts, type: Time
end

控制台中的查询日志

Started GET "/q/com_disagg/last" for 127.0.0.1 at 2015-01-10 10:36:55 +0800
Processing by QueryController#last as HTML
  Parameters: {"report_type"=>"com_disagg"}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.4290ms
    ...  
  MOPED: 127.0.0.1:27017 GET_MORE     database=cot_development collection=histories limit=0 cursor_id=44966970901 runtime: 349.9560ms

已将时间戳设置为索引,但查询仍然非常慢

  

db.system.indexes.find()

{ "v" : 1, "key" : { "ts" : 1 }, 
"name" : "ts_index",
 "ns" : "cot_development.histories" }

0 个答案:

没有答案