2013-10-27 52 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你。 –