MongoDB intermediate ---- & gt; replace GroupBy with MapReduce in large data volumes

Source: Internet
Author: User
Tags emit

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

  1. For(VarI = 0; I <1000000; I ++)
  2. {
  3.  VarX ="0123456789";
  4.  VarTmp ="";
  5.  For(VarJ = 0; j <3; j ++)
  6. {
  7. Tmp + = x. charAt (Math. ceil (Math. random () * 100000000) % x. length); |
  8. }
  9.  VarU = {_ id: I, v1: tmp };
  10. Db. RandomNum. insert (u );
  11. }

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

  1. VarM =Function() {Emit (This. V1, {count: 1 });};// Map key is similar to group by of relational data. The second value is the field to be aggregated (sum ...)
  2. VarR =Function(Key, values ){VarTotal = 0;For(VarI = 0; I <values. length; I ++) {total + = values [I]. count ;}Return{Count: total };};.// Reduce
  3. VarRes = db. RandomNum. mapReduce (m, r, {out: {replace:'Result'}});
  4. Db [res. result]. find ()

Tested:

  1. VarStartTime =NewDate ();
  2. VarM =Function() {Emit (This. V1, {count: 1 });};
  3. VarR =Function(Key, values ){VarTotal = 0;For(VarI = 0; I <values. length; I ++) {total + = values [I]. count ;}Return{Count: total };};
  4. VarRes = db. RandomNum. mapReduce (m, r, {out: {replace:'Result'}});
  5. Db [res. result]. find ()
  6. (NewDate (). getTime ()-startTime. getTime ()/1000
The result is as follows:
  1. > Db [res. result]. find ()
  2. {"_ Id":"000","Value":{"Count":1075}}
  3. {"_ Id":"001","Value":{"Count":1045}}
  4. {"_ Id":"002","Value":{"Count":1022}}
  5. {"_ Id":"003","Value":{"Count":968}}
  6. {"_ Id":"004","Value":{"Count":994}}
  7. {"_ Id":"005","Value":{"Count":1009}}
  8. {"_ Id":"006","Value":{"Count":948}}
  9. {"_ Id":"007","Value":{"Count":1003}}
  10. {"_ Id":"008","Value":{"Count":983}}
  11. {"_ Id":"009","Value":{"Count":993}}
  12. {"_ Id":"010","Value":{"Count":987}}
  13. {"_ Id":"011","Value":{"Count":982}}
  14. {"_ Id":"012","Value":{"Count":957}}
  15. {"_ Id":"013","Value":{"Count":1031}}
  16. {"_ Id":"014","Value":{"Count":971}}
  17. {"_ Id":"015","Value":{"Count":1053}}
  18. {"_ Id":"016","Value":{"Count":974}}
  19. {"_ Id":"017","Value":{"Count":975}}
  20. {"_ Id":"018","Value":{"Count":978}}
  21. {"_ Id":"019","Value":{"Count":1010}}
  22. Has more
  23. >
  24. > (New Date (). getTime ()-startTime. getTime ())/1000
  25. 63.335 s
  26. > 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.