2013-10-27 88 views
1

考慮以下代碼輸出時間爲3056(約3秒)。性能差

因此,我使用MongoDb文檔推薦的代碼。

MongoClient mongo = new MongoClient(); 
      var server = mongo.GetServer(); 

      MongoDatabase db = server.GetDatabase("tutorial"); 
      Stopwatch stopwatch = new Stopwatch(); 
      stopwatch.Start(); 
      using (server.RequestStart(db)) 
      { 
       MongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("books"); 

       for (int i = 0; i < 100000; i++) 
       { 
        var nested = new BsonDocument 
        { 
         {"name", "John Doe"}, 

        }; 
        collection.Insert(nested); 
       } 
      } 
      stopwatch.Stop(); 
      Console.WriteLine(stopwatch.ElapsedMilliseconds); 

      Console.ReadLine(); 

當上述代碼運行的輸出時間是14225(大約10至14秒我的PC上)。 爲什麼我在新版本的mongoDb上重構代碼後得到這樣的性能時間。我錯過了什麼?

回答

2

當您使用推薦的連接模式(如第二個示例中所示)時,您很可能會看到新驅動程序默認啓用的寫入關注點之間的差異。

的變化是在2012年11月提出:

http://docs.mongodb.org/manual/release-notes/drivers-write-concern/

在此之前,寫入並沒有被默認確認,所以你會看到「更快」寫道。

還有一些關於C#更改的更多細節here

如果您想用新的風格的連接嘗試,你可以在您的測試數據庫連接禁用WriteConcern:

MongoDatabase db = server.GetDatabase("tutorial", WriteConcern.Unacknowledged); 

,然後重新運行測試,比較性能。

+0

這正是我正在尋找與WriteConcern.Unacknowledged集我得到同樣的performance.thank你。 –