2011-08-03 1 views
0

今日私はMongoデータベースをテストしましたが、パフォーマンスの問題があります。 1.800.00を挿入した後、私はすべての値の合計を作成しようとしましたが、それも57秒です。 それから、私はMSSQLで同じことを試み、0を取った!!Mongoデータベースについての性能質問

私が間違っていることをヒントを教えてもらえますか? これはマンゴーの制限ですか?次の行の

static void Main(string[] args) 
    { 
     //Create a default mongo object. This handles our connections to the database. 
     //By default, this will connect to localhost, port 27017 which we already have running from earlier. 
     var connStr = new MongoConnectionStringBuilder(); 
     connStr.ConnectTimeout = new TimeSpan(1, 0, 0); 
     connStr.SocketTimeout = new TimeSpan(1, 0, 0); 
     connStr.Server = new MongoServerAddress("localhost"); 
     var mongo = MongoServer.Create(connStr); 

     //Get the blog database. If it doesn't exist, that's ok because MongoDB will create it 
     //for us when we first use it. Awesome!!! 
     var db = mongo.GetDatabase("blog"); 

     var sw = new Stopwatch(); 
     sw.Start(); 
     //Get the Post collection. By default, we'll use the name of the class as the collection name. Again, 
     //if it doesn't exist, MongoDB will create it when we first use it. 
     var collection = db.GetCollection<Post>("Post"); 
     Console.WriteLine(collection.Count()); 
     sw.Stop(); 
     Console.WriteLine("Time: " + sw.Elapsed.TotalSeconds); 

     sw.Reset(); 
     sw.Start(); 
     var starting = collection.Count(); 
     var batch = new List<Post>(); 
     for (int i = starting; i < starting + 200000; i++) 
     { 
      var post = new Post 
      { 
       Body = i.ToString(), 
       Title = "title " + i.ToString(), 
       CharCount = i.ToString().Length, 
       CreatedBy = "user", 
       ModifiedBy = "user", 
       ModifiedOn = DateTime.Now, 
       CreatedOn = DateTime.Now 
      }; 
      //collection.Insert<Post>(post); 
      batch.Add(post); 
     } 
     collection.InsertBatch(batch); 
     Console.WriteLine(collection.Count()); 
     sw.Stop(); 
     Console.WriteLine("Time to insert 100.000 records: " + sw.Elapsed.TotalSeconds); 

     //var q = collection.Find(Query.LT("Body", "30000")).ToList(); 
     //Console.WriteLine(q.Count()); 

     sw.Reset(); 
     sw.Start(); 
     var q2 = collection.AsQueryable<Post>(); 
     var sum = q2.Sum(p => p.CharCount); 
     Console.WriteLine(sum); 
     sw.Stop(); 
     Console.WriteLine("Time to sum '" + q2.Count() + "' Post records: " + sw.Elapsed.TotalSeconds); //PROBLEM: take 57 to SUM 1.000.000 records 

} }

答えて

2

パフォーマンスの問題:あなたは、LINQをサポートしていないため、ドライバのメモリにポストのコレクションからすべての記事をロードする上の行で

var q2 = collection.AsQueryable<Post>(); 

。 MSSQLでは、Linqのために2分の1を取ることができず、計算はデータベースを経由します。ここでは、データをメモリにロードするためには、ほとんどすべて57秒が必要だと思います。あなたはいつでもその可能性など、余分なフィールドを作成します(デ・データを正規化)し、任意の合計を計算し、カウンターする必要が最高のパフォーマンスを達成するにMongoDBでは

。それができない場合は、group(和演算のあなたの例のために良いフィット感)のように、map/reduceまたは利用可能aggregate機能を使用する必要があります。