我正在使用 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 Title:(Canon) AND Title:(MP) AND Title:(Black) -Title:(G1) -Title:(T3)
- 结果: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;
他们的代码有问题吗?