MongoDB find:explain(" executionStats")。executionStats.nReturned比count()更快

时间:2016-05-10 17:06:02

标签: mongodb

注意到count()的执行时间比explain(" executionStats")慢得多.repecutiveStats.nReturned也返回执行的文件数。谁能解释一下发生了什么?以下是一些例子:

// Query1: returns incorrect size() 

        db.test.tourArrivals.find({"tour.stops.locationId":locationId}).max({"scheduleInfo.stopSchedules.lastETAResult":"2016-05-04T15:29:37.9960000+02:00"}).min({"scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"}).size();

// Query2: returns correct count     

    db.test.tourArrivals.find({"tour.stops.locationId":locationId}).max({"scheduleInfo.stopSchedules.lastETAResult":"2016-05-04T15:29:37.9960000+02:00"}).min({"scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"}).explain("executionStats").executionStats.nReturned;

// Query3: Correct count but slower than Query4
db.test.tourArrivals.find({$and: [
        {"scheduleInfo.stopSchedules.lastETAResult":{$gte: "2016-05-04T15:29:10.8910000+02:00"}},
        {"scheduleInfo.stopSchedules.lastETAResult":{$lte: "2016-05-04T15:29:37.9960000+02:00"}},
        {"tour.stops.locationId":locationId}
]}).count();

// Query4: Correct count but faster than Query3
db.test.tourArrivals.find({$and: [
        {"scheduleInfo.stopSchedules.lastETAResult":{$gte: "2016-05-04T15:29:10.8910000+02:00"}},
        {"scheduleInfo.stopSchedules.lastETAResult":{$lte: "2016-05-04T15:29:37.9960000+02:00"}},
        {"tour.stops.locationId":locationId}
]}).explain("executionStats").executionStats.nReturned;

使用前两个查询,虽然使用explain(),但我确实看到正确执行了文档计数,带有count()或size()的输出是集合中所有文档的计数。

Query4比query3快得多,我无法解释原因?

我将lastETAResult字段编入索引。

以下是所有查询的执行时间:

// 10.05.2016 18:31:35
// Command #3:
// db.test.tourArrivals.find({"tour.stops.locationId":locationId}).max({"scheduleInfo.stopSchedules.lastETAResult":"2016-05-04T15:29:37.9960000+02:00"}).min({"scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"}).size();
// Execution time: 0,1s
// Result:
13629

// 10.05.2016 18:31:35
// Command #4:
// db.test.tourArrivals.find({"tour.stops.locationId":locationId}).max({"scheduleInfo.stopSchedules.lastETAResult":"2016-05-04T15:29:37.9960000+02:00"}).min({"scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"}).explain("executionStats").executionStats.nReturned;
// Execution time: 0,3s
// Result:
4638

// 10.05.2016 18:31:36
// Command #5:
// db.test.tourArrivals.find({$and: [
// {"scheduleInfo.stopSchedules.lastETAResult":{$gte: "2016-05-04T15:29:10.8910000+02:00"}},
// {"scheduleInfo.stopSchedules.lastETAResult":{$lte: "2016-05-04T15:29:37.9960000+02:00"}},
// {"tour.stops.locationId":locationId}
// ]}).count();
// Execution time: 0,8s
// Result:
4639

// 10.05.2016 18:31:36
// Command #6:
// db.test.tourArrivals.find({$and: [
// {"scheduleInfo.stopSchedules.lastETAResult":{$gte: "2016-05-04T15:29:10.8910000+02:00"}},
// {"scheduleInfo.stopSchedules.lastETAResult":{$lte: "2016-05-04T15:29:37.9960000+02:00"}},
// {"tour.stops.locationId":locationId}
// ]}).explain("executionStats").executionStats.nReturned;
// Execution time: 0,5s
// Result:
4639

0 个答案:

没有答案