我最近一直致力于设置 Mongo 数据库服务器来充当网站的对象缓存。然而,在设置完所有内容后,我担心性能会相当慢。
它是单个服务器,不是副本集或分片的一部分。我正在从本地计算机到 Windows Azure VM 上的服务器运行所有测试。
例如,我收集了大约 5,500 个文档,其中每个文档都存储一个指向外部站点的链接。典型的文档如下所示:
{
"_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() 公开了 IQuerable 接口(interface))
上面的查询返回大约 3000 条记录。
我已经检查了 linq 生成的查询,它似乎没问题:
{_id: { $gt: 7000} }
从umongo(GUI 界面)运行相同的查询会在不到一秒的时间内返回结果,这更符合我的预期。
使用确保索引正确索引字段(填充集合时调用一次,之后这是只读数据:
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));
索引似乎用于搜索:
db.ExternalResources.find({_id: {$gt: 7000}}).explain()
{
"cursor" : "BtreeCursor _id_",
"isMultiKey" : false,
"n" : 3087,
"nscannedObjects" : 3087,
"nscanned" : 3087,
"nscannedObjectsAllPlans" : 3087,
"nscannedAllPlans" : 3087,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 5,
"indexBounds" : {
"_id" : [
[
7000,
1.7976931348623157e+308
]
]
},
"server" : <Server Name>
}
即使是单个直接 ID 查找也需要近 100 毫秒才能在代码中返回,例如:
from er in lib.All()
where er.Id == 3100
select er
我是否缺少任何可以加快搜索速度的设置,或者我的期望一开始就不切实际?
编辑:我已经尝试完全重建/重新填充数据库,但仍然感觉慢得令人无法接受
编辑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);
}
}
编辑 3:服务器日志分析对数据库的查询:
该查询只是为了展示性能而进行的人工查询。查询是:
var sw = new Stopwatch();
sw.Start();
var temp = _externalResourceLibrary.All().Where(er => er.Id > 7500).ToList();
sw.Stop();
您可以从日志中看到它经过身份验证,然后在 2 趟中检索了数据。总共检索到 2677 份文件。所用时间为 3580 毫秒(5 次运行的平均值)
>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>;
}
最佳答案
问题是由网络延迟引起的
关于c# - 使用 C# 的 MongoDb 响应缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21336012/