【问题标题】:Speed of MongoDB $in by _id Query_id 查询 MongoDB $in 的速度
【发布时间】:2013-04-24 23:04:14
【问题描述】:

我想要一个包含 1,000 个文档的集合,我通过将 1,000 个唯一 _id 键传递给单个 find() 查询的 $in 运算符来指定它。

这比我运行 1000 个 find({_id:}) 查询快多少?

【问题讨论】:

  • 快得多。快近 1000 倍。你为什么不实际尝试并测量它?
  • 我牺牲了反对票,这样以后想知道同样问题的其他 NoSQL 新手就不必花时间来获得答案了。呵呵。

标签: performance mongodb find documents


【解决方案1】:

这样的东西应该可以帮助你

//create some data (100k documents)
for(i=0; i<=100000; i++){
  db.foo.insert({"_id":i});
}

//generate some ids to search for
var ids = [];
for(i=0; i<=1000; i++){
  ids.push(i);
}

//now let's try the two queries

//#1 - 1000 separate find queries

var timer1Start = new Date().getTime(); //start the stopwatch

ids.forEach(function(i){
  var result = db.foo.find({"_id":i});
});

var timer1End = new Date().getTime(); //stop the stopwatch

//#2 $in query
var timer2Start = new Date().getTime(); //start the stopwatch
var result = db.foo.find({"_id": {$in: ids}});
var timer2End = new Date().getTime(); //stop the stopwatch

print("Function #1 = " + (timer1Start - timer1End));
print("Function #2 = " + (timer2Start - timer2End));

【讨论】:

  • 结果,$in 快了很多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 2021-01-30
相关资源
最近更新 更多