MongoDB middle level (replace groupby with mapreduce in large data volumes)

Source: Internet
Author: User

In fact, mapreduce in MongoDB is more similar to groupby in relational databases.

Just after doing this experiment, the groupby (mapreduce) for large data volumes is still ideal, generating million 3-bit random strings

  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);  }

Then perform the same random number to get the Count number, so it must be groupby

VaR M = function () {emit (this. v1, {count: 1}) ;}; // map key is similar to the group by of relational data. The second value is the field to be aggregated (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 ()

Tested:

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

The result is as follows:

> 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

Test Machine performance:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.