具有时间条件的mongoid/mongodb查询速度非常慢
我想选择ts(时间戳)小于特定时间的所有数据具有时间条件的mongoid/mongodb查询速度非常慢,mongodb,ruby-on-rails-4,mongoid,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
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
查询登录控制台
已将时间戳设置为索引,但查询速度仍然非常慢
db.system.index.find()
也许ts没有索引?是的,
ts没有索引
我稍后会添加它,但不会太慢,就是时间。现在是函数吗?返回时间(每次比较)。这也可能会减慢速度(可能会复制值或使用常量进行测试)。@Matthias请查看我的更新您需要ts
上的索引;否则,每次查询都会扫描整个集合。
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
{ "v" : 1, "key" : { "ts" : 1 },
"name" : "ts_index",
"ns" : "cot_development.histories" }