C# 雷文德慢速阅读
我正在使用RavenDB,但我注意到我的数据库在读取时非常慢。以下是我如何查询我的数据库:C# 雷文德慢速阅读,c#,.net,database,ravendb,C#,.net,Database,Ravendb,我正在使用RavenDB,但我注意到我的数据库在读取时非常慢。以下是我如何查询我的数据库: IEnumerable<Reduced> items; Filter filter = (Filter)Filter; var watch = Stopwatch.StartNew(); using (var session = documentStore.OpenSession()) {
IEnumerable<Reduced> items;
Filter filter = (Filter)Filter;
var watch = Stopwatch.StartNew();
using (var session = documentStore.OpenSession())
{
var query = session.Query<eBayItem,eBayItemIndexer>().Where(y => y.Price <= filter.MaxPrice && y.Price >= filter.MinPrice);
query = filter.Keywords.ToArray()
.Aggregate(query, (q, term) =>
q.Search(xx => xx.Title, term, options: SearchOptions.And));
if (filter.ExcludedKeywords.Count > 0)
{
query = filter.ExcludedKeywords.ToArray().Aggregate(query, (q, exterm) =>
q.Search(it => it.Title, exterm, options: SearchOptions.Not));
}
items = query.AsProjection<Reduced>().ToList();
Console.WriteLine("Query: " + query.ToString());
Console.WriteLine("Results: " + items.Count());
}
watch.Stop();
Console.WriteLine("Query time: " + watch.Elapsed.Milliseconds + " ms");
return items;
他们的代码是否有问题,导致速度变慢
embeddedabledocumentstore
可能是问题所在。更确切地说,您似乎正在初始化存储,然后立即调用查询。对索引的最初几次查询总是会稍微慢一些。我遇到了与此类似的问题,我使用的是专用服务器(不是嵌入式服务器)。我发现的问题是,我在查询时初始化文档存储,而不是在应用程序加载之前
如果在本机程序上运行,请在启动过程中初始化文档存储,并将其作为单例保存,直到要关闭该程序为止。您只需要一个文档存储,但应该为每个原子操作创建一个新会话
如果您在web应用程序中运行此应用程序,请在您选择的依赖项注入容器中注册应用程序启动时的文档存储作为单例作用域
这两个选项中的任何一个都将提前初始化文档存储并使其保持打开状态。然后,您只需为每个查询打开一个会话。在200万条记录中,用通用Guid查询大约300条记录时,我的时间从2秒多增加到了大约150毫秒。如果你有一个较小的数据库,那么这个数字应该会显著下降。在程序空闲1小时后,查询在Avarge上仍然需要350毫秒。不过,它必须与EmbeddedBledDocumentStore有关,托管一个本地服务器,查询时间就到了20ms@Ayende我也尝试在嵌入式模式下使用db,我的第一次很可怕。我能做些什么来加快速度吗?
public class eBayItemIndexer : AbstractIndexCreationTask<eBayItem>
{
public eBayItemIndexer()
{
Map = contentItems =>
from contentItem in contentItems
select new { contentItem.Title, contentItem.Price };
Index(x => x.Title, FieldIndexing.Analyzed);
Index(x => x.Price, FieldIndexing.Analyzed);
TransformResults = (database, items) =>
from contentItem in items
select new { contentItem.Id };
}
}
documentStore = new EmbeddableDocumentStore() { DataDirectory = "test.db" };
documentStore.Initialize();
IndexCreation.CreateIndexes(typeof(eBayItemIndexer).Assembly, documentStore);
documentStore.Configuration.TransactionMode = Raven.Abstractions.Data.TransactionMode.Lazy;
documentStore.Configuration.AllowLocalAccessWithoutAuthorization = true;