1

1万レコードのコレクションにデータを集計しています。 一致クエリはインデックスを使用します。最終結果のリストには3500件のレコードを含むカーソル時間の制限に影響なし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)); 

- 以下のコードを参照して下さい。最終結果のリストが今だけ30レコードが含まれている

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)); 

-

は今、私はとして集計パイプラインで$限度を渡すことによって、レコードを制限しています。

私は、2つの場合に時間変動がない理由を理解できません。 パイプラインに制限を設けた後でも、パイプラインに制限がない場合、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)); 

を見て

は、あなただけのクエリの実行にかかる時間を見つけようとしています。我々はexplain

サンプルでのmongoシェルで同じクエリを実行することができますどのくらい実際にこれらのクエリを実行するためにMongoDBにかかる時間の良いアイデアがリミット

なければ

を問い合わせ取得するために

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

制限あり

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

また、あなたがOptimize QueryPerformance of MongoDB queryに見える必要があるかもしれません、Analyze Query Plancursor limit

はそれが役に立てば幸い!

+0

ありがとう@Clement。これは非常に役に立ちました。 –

関連する問題