C# MongoDb对C的响应较慢#

C# MongoDb对C的响应较慢#,c#,mongodb,azure,C#,Mongodb,Azure,我最近一直在努力建立一个Mongo db服务器,作为一个网站的对象缓存。然而,在设置完所有内容后,我担心性能相当缓慢 它是单个服务器,不是副本集或分片的一部分。我正在运行从本地计算机到Windows Azure VM上的服务器的所有测试 例如,我有一个大约5500个文档的集合,每个文档存储一个指向外部站点的链接。典型的文档如下所示: { "_id" : 5001 , "Active" : true , "CategoryId" : 1 , "Crci" :

我最近一直在努力建立一个Mongo db服务器,作为一个网站的对象缓存。然而,在设置完所有内容后,我担心性能相当缓慢

它是单个服务器,不是副本集或分片的一部分。我正在运行从本地计算机到Windows Azure VM上的服务器的所有测试

例如,我有一个大约5500个文档的集合,每个文档存储一个指向外部站点的链接。典型的文档如下所示:

{ 
    "_id" : 5001 , 
    "Active" : true , 
    "CategoryId" : 1 , 
    "Crci" : "V" , 
    "CultureId" :  null,
    "DateUpdated" : { "$date" : 1333370987810} ,
    "Description" : "National Careers Service: Childminder" ,
    "Keywords" :  null ,
    "MaxLevel" :  null ,
    "MinLevel" :  null ,
    "PhoneNumber" :  null ,
    "Priority" : 1 , 
    "Title" : "National Careers Service: Childminder" ,
    "WebUrl" : "https://nationalcareersservice.direct.gov.uk/advice/planning/jobprofiles/Pages/childminder.aspx"
}
我使用官方10gen驱动程序从代码中尝试了以下示例查询,平均耗时2.7-3.0秒:

var query = (from er in lib.All()
             where er.Id > 7000
             select er);
(lib是一个瘦包装类,All()公开iquirable接口)

上面的查询返回大约3000条记录

我已经检查了linq生成的查询,它似乎很好:

{_id: { $gt: 7000} }
从umongo(一个GUI界面)运行相同的查询会在不到一秒钟的时间内返回结果,这比我预期的要多

使用Sure index(填充集合时称为once,之后称为只读数据)对字段进行正确索引:

collection.EnsureIndex(new IndexKeysBuilder<ExternalResourceView>().Ascending(er => er.Id), IndexOptions.SetUnique(true));
collection.EnsureIndex(new IndexKeysBuilder<ExternalResourceView>().Ascending(er => er.CategoryId));
collection.EnsureIndex(new IndexKeysBuilder<ExternalResourceView>().Ascending(er => er.Active));
collection.EnsureIndex(new IndexKeysBuilder<ExternalResourceView>().Ascending(er => er.Keywords));
我是否缺少任何可以加快搜索速度的设置,或者一开始我的期望是否不切实际

编辑:我已尝试完全重建/重新填充数据库,但仍然感觉速度慢得令人无法接受

编辑2:我将包装器类全部封装起来。我不认为它有错,只是以防万一:

public abstract class MongoLibrary<TViewModel> : ILibrary<TViewModel> where TViewModel : class
{
    private readonly MongoCollection _collection;
    private readonly string _dbName;
    private readonly string _collectionName;
    private readonly string _connString;

    protected MongoLibrary(string connString, string dbName, string collectionName)
    {
        _connString = connString;
        connString += "/" + dbName;
        var client = new MongoClient(connString);
        _dbName = dbName;
        _collectionName = collectionName;
        var db = client.GetServer().GetDatabase(dbName);
        _collection = db.GetCollection<TViewModel>(collectionName);
    }

    public abstract void ConfigureIndex(MongoCollection collection);

    public IQueryable<TViewModel> All()
    {
        return _collection.AsQueryable<TViewModel>();
    }

    public IEnumerable<TViewModel> GetWhere(Func<TViewModel, bool> predicate)
    {
        return _collection.AsQueryable<TViewModel>().Where(predicate);
    }

