2013-04-01 84 views
0

我有一個數據庫(RavenDB)需要能夠每10秒處理300個查詢(全文搜索)。爲了提高peformance我分裂了數據庫,所以我有多個documentStores 我的代碼:c#RavenDB嵌入式優化

  var watch = Stopwatch.StartNew(); 
     int taskcnt = 0; 
     int sum = 0; 


     for (int i = 0; i < 11; i++) 
     { 
      Parallel.For(0, 7, new Action<int>((x) => 
      { 
       for(int docomentStore = 0;docomentStore < 5; docomentStore++) 
       { 
        var stopWatch = Stopwatch.StartNew(); 
        Task<IList<eBayItem>> task = new Task<IList<eBayItem>>(Database.ExecuteQuery, new Filter() 
        { 
         Store = "test" + docomentStore, 
         MaxPrice = 600, 
         MinPrice = 200, 
         BIN = true, 
         Keywords = new List<string>() { "Canon", "MP", "Black" }, 
         ExcludedKeywords = new List<string>() { "G1", "T3" } 
        }); 
        task.ContinueWith((list) => { 
         stopWatch.Stop(); 
         sum += stopWatch.Elapsed.Milliseconds; 
         taskcnt++; 
         if (taskcnt == 300) 
         { 
          watch.Stop(); 
          Console.WriteLine("Average time: " + (sum/(float)300).ToString()); 
          Console.WriteLine("Total time: " + watch.Elapsed.ToString() + "ms"); 

         } 

        }); 
        task.Start(); 
       } 

      })); 
      Thread.Sleep(1000); 

     } 
  • 平均查詢時間:514,13 MS
  • 總時間:00:01:29.9108016

查詢ravenDB的代碼:

 public static IList<eBayItem> ExecuteQuery(object Filter) 
    { 
     IList<eBayItem> items; 
     Filter filter = (Filter)Filter; 

     if (int.Parse(filter.Store.ToCharArray().Last().ToString()) > 4) 
     { 
      Console.WriteLine(filter.Store); return null; 
     } 
     using (var session = Shards[filter.Store].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.ToList<eBayItem>(); 
     } 
     return items; 
    } 

並初始化RavenDB:

 static Dictionary<string, EmbeddableDocumentStore> Shards = new Dictionary<string, EmbeddableDocumentStore>(); 
    public static void Connect() 
    { 
     Shards.Add("test0", new EmbeddableDocumentStore() { DataDirectory = "test.db" }); 
     Shards.Add("test1", new EmbeddableDocumentStore() { DataDirectory = "test1.db" }); 
     Shards.Add("test2", new EmbeddableDocumentStore() { DataDirectory = "test2.db" }); 
     Shards.Add("test3", new EmbeddableDocumentStore() { DataDirectory = "test3.db" }); 
     Shards.Add("test4", new EmbeddableDocumentStore() { DataDirectory = "test4.db" }); 
     foreach (string key in Shards.Keys) 
     { 
      EmbeddableDocumentStore store = Shards[key]; 
      store.Initialize(); 
      IndexCreation.CreateIndexes(typeof(eBayItemIndexer).Assembly, store); 
     } 
    } 

如何優化我的代碼,使我的總時間更短?把我的數據庫分成5個不同的數據庫是好的嗎?

編輯:程序只有1 documentStore而不是5.(如Ayende Rahien sugested) 另外這是查詢自身:

Price_Range:[* TO Dx600] AND Price_Range:[Dx200 TO NULL] AND Title:(Canon) AND Title:(MP) AND Title:(Black) -Title:(G1) -Title:(T3) 

回答

2

不,這是不好的。 使用一個嵌入式RavenDB。如果你需要分片,這涉及多臺機器。

一般來說,RavenDB查詢每個都有幾個ms。你需要顯示你的查詢的樣子(你可以調用ToString()來查看)。以這種方式RavenDB的

有碎片意味着所有的人都在爭取CPU和IO

+0

Price_Range:[* TO Dx600]和Price_Range:[Dx200 TO NULL]和標題:(Canon)AND標題:(MP)AND標題:(黑色) - 標題:(G1) - 標題:(T3) 這是我的查詢。 – Svexo