[英]MongoDb C# Driver - Get item by indexed field very slow
I ran into performance issues when fetching filtered data on a small local database. 在小型本地数据库上获取过滤数据时,我遇到了性能问题。 I reduced the code (as below) to reproduce the issue. 我减少了代码(如下所示)以重现该问题。 The output is about 2000ms. 输出约为2000ms。
Some additional information: 一些其他信息:
Code: 码:
public class UnitTest1
{
public void TestMethod2()
{
Stopwatch sw = new Stopwatch();
sw.Start();
new BaseRepository<TickerData>().GetById("5731d39062deb83134772e77");
sw.Stop();
Debug.Write(sw.Elapsed.TotalMilliseconds);
}
}
public class BaseRepository<T> : MongoBase where T : BaseEntity
{
MongoDatabase DataBase { get; set; }
protected IQueryable<T> Collection { get; set; }
MongoCollection<BsonDocument> mCollection { get; set; }
public BaseRepository()
{
DataBase = Server.GetDatabase("TradingBot");
mCollection = DataBase.GetCollection<BsonDocument>(typeof(T).Name);
Collection = mCollection.AsQueryable<T>();
}
public T GetById(string ID)
{
return Collection.Single(i => i.Id.ToString() == ID);
}
}
Update: Suggest as per @rnofenko (see comments) made a huge improvement but still seem way to slow? 更新:根据@rnofenko提出的建议(请参阅评论)取得了很大的进步,但似乎仍然很慢?
It's a tricky moment. 这是一个棘手的时刻。 You call extension method Single from System.Linq . 您从System.Linq调用扩展方法Single 。 In fact you load whole collection from DB and select by ID inside your application. 实际上,您是从数据库加载整个集合,然后在应用程序中按ID选择。
Instead you need to use MongoDB extension method - SingleAsync from MongoDB.Driver.Linq . 相反,您需要使用MongoDB扩展方法-MongoDB.Driver.Linq中的SingleAsync 。
public async Task<T> GetById(string id)
{
return await Collection.SingleAsync(x => x.Id == new ObjectId(id));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.