使用索引的Mongodb MapReduce性能

时间:2013-02-27 07:58:49

标签: mongodb

我在mongodb中有一个示例文档(我还是mongodb的新手)

{
    "ID": 0,
    "Facet1":"Value1",
    "Facet2":[
        {
            "Facet2Obj1":{
                "Obj1Facet1":"Value11",
                "Obj2Facet1":"Value21",
                "Obj3Facet1":"Value31"
            }   
        },
        {
            "Facet2Obj2":{
                "Obj1Facet2":"Value12",
                "Obj2Facet2":"Value22",
                "Obj3Facet2":"Value32"
            }
        },
        {
            "Facet2Obj3":{
                "Obj1Facet3":"Value13",
                "Obj2Facet3":"Value23",
                "Obj3Facet3":"Value33"
            }
        }
    ],
    "Facet3":"Value3"
    "Facet4":{
        "Facet4Obj1":{
            "Obj1Facet1":"Value4111"
        }
    }
}

Mapreduce有点复杂,它提供以下输出(对于30,000个文档):

{
    "_id" : "Facet1",
    "value" : [
        {
            "value" : "Value1",
            "count" : 30000,
            "ID" : [
                0,
                1,
            .
                .
                .
            ]
        }
    ]
}
{
    "_id" : "ID",
    "value" : [
        {
            "value" : 0,
            "count" : 1,
            "ID" : [
                0
            ]
        },
        {
            "value" : 1,
            "count" : 1,
            "ID" : [
                1
            ]
        },
        .
        .
        .
    ]
}
{
    "_id" : "Facet2",
    "value" : [
        {
            "value" : "Facet2Obj1",
            "count" : 30000,
            "ID" : [
                0,
                1,
                .
                .
                .
            ]
        },
        {
            "value" : "Facet2Obj2",
            "count" : 30000,
            "ID" : [
                0,
                1,
                .
                .
                .
            ]
        },
        {
            "value" : "Facet2Obj3",
            "count" : 30000,
            "ID" : [
                0,
                1,
                .
                .
                .
            ]
        }
    ]
}
{
    "_id" : "Facet3",
    "value" : [
    {
            "value" : "Value3",
        "count" : 30000,
            "ID" : [
                0,
                1,
                2,
                .
                .
                .
            ]
        }
    ]
} 
{
    "_id" : "Facet4",
    "value" : [
        {
            "value" : "Facet4Obj1",
            "count" : 30000,
            "ID" : [
                0,
                1,
                2,
                .
                .
                .
            ]
        }
    ]
}

我使用格式(使用不同的ID)将30,000个文档插入到mongodb中,然后我执行了map-reduce,但速度很慢。使用30,000个文档需要大约30分钟,但随后我将索引与facet一起变得更快一点,就像需要350秒但是有50,000个文档需要大约30分钟。当我使用db.collection.getIndexes() mongodb检查索引时,将返回此输出:

{
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "ns" : "database.collection",
    "name" : "_id_"
},
{
    "v" : 1,
    "key" : {
        "ID" : 1,
        "Facet1" : 1,
        "Facet2" : 1,
        "Facet3" : 1,
        "Facet4" : 1
    },
    "ns" : "database.collection",
    "name" : "ID_1_Facet1_1_Facet2_1_Facet3_1_Facet4_1"
}

对于索引还有什么错误吗,map-reduce仍然不够快,因为索引必须策略性地放置或性能输出相反

非常感谢答案,并提前致谢

1 个答案:

答案 0 :(得分:5)

MapReduce将集合中的每个文档传递给地图函数,除了,如果你传递它{query:}选项,它将用于“预先” - 过滤发送到MapReduce的文件。您还可以将{sort:}选项传递给mapReduce,它会将文档发送到按该字段排序的地图函数。

这是仅使用索引的两个地方 - 之后一切都发生在为工作产生的Javascript线程中。