【问题标题】:no affect of limit on cursor time aggrgate mongoDB对游标时间聚合 mongoDB 的限制没有影响
【发布时间】:2016-10-03 19:23:39
【问题描述】:

我正在聚合具有 100 万条记录的集合上的数据。 匹配查询使用索引。 在下面找到代码参考 -

    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 条记录。

现在我通过在聚合管道中传递 $limit 来限制记录 -

    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

【问题讨论】:

    标签: mongodb mongodb-query limit aggregation-framework mongodb-aggregation


    【解决方案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 中执行这些查询实际花费了多少时间,我们可以在 mongo shell 中使用 explain 执行相同的查询

    示例查询

    没有限制

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

    有限制

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

    您可能还需要查看Performance of MongoDB queryOptimize QueryAnalyze Query Plancursor limit

    希望对你有帮助!

    【讨论】:

    • 谢谢@Clement。这很有帮助。!
    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 2019-05-28
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2017-11-08
    相关资源
    最近更新 更多