I am trying to learn how to use RavenDB, and for that I created a basic example. It seems that initializing the store and querying takes a huge amount of time!
static void Main( string[] args )
{
const bool createNewEntities = true;
var sw = new Stopwatch();
using( var store = new EmbeddableDocumentStore {DataDirectory = "~\\Data"} )
{
sw.Start();
store.Initialize();
sw.Stop();
Console.WriteLine( "Initialized in {0} ms.", sw.ElapsedMilliseconds );
if (createNewEntities)
{
sw.Reset();
sw.Start();
using( var session = store.OpenSession() )
{
sw.Stop();
Console.WriteLine();
Console.WriteLine( "Opened session in {0} ms.", sw.ElapsedMilliseconds );
for( var i = 0; i < 10; i++ )
{
var entity = new EntityA( "Entity A " + DateTime.Now.ToLongTimeString() );
sw.Reset();
sw.Start();
session.Store( entity );
sw.Stop();
if (i < 3)
Console.WriteLine( "Stored '{0}' in {1} ms.", entity.Name, sw.ElapsedMilliseconds );
}
sw.Reset();
sw.Start();
session.SaveChanges();
sw.Stop();
Console.WriteLine( "Saved changes in {0} ms.", sw.ElapsedMilliseconds );
}
}
sw.Reset();
sw.Start();
using( var session = store.OpenSession() )
{
sw.Stop();
Console.WriteLine();
Console.WriteLine( "Opened EntityA session in {0} ms.", sw.ElapsedMilliseconds );
sw.Reset();
sw.Start();
var entities = session.Query<EntityA>().ToArray();
sw.Stop();
Console.WriteLine("Queried for all {0} EntityA in {1} ms.", entities.Length, sw.ElapsedMilliseconds);
}
sw.Reset();
sw.Start();
using( var session = store.OpenSession() )
{
sw.Stop();
Console.WriteLine();
Console.WriteLine( "Opened EntityA session (again) in {0} ms.", sw.ElapsedMilliseconds );
sw.Reset();
sw.Start();
var entities2 = session.Query<EntityA>().ToArray();
sw.Stop();
Console.WriteLine( "Queried (again) for all {0} EntityA in {1} ms.", entities2.Length, sw.ElapsedMilliseconds );
}
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine( "Press ENTER to exit..." );
Console.ReadLine();
}
This produces the following output:
Initialized in 6132 ms. Opened session in 3 ms. Stored 'Entity A 08:50:14' in 129 ms. Stored 'Entity A 08:50:15' in 0 ms. Stored 'Entity A 08:50:15' in 0 ms. Saved changes in 29 ms. Opened EntityA session in 0 ms. Queried for all 10 EntityA in 463 ms. Opened EntityA session (again) in 0 ms. Queried (again) for all 10 EntityA in 1 ms.
From this crude example, I can see that:
- Initializing the store takes a huge amount of time!!
- Storing the first entity (of ten) takes quite some time.
- Querying for all entities takes a lot of time the first time, but no time at all the second time.
How do I properly query the DB for all documents of a certain type (EntityA)? Surely, it cannot be that RavenDB requires an index for every query? Especially not for queries without any criteria?
(Note: I intend to use the DB embedded in a desktop application, where listing all documents is used to display the contents of the DB.)