RavenDB慢速读取
本文关键字:读取 RavenDB | 更新日期: 2023-09-27 18:07:17
我正在使用RavenDB,但我注意到我的数据库在读取时非常慢。以下是我如何查询我的数据库:
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;
输出:
- 查询:Price_Range:[*TO Dx600]AND Price_Range:[Dx400 TO NULL]AND标题:(佳能(AND标题:
- 结果:3
- 查询时间:365毫秒
索引:
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;
他们的代码有问题吗?
EmbeddableDocumentStore
可能是问题所在。更确切地说,它看起来像是在初始化存储,然后立即调用查询。索引上的前几个查询总是会慢一些。
我遇到了类似的问题,我使用的是专用服务器(不是嵌入式的(。我发现的问题是,我在查询时初始化文档存储,而不是在加载应用程序时预先初始化。
如果在本机程序上运行,请在启动过程中初始化文档存储,并将其作为单例保存,直到关闭程序为止。您只需要一个文档存储,但应该为每个原子操作创建一个新会话。
如果您在web应用程序中运行此操作,请在您选择的依赖项注入容器中的应用程序启动时将文档存储注册为单例作用域。
这些选项中的任何一个都将提前初始化文档存储并保持其打开状态。然后,您只需要为每个查询打开一个会话。当我在200万条记录中用一个普通的Guid查询大约300条记录时,我从2秒多到大约150毫秒。如果你有一个较小的数据库,这个数字应该会大幅下降。