我能夠使用嵌入式版本的RavenDb加載幾百萬個文檔,非常漂亮!RavenDb對以百萬爲單位的文檔查詢性能的期望
現在我試圖查詢這些項目,並發現性能不是我所期望的,如果可能的話,幾乎是瞬間的,但是在一臺相當健壯的機器上可以達到18秒。
下面,你會發現我的樸素代碼。
注意:我現在已經解決了這個問題,最終的代碼位於帖子的底部。帶走的是你需要索引,它們必須是正確的類型,並且需要讓RavenDB知道它們。非常滿意通過查詢引擎返回記錄的性能和質量。
謝謝 斯蒂芬
using (var store = new EmbeddableDocumentStore { DataDirectory = @"C:\temp\ravendata" }.Initialize())
{
using (IDocumentSession session = store.OpenSession())
{
var q = session.Query<Product>().Where(x => x.INFO2.StartsWith("SYS")).ToList();
}
}
[Serializable]
public class Product
{
public decimal ProductId { get; set; }
....
public string 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);
}
}
,改變調用方法看起來像這樣。
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();
}
}
但我仍然報告18秒做搜索。我錯過了什麼?在另一個說明中,C:\ temp \ ravendata \ Indexes \ InfoIndex%2fSearch文件夾中有很多新文件,雖然不像插入數據時那麼多,但它們在運行此代碼後似乎都消失了幾次嘗試查詢。如果IndexCreation.CreateIndexes(Assembly.GetExecutingAssembly(),store);在插入之前調用,只有這樣?
EDIT1
使用此代碼我能得到查詢的一個實例幾乎要發生,但似乎你只能運行一次此,這樣引出了一個問題。在哪裏運行,什麼是正確的初始化程序?
store.DatabaseCommands.PutIndex("ProdcustByInfo2", new IndexDefinitionBuilder<Product>
{
Map = products => from product in products
select new { product.INFO2 },
Indexes =
{
{ x => x.INFO2, FieldIndexing.Analyzed}
}
});
EDIT2:工作示例
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();
}
你有一個涵蓋INFO2的指數嗎? – 2012-03-23 16:38:20
我現在在做什麼..旅程是多麼有趣,但非常值得。 – 2012-03-24 14:24:38