Mongodb性能问题与简单的搜索查询

时间:2014-02-22 18:15:08

标签: c# .net mongodb

这是我在mongodb中拥有的一个集合的示例结构,同样在一个集合中有大约60多万个类似的记录。我们转移到mongodb看到快速访问性能,但现在有点难以说evean简单搜索需要花费大量时间。我给了搜索参数索引。用于搜索名称(搜索参数号MNG_FIRST_NAME,MNG_MIDDLE_NAME,MNG_LAST_NAME,MNG_ALIASIST.AKAFIRST_NAME,MNG_ALIASIST.AKALAST_NAME)。 请考虑我使用mongodb正确遵循的方法。

请找到我正在使用的代码示例和集合结构。

{
    "_id" : ObjectId("5308e2e1f525bc0f0472cbf6"),
    "MNG_UID" : "1389120",
    "MNG_CATEGORY" : "OFFICER",
    "MNG_FIRST_NAME" : "JOHN",
    "MNG_LAST_NAME" : "DOE",
    "MNG_MIDDLE_NAME" : "MARK",
    "MNG_CREATEDON" : "2010-11-19",
    "MNG_UPDATEDON" : "2014-01-01",
    "MNG_TITLE" : "Dr",
    "MNG_REMARKS" : "",
    "MNG_TYPE" : "WORLDCHECK",
    "MNG_PROGRAMLIST" : [],
    "MNG_IDLIST" : [],
    "MNG_ALIASIST" : [ 
        {
            "AKAUID" : "101",
            "AKATYPE" : "ALIAS",
            "AKACATEGORY" : "",
            "AKAFIRST_NAME" : "JOHNNY TURA, DOE",
            "AKALAST_NAME" : ""
        },
        {
            "AKAUID" : "102",
            "AKATYPE" : "ALIAS",
            "AKACATEGORY" : "",
            "AKAFIRST_NAME" : "MARK TURA, DOE",
            "AKALAST_NAME" : ""
        }
    ],
    "MNG_ADDRESSLIST" : [ 
        {
            "ADDRESSUID" : "",
            "ADDRESS1" : "Dakar",
            "ADDRESS2" : "Dakar",
            "ADDRESS3" : "SENEGAL",
            "ADDRESSCITY" : "Dakar",
            "ADDRESSCOUNTRY" : "SENEGAL",
            "ADDRESSPOCODE" : "",
            "ADDRESSSTATE" : "Dakar"
        }
    ],
    "MNG_NATIONALITYLIST" : [ 
        {
            "NATUID" : "",
            "NATCOUNTRY" : "ISRAEL",
            "NATMAINENTRY" : ""
        },
    {
            "NATUID" : "",
            "NATCOUNTRY" : "AMERICIAN",
            "NATMAINENTRY" : ""
        }
    ],
    "MNG_CITIZENSHIPLIST" : [],
    "MNG_DOBLIST" : [],
    "MNG_PLACEOBLIST" : [],
    "MNG_ADDINFOLIST" : []
}

用于使用mongodb c#driver获取集合的代码

    string MNG_REMTNAME = "JOHN";

    var collection = mongoDB.GetCollection<EMPLOYEES>("EMPLOYEES");

    if (!string.IsNullOrEmpty(MNG_REMTNAME))
    {
       IMongoQuery MFName = new QueryDocument();
       MFName = Query.Or(Query.Matches("MNG_FIRST_NAME", MNG_REMTNAME.ToUpper()),
                Query.Matches("MNG_MIDDLE_NAME", MNG_REMTNAME.ToUpper()),
                Query.Matches("MNG_LAST_NAME", MNG_REMTNAME.ToUpper()),
                Query.ElemMatch("MNG_ALIASIST", Query.Or(Query.Matches("AKAFIRST_NAME",
MNG_REMTNAME.ToUpper())))
                );
    }
    var colln = collection.Find(MFName).ToList();

请提供一些解决方案,以提高效果。

1 个答案:

答案 0 :(得分:0)

更改行

Query.ElemMatch("MNG_ALIASIST", Query.Or(Query.Matches("AKAFIRST_NAME",
MNG_REMTNAME.ToUpper())))

这个

Query.ElemMatch("MNG_ALIASIST", Query.EQ("AKAFIRST_NAME", MNG_REMTNAME.ToUpper()))

另外请注意,除非您拥有正确的索引,否则在MongoDB阵列中进行搜索非常昂贵。因此,请确保您的索引至少包含字段MNG_ALIASIST

考虑为此字段http://docs.mongodb.org/manual/core/index-multikey/

创建多键索引