1

我正在彙總有100萬條記錄的集合上的數據。 匹配查詢使用索引。 找到下面的代碼參考 -對遊標時間限制沒有影響aggrgate mongoDB

AggregateIterable<Document> aggregateIterable = timeCollection.aggregate(Arrays.asList(match, project,group)).batchSize(1000).allowDiskUse(true); 
    long curStartTs = Calendar.getInstance().getTimeInMillis(); 
    MongoCursor<Document> cursor = aggregateIterable.iterator(); //this line roughly takes 15 seconds 
    long curEndTs = Calendar.getInstance().getTimeInMillis(); 
    System.out.println("Cursor time - " + (curEndTs - curStartTs)); 

最終的結果列表中包含3500條記錄。

現在我在總管道傳遞$限制爲限制記錄 -

Document limitParam = new Document("$limit",30); 
    AggregateIterable<Document> aggregateIterable = timeCollection.aggregate(Arrays.asList(match, project,group,limitParam)).batchSize(1000).allowDiskUse(true); 
    long curStartTs = Calendar.getInstance().getTimeInMillis(); 
    MongoCursor<Document> cursor = aggregateIterable.iterator(); //this line still taking around 15 seconds 
    long curEndTs = Calendar.getInstance().getTimeInMillis(); 
    System.out.println("Cursor time - " + (curEndTs - curStartTs)); 

最終的結果列表現在只包含30條記錄。

我無法理解,爲什麼在兩種情況下沒有時間變化。 即使在管道中提供了限制之後,爲什麼aggregateIterable.iterator()與管道中沒有限制時的情況相同?

非常感謝。

親切的問候,

Vibhav

回答

1

Aggregation $limit有它通過文件的內容沒有影響。

通過看你的代碼

long curStartTs = Calendar.getInstance().getTimeInMillis(); 
MongoCursor<Document> cursor = aggregateIterable.iterator(); //this line roughly takes 15 seconds 
long curEndTs = Calendar.getInstance().getTimeInMillis(); 
System.out.println("Cursor time - " + (curEndTs - curStartTs)); 

你正在努力尋找只是執行查詢的時間。

要獲得多少時間實際採取在MongoDB中執行這些疑問,我們可以用explain

樣品執行在蒙戈外殼相同的查詢一個更好的主意查詢

無極限

db.foo.aggregate([ { 'conditions' }], {explain: true}) 

有限制

db.foo.aggregate([{ 'conditions' }, {$limit: 10}], {explain: true}) 

而且你可能需要把眼光放在Performance of MongoDB queryOptimize QueryAnalyze Query Plancursor limit

希望它能幫助!

+0

Thanks @Clement。這非常有幫助。 –