【问题标题】:RavenDB slow readingRavenDB 慢读
【发布时间】:2013-04-02 18:20:33
【问题描述】:

我正在使用 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;

他们的代码有问题吗?

【问题讨论】:

    标签: c# .net database ravendb


    【解决方案1】:

    EmbeddableDocumentStore 可能是问题所在。更准确地说,看起来您正在初始化商店,然后立即调用查询。索引的前几个查询总是会慢一些。

    【讨论】:

    • 在保持程序空闲 1 小时后,查询仍然平均需要 350 毫秒。
    • 它必须与 EmbeddableDocumentStore 做一些事情,在本地托管服务器,查询时间约为 20 毫秒
    • @Ayende 我也在尝试在嵌入式模式下使用数据库,而且我的初始化时间很糟糕。有什么办法可以加快速度吗?
    【解决方案2】:

    我遇到了类似的问题,我使用的是专用服务器(非嵌入式)。我发现的问题是我在查询时初始化文档存储,而不是在应用程序加载时初始化。

    如果在本机程序上运行,请在启动过程中初始化文档存储并将其保持为单例,直到您要关闭程序。您只需要一个文档存储,但您应该为每个原子操作创建一个新会话。

    如果您在 Web 应用程序中运行它,请在您选择的依赖注入容器中将应用程序启动时的文档存储注册为单例范围。

    这些选项中的任何一个都会提前初始化文档存储并使其保持打开状态。然后,您只需为每个查询打开一个会话。当通过一个通用 Guid 从 200 万条记录中查询大约 300 条记录时,我从 2+ 秒到大约 150 毫秒。如果您有一个较小的数据库,这个数字应该会显着下降。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      相关资源
      最近更新 更多