RavenDb对查询数以百万计的文档的性能期望

RavenDb对查询数以百万计的文档的性能期望,ravendb,Ravendb,我能够用嵌入式版本的RavenDb加载数百万个文档,非常灵活 现在我试图查询这些项目,发现性能并不是我所期望的,如果可能的话,几乎是瞬间的,而是在一台相当结实的机器上超过18秒 下面,您将看到我的天真代码 注意:我现在已经解决了这个问题,最后的代码在文章的底部。需要注意的是,您需要索引,它们必须是正确的类型,而且RavenDB需要知道它们。非常满意通过查询引擎返回的记录的性能和质量 谢谢,, 斯蒂芬 使用(var store=new embeddedabledocumentstore{DataD

我能够用嵌入式版本的RavenDb加载数百万个文档,非常灵活

现在我试图查询这些项目,发现性能并不是我所期望的,如果可能的话,几乎是瞬间的,而是在一台相当结实的机器上超过18秒

下面,您将看到我的天真代码

注意:我现在已经解决了这个问题,最后的代码在文章的底部。需要注意的是,您需要索引,它们必须是正确的类型,而且RavenDB需要知道它们。非常满意通过查询引擎返回的记录的性能和质量

谢谢,, 斯蒂芬

使用(var store=new embeddedabledocumentstore{DataDirectory=@“C:\temp\ravendata”}.Initialize())
{
使用(IDocumentSession=store.OpenSession())
{
var q=session.Query().Where(x=>x.INFO2.StartsWith(“SYS”).ToList();
}
}
[可序列化]
公共类产品
{
公共十进制ProductId{get;set;}
....
公共字符串INFO2{get;set;}
}
编辑

我加了这个类

public class InfoIndex_Search : AbstractIndexCreationTask<Product>
{
    public InfoIndex_Search()
    {
        Map = products => 
            from p in products
                          select new { Info2Index = p.INFO2 };

        Index(x => x.INFO2, FieldIndexing.Analyzed);
    }
}
公共类信息索引\u搜索:AbstractIndexCreationTask
{
公共信息索引搜索()
{
地图=产品=>
产品中的p
选择新{Info2Index=p.INFO2};
索引(x=>x.INFO2,fieldindex.analysis);
}
}
并将调用方法更改为如下所示

        using (var store = new EmbeddableDocumentStore { DataDirectory = @"C:\temp\ravendata" }.Initialize())
        {
            // Tell Raven to create our indexes.
            IndexCreation.CreateIndexes(Assembly.GetExecutingAssembly(), store);

            List<Product> q = null;
            using (IDocumentSession session = store.OpenSession())
            {
                q = session.Query<Product>().Where(x => x.INFO2.StartsWith("SYS")).ToList();
                watch.Stop();
            }
        }
使用(var store=new embeddedabledocumentstore{DataDirectory=@“C:\temp\ravendata”}.Initialize())
{
//告诉Raven创建我们的索引。
CreateIndexes(Assembly.getExecutionGassembly(),store);
列表q=null;
使用(IDocumentSession=store.OpenSession())
{
q=session.Query().Where(x=>x.INFO2.StartsWith(“SYS”).ToList();
看,停;
}
}
但我还是报告了18秒的搜索时间。我错过了什么?另一方面,在C:\temp\ravenada\index\InfoIndex%2fSearch文件夹中有很多新文件,虽然没有我插入数据时那么多,但在尝试查询几次后,这些文件似乎都消失了。应该是IndexCreation.CreateIndexes(Assembly.getExecutionGassembly(),store);在插入之前调用,并且仅在插入之前调用

编辑1

使用这段代码,我几乎可以在一个实例中执行查询,但您似乎只能运行一次,所以这就回避了问题。在何处运行此操作?正确的初始化过程是什么

store.DatabaseCommands.PutIndex("ProdcustByInfo2", new IndexDefinitionBuilder<Product>
{
    Map = products => from product in products
                      select new { product.INFO2 },
    Indexes =
            {
                { x => x.INFO2, FieldIndexing.Analyzed}
            }
});
store.DatabaseCommands.PutIndex(“ProdcustByInfo2”,新的IndexDefinitionBuilder
{
映射=产品=>来自产品中的产品
选择新建{product.INFO2},
索引=
{
{x=>x.INFO2,FieldIndexing.Analyzed}
}
});
编辑2:工作示例

static void Main()
{
    Stopwatch watch = Stopwatch.StartNew();

    int q = 0;
    using (var store = new EmbeddableDocumentStore { DataDirectory = @"C:\temp\ravendata" }.Initialize())
    {
        if (store.DatabaseCommands.GetIndex("ProdcustByInfo2") == null)
        {
            store.DatabaseCommands.PutIndex("ProdcustByInfo2", new IndexDefinitionBuilder<Product>
            {
                Map = products => from product in products
                                  select new { product.INFO2 },
                Indexes = { { x => x.INFO2, FieldIndexing.Analyzed } }
            });
        }
        watch.Stop();
        Console.WriteLine("Time elapsed to create index {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);

        watch = Stopwatch.StartNew();               
        using (IDocumentSession session = store.OpenSession())
        {
            q = session.Query<Product>().Count();
        }
        watch.Stop();
        Console.WriteLine("Time elapsed to query for products values {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
        Console.WriteLine("Total number of products loaded: {0}{1}", q, System.Environment.NewLine);

        if (q == 0)
        {
            watch = Stopwatch.StartNew();
            var productsList = Parsers.GetProducts().ToList();
            watch.Stop();
            Console.WriteLine("Time elapsed to parse: {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
            Console.WriteLine("Total number of items parsed: {0}{1}", productsList.Count, System.Environment.NewLine);

            watch = Stopwatch.StartNew();
            productsList.RemoveAll(_ => _ == null);
            watch.Stop();
            Console.WriteLine("Time elapsed to remove null values {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
            Console.WriteLine("Total number of items loaded: {0}{1}", productsList.Count, System.Environment.NewLine);

            watch = Stopwatch.StartNew();
            int batch = 0;
            var session = store.OpenSession();
            foreach (var product in productsList)
            {
                batch++;
                session.Store(product);
                if (batch % 128 == 0)
                {
                    session.SaveChanges();
                    session.Dispose();
                    session = store.OpenSession();
                }
            }
            session.SaveChanges();
            session.Dispose();
            watch.Stop();
            Console.WriteLine("Time elapsed to populate db from collection {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
        }

        watch = Stopwatch.StartNew();
        using (IDocumentSession session = store.OpenSession())
        {
            q = session.Query<Product>().Where(x => x.INFO2.StartsWith("SYS")).Count();
        }
        watch.Stop();
        Console.WriteLine("Time elapsed to query for term {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
        Console.WriteLine("Total number of items found: {0}{1}", q, System.Environment.NewLine);
    }
    Console.ReadLine();
}
static void Main()
{
秒表=Stopwatch.StartNew();
int q=0;
使用(var store=new embeddedabledocumentstore{DataDirectory=@“C:\temp\ravendata”}.Initialize())
{
if(store.DatabaseCommands.GetIndex(“ProdcustByInfo2”)==null)
{
store.DatabaseCommands.PutIndex(“ProdcustByInfo2”,新的IndexDefinitionBuilder
{
映射=产品=>来自产品中的产品
选择新建{product.INFO2},
index={{x=>x.INFO2,fieldindex.Analyzed}
});
}
看,停;
WriteLine(“创建索引{0}{1}所用的时间”,watch.elapsedmillesons,System.Environment.NewLine);
watch=Stopwatch.StartNew();
使用(IDocumentSession=store.OpenSession())
{
q=session.Query().Count();
}
看,停;
WriteLine(“查询产品值{0}{1}所用的时间”,watch.elapsedmillesons,System.Environment.NewLine);
WriteLine(“加载的产品总数:{0}{1}”,q,System.Environment.NewLine);
如果(q==0)
{
watch=Stopwatch.StartNew();
var productsList=Parsers.GetProducts().ToList();
看,停;
WriteLine(“解析所用的时间:{0}{1}”,watch.elapsedmillesons,System.Environment.NewLine);
WriteLine(“分析的项目总数:{0}{1}”,productsList.Count,System.Environment.NewLine);
watch=Stopwatch.StartNew();
productsList.RemoveAll(\u=>\ u==null);
看,停;
WriteLine(“删除空值{0}{1}所用的时间”,watch.elapsedmillesons,System.Environment.NewLine);
WriteLine(“加载的项目总数:{0}{1}”,productsList.Count,System.Environment.NewLine);
watch=Stopwatch.StartNew();
int批=0;
var session=store.OpenSession();
foreach(产品列表中的var产品)
{
批处理++;
商店(产品);
如果(批次%128==0)
{
session.SaveChanges();
session.Dispose();
session=store.OpenSession();
}
}
session.SaveChanges();
session.Dispose();
看,停;
WriteLine(“从集合{0}{1}填充数据库所用的时间”,watch.elapsedmillesons,System.Environment.NewLine);
}
watch=Stopwatch.StartNew();
使用(IDocumentSession=store.OpenSession())
{
q=session.Query().Where(x=>x.INFO2.StartsWith(“SYS”).Count();
}
看,停;
Console.WriteLine(“到que所用的时间
static void Main()
{
    Stopwatch watch = Stopwatch.StartNew();

    int q = 0;
    using (var store = new EmbeddableDocumentStore { DataDirectory = @"C:\temp\ravendata" }.Initialize())
    {
        if (store.DatabaseCommands.GetIndex("ProdcustByInfo2") == null)
        {
            store.DatabaseCommands.PutIndex("ProdcustByInfo2", new IndexDefinitionBuilder<Product>
            {
                Map = products => from product in products
                                  select new { product.INFO2 },
                Indexes = { { x => x.INFO2, FieldIndexing.Analyzed } }
            });
        }
        watch.Stop();
        Console.WriteLine("Time elapsed to create index {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);

        watch = Stopwatch.StartNew();               
        using (IDocumentSession session = store.OpenSession())
        {
            q = session.Query<Product>().Count();
        }
        watch.Stop();
        Console.WriteLine("Time elapsed to query for products values {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
        Console.WriteLine("Total number of products loaded: {0}{1}", q, System.Environment.NewLine);

        if (q == 0)
        {
            watch = Stopwatch.StartNew();
            var productsList = Parsers.GetProducts().ToList();
            watch.Stop();
            Console.WriteLine("Time elapsed to parse: {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
            Console.WriteLine("Total number of items parsed: {0}{1}", productsList.Count, System.Environment.NewLine);

            watch = Stopwatch.StartNew();
            productsList.RemoveAll(_ => _ == null);
            watch.Stop();
            Console.WriteLine("Time elapsed to remove null values {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
            Console.WriteLine("Total number of items loaded: {0}{1}", productsList.Count, System.Environment.NewLine);

            watch = Stopwatch.StartNew();
            int batch = 0;
            var session = store.OpenSession();
            foreach (var product in productsList)
            {
                batch++;
                session.Store(product);
                if (batch % 128 == 0)
                {
                    session.SaveChanges();
                    session.Dispose();
                    session = store.OpenSession();
                }
            }
            session.SaveChanges();
            session.Dispose();
            watch.Stop();
            Console.WriteLine("Time elapsed to populate db from collection {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
        }

        watch = Stopwatch.StartNew();
        using (IDocumentSession session = store.OpenSession())
        {
            q = session.Query<Product>().Where(x => x.INFO2.StartsWith("SYS")).Count();
        }
        watch.Stop();
        Console.WriteLine("Time elapsed to query for term {0}{1}", watch.ElapsedMilliseconds, System.Environment.NewLine);
        Console.WriteLine("Total number of items found: {0}{1}", q, System.Environment.NewLine);
    }
    Console.ReadLine();
}
public class LogMessageCreatedTime : AbstractIndexCreationTask<LogMessage>
{
    public LogMessageCreatedTime()
    {
        Map = messages => from message in messages
                          select new { MessageCreatedTime = message.MessageCreatedTime };
    }
}
private static DocumentStore GetDatabase()
{            
    DocumentStore documentStore = new DocumentStore();            

    try
    {
        documentStore.ConnectionStringName = "RavenDb";                
        documentStore.Initialize();

        // Tell Raven to create our indexes.
        IndexCreation.CreateIndexes(typeof(DataAccessFactory).Assembly, documentStore);
    }
    catch
    {
        documentStore.Dispose();
        throw;
    }

    return documentStore;
}