【问题标题】:MongoDB map-reduce slow and runs out of memoryMongoDB map-reduce 速度慢并且内存不足
【发布时间】:2023-03-08 06:55:01
【问题描述】:

我想使用 MongoDB 作为我正在构建的分析系统的后端。 使用 MongoDB 的主要优点之一是内置的 map reduce。 由于我们处于“中等数据”规模,我们还不需要 Hadoop 的开销。

出于测试目的,我插入了 5000 万行该类型

{
 user_id: xxxx,
 thing_id:xxxx,
 time: xxx
}

使用 EC2 大型实例上的 user_id 索引。它是一个单实例 mongodb(未分片)。

db.user_thing_like.find({user_id: 37104857}) 

不到一秒。

但是,我想计算用户条目数量的 mapreduce 花了整晚,并返回内存不足错误,要么我必须做一些愚蠢的事情,要么 mongo db 不是我想做的正确工具。

我是 Mongo DB 的新手,如果有任何帮助,我将不胜感激。提前致谢

错误:

Tue Aug  9 13:15:58 uncaught exception: map reduce failed:{
        "assertion" : "invoke failed: JS Error: out of memory nofile_b:2",
        "assertionCode" : 9004,
        "errmsg" : "db assertion failure",
        "ok" : 0
}

MAPREDUCE 查询:

db.user_thing_like.mapReduce(map, reduce, {out: "tmp_test"}, {query: {"user_id" : 37104857 }});

映射和减少:

map = function () {
    for (var key in this) {
        emit(key.user_id, {count: 1});
    }
};

reduce = function (key, emits) {
    total = 0;
    for (var i in emits) {
        total += emits[i].count;
    }
    return {"count": total};
}

--- 更新 ---

我意识到在我使用的语法中,mapreduce 没有考虑我的查询过滤器。

这是正确的 mapreduce 查询。

db.runCommand({mapreduce: "user_thing_like", map: map, reduce: reduce, out: "tmp_test", query: {"user_id" : 37104857 }});

【问题讨论】:

    标签: mongodb mapreduce


    【解决方案1】:
    map = function () {
            emit(this.user_id, {count: 1});
        }
    };
    

    另外,请尝试在手册中指定 user_id 作为 MapReduce 的排序键:

    sort : <sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces>]
    

    【讨论】:

    • 已解决。 db.runCommand({mapreduce: "user_thing_like", map: map, reduce: reduce, out: "tmp_test", query: {"user_id" : 37104857 }});
    【解决方案2】:

    我意识到在我使用的语法中,mapreduce 没有考虑我的查询过滤器。

    这是正确的 mapreduce 查询。

    db.runCommand({mapreduce: "user_thing_like", map: map, reduce: reduce, out: "tmp_test", query: {"user_id" : 37104857 }});
    

    【讨论】:

    • 这种评论应该包含在原始问题中,而不是作为问题的答案。我将这些 cmets 移到了你的问题中。
    猜你喜欢
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多