MongoDB Middle Level (大資料量下MapReduce取代GroupBy)

來源:互聯網
上載者:User

MongoDB中的MapReduce其實更類似關係型資料庫中的GroupBy 。

剛做了下這樣實驗,對於大資料量的GroupBy(MapReduce)還是比較理想的,產生100W條3位隨機字串

  for (var i=0; i<1000000; i++)   {  var x = "0123456789";  var tmp="";  for (var j=0; j<3; j++)  {  tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);|  }  var u = {_id:i,v1:tmp};  db.RandomNum.insert(u);  }

然後進行對相同的隨機數取Count數 所以必須GroupBy

var m = function(){emit(this.v1,{count:1}); }; //map key類似關係型資料的group by 第二個是value 就是要進行彙總的欄位(sum...)  var r = function (key,values) { var total = 0;for (var i=0; i<values.length; i++) { total += values[i].count;     } return {count : total}; };. //reduce var res = db.RandomNum.mapReduce(m, r, {out:{replace:'Result'}});db[res.result].find()

測試了下時間:

var startTime = new Date();var m = function(){emit(this.v1,{count:1}); };var r = function (key,values) { var total = 0;for (var i=0; i<values.length; i++) { total += values[i].count; } return {count : total}; }; var res = db.RandomNum.mapReduce(m, r, {out:{replace:'Result'}});db[res.result].find()(new Date().getTime()-startTime.getTime())/1000

結果如下:

> db[res.result].find(){ "_id" : "000", "value" : { "count" : 1075 } }{ "_id" : "001", "value" : { "count" : 1045 } }{ "_id" : "002", "value" : { "count" : 1022 } }{ "_id" : "003", "value" : { "count" : 968 } }{ "_id" : "004", "value" : { "count" : 994 } }{ "_id" : "005", "value" : { "count" : 1009 } }{ "_id" : "006", "value" : { "count" : 948 } }{ "_id" : "007", "value" : { "count" : 1003 } }{ "_id" : "008", "value" : { "count" : 983 } }{ "_id" : "009", "value" : { "count" : 993 } }{ "_id" : "010", "value" : { "count" : 987 } }{ "_id" : "011", "value" : { "count" : 982 } }{ "_id" : "012", "value" : { "count" : 957 } }{ "_id" : "013", "value" : { "count" : 1031 } }{ "_id" : "014", "value" : { "count" : 971 } }{ "_id" : "015", "value" : { "count" : 1053 } }{ "_id" : "016", "value" : { "count" : 974 } }{ "_id" : "017", "value" : { "count" : 975 } }{ "_id" : "018", "value" : { "count" : 978 } }{ "_id" : "019", "value" : { "count" : 1010 } }has more> > (new Date().getTime()-startTime.getTime())/100063.335s> bye

測試機的效能:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.