我想學習如何使用RavenDB,併爲此創建了一個基本示例。 看起來初始化商店和查詢需要很長時間!如何優化用於檢索所有文檔的RavenDB查詢?
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();
}
這將產生以下輸出:
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.
從該粗例如,我可以看出:
- 初始化商店花費的時間量巨大!!
- 存儲第一個實體(十個)需要相當長的一段時間。
- 查詢所有實體首次需要很多時間,但第二次不需要任何時間。
如何正確查詢某個類型(EntityA)的所有文檔的數據庫? 當然,它不可能是RavenDB需要每個查詢的索引?特別是沒有任何標準的查詢?
(注:我打算使用嵌入在桌面應用程序,其中列出了所有文件,用於顯示數據庫中的內容DB)
這樣的「爲什麼」這樣的問題的答案通常很難找到,難以應用(因此,「不具有建設性」)。不要問爲什麼它是這樣的,你可能想要編輯來問你需要在你的代碼中做什麼樣的事情來優化性能。 – 2013-02-09 08:08:07
夠公平的。舉一個基本的例子,我的目的是作爲一個解釋性答案,比如「你使用錯了,因爲......」而不是簡單的是/否的答案,例如「是的,使用索引」,它不能解釋「爲什麼」。但你的觀點是合理的。我會嘗試重新說明我的問題! – Reyhn 2013-02-09 08:13:44