    protected void PopulateData(IEnumerable<TViewModel> views, int instanceToUpdate)
    {
        var ports = //port numbers hidden for security

        foreach (var port in ports)
        {
            var client = new MongoClient(string.Format("{0}:{1}/{2}", _connString, port, _dbName));

            var db = client.GetServer().GetDatabase(_dbName);
            var coll = db.GetCollection(_collectionName);
            coll.Drop();
            coll.InsertBatch(views);
        }
        ConfigureIndex(_collection);
    }

}
您可以从经过身份验证的日志中看到,它在两次访问中检索了数据。总共检索了2677个文档。所用时间为3580毫秒(平均5次运行)

>db.system.profile.find().limit(10).sort({ts:-1}).pretty()
{
“op”:“getmore”,
“ns”:“cache.ExternalResources”,
“查询”:{
“_id”:{
“$gt”:7500
}
},
“光标”:数字长(“949848842778037962”),
“返回”:0,
“密钥更新”:0,
“numYield”:0,
“lockStats”:{
“timeLockedMicros”:{
“r”:长(9486)号,
“w”:数字长(0)
},
“timeAcquiringMicros”:{
“r”:长(4)号,
“w”:长(5)号
}
},
“未回复”:2576,
“响应长度”:1511874,
“米利斯”:9,
“ts”:ISODate(“2014-01-28810:52:16.125Z”),
“客户”:,
“诱惑者”:[
{
“用户”:,
“用户源”:;
}
],
“用户”:;
}
{
“op”:“查询”,
“ns”:“cache.ExternalResources”,
“查询”:{
“_id”:{
“$gt”:7500
}
},
“光标”:数字长(“949848842778037962”),
“返回”:0,
“跳过”:0,
“未扫描”:102,
“密钥更新”:0,
“numYield”:0,
“lockStats”:{
“timeLockedMicros”:{
“r”:长(749)号,
“w”:数字长(0)
},
“timeAcquiringMicros”:{
“r”:长(5)号,
“w”:长(3)号
}
},
“nreturned”:101,
“响应长度”:64013,
“毫”:0,
“ts”:ISODate(“2014-01-28810:52:15.954Z”),
“客户”:,
“诱惑者”:[
{
“用户”:,
“用户源”:;
}
],
“用户”:;
}
{
“op”:“命令”,
“ns”:“cache.$cmd”,
“命令”:{
“认证”:1,
“用户”:,
“暂时性”:;,
“关键”:;
},
"回归":一,,
“密钥更新”:0,
“numYield”:0,
“lockStats”:{
“timeLockedMicros”:{
“r”:长(381)号,
“w”:数字长(0)
},
“timeAcquiringMicros”:{
“r”:长(9)号,
“w”:长(3)号
}
},
“响应长度”:79,
“毫”:0,
“ts”:ISODate(“2014-01-28810:52:15.938Z”),
“客户”:,
“诱惑者”:[
{
“用户”:,
“用户源”:
}
],
“用户”:;
}

问题是由网络延迟引起的

而不是
。All()
,你能试试
AsQueryable()
…?我已经试过直接使用AsQueryable()了但这并没有减少时间-谢谢你的建议,尽管一切都是一样的,“lib”包装器我还是会看的,你调查过吗?如果是的话,当你运行umongo查询时,它是在那个虚拟机上还是从远程位置?只是试图限制这里的变量。@Joey-是从我的本地机器上尝试删除的尽可能多的变量。我在上面添加了包装器类,正如您所看到的All()函数只是公开现有的IQueryable接口。包装器主要在那里,因此可以向它注入连接字符串etcIt's clear时间不是花在MongoDB上的-查询以一位数毫秒运行。应用程序和数据库之间的网络是什么?
 from er in lib.All()
 where er.Id == 3100
 select er
public abstract class MongoLibrary<TViewModel> : ILibrary<TViewModel> where TViewModel : class
{
    private readonly MongoCollection _collection;
    private readonly string _dbName;
    private readonly string _collectionName;
    private readonly string _connString;

    protected MongoLibrary(string connString, string dbName, string collectionName)
    {
        _connString = connString;
        connString += "/" + dbName;
        var client = new MongoClient(connString);
        _dbName = dbName;
        _collectionName = collectionName;
        var db = client.GetServer().GetDatabase(dbName);
        _collection = db.GetCollection<TViewModel>(collectionName);
    }

    public abstract void ConfigureIndex(MongoCollection collection);

    public IQueryable<TViewModel> All()
    {
        return _collection.AsQueryable<TViewModel>();
    }

    public IEnumerable<TViewModel> GetWhere(Func<TViewModel, bool> predicate)
    {
        return _collection.AsQueryable<TViewModel>().Where(predicate);
    }

    protected void PopulateData(IEnumerable<TViewModel> views, int instanceToUpdate)
    {
        var ports = //port numbers hidden for security

        foreach (var port in ports)
        {
            var client = new MongoClient(string.Format("{0}:{1}/{2}", _connString, port, _dbName));

            var db = client.GetServer().GetDatabase(_dbName);
            var coll = db.GetCollection(_collectionName);
            coll.Drop();
            coll.InsertBatch(views);
        }
        ConfigureIndex(_collection);
    }

}
        var sw = new Stopwatch();
        sw.Start();
        var temp = _externalResourceLibrary.All().Where(er => er.Id > 7500).ToList();
        sw.Stop();
>db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()  
{
    "op" : "getmore",
    "ns" : "cache.ExternalResources",
    "query" : {
        "_id" : {
            "$gt" : 7500
        }
    },
    "cursorid" : NumberLong("949848842778037962"),
    "ntoreturn" : 0,
    "keyUpdates" : 0,
    "numYield" : 0,
    "lockStats" : {
        "timeLockedMicros" : {
            "r" : NumberLong(9486),
            "w" : NumberLong(0)
        },
        "timeAcquiringMicros" : {
            "r" : NumberLong(4),
            "w" : NumberLong(5)
        }
    },
    "nreturned" : 2576,
    "responseLength" : 1511874,
    "millis" : 9,
    "ts" : ISODate("2014-01-28T10:52:16.125Z"),
    "client" : <ipAddress>,
    "allUsers" : [
                    {
                        "user" : <username>,
                        "userSource" : <dbName>;
                    }
    ],
    "user" : <username>@<dbName>;
}
{
    "op" : "query",
    "ns" : "cache.ExternalResources",
    "query" : {
            "_id" : {
                    "$gt" : 7500
            }
    },
    "cursorid" : NumberLong("949848842778037962"),
    "ntoreturn" : 0,
    "ntoskip" : 0,
    "nscanned" : 102,
    "keyUpdates" : 0,
    "numYield" : 0,
    "lockStats" : {
            "timeLockedMicros" : {
                    "r" : NumberLong(749),
                    "w" : NumberLong(0)
            },
            "timeAcquiringMicros" : {
                    "r" : NumberLong(5),
                    "w" : NumberLong(3)
            }
    },
    "nreturned" : 101,
    "responseLength" : 64013,
    "millis" : 0,
    "ts" : ISODate("2014-01-28T10:52:15.954Z"),
    "client" : <ipAddress>,
    "allUsers" : [
            {
                    "user" : <username>,
                    "userSource" : <dbName>;
            }
    ],
    "user" : <username>@<dbName>;
}
{
    "op" : "command",
    "ns" : "cache.$cmd",
    "command" : {
            "authenticate" : 1,
            "user" : <username>,
            "nonce" : <nonce>;,
            "key" : <key>;
    },
    "ntoreturn" : 1,
    "keyUpdates" : 0,
    "numYield" : 0,
    "lockStats" : {
            "timeLockedMicros" : {
                    "r" : NumberLong(381),
                    "w" : NumberLong(0)
            },
            "timeAcquiringMicros" : {
                    "r" : NumberLong(9),
                    "w" : NumberLong(3)
            }
    },
    "responseLength" : 79,
    "millis" : 0,
    "ts" : ISODate("2014-01-28T10:52:15.938Z"),
    "client" : <ipAddress>,
    "allUsers" : [
            {
                    "user" : <username>,
                    "userSource" : <dbName>
            }
    ],
    "user" : <username>@<dbName>;
}