Remember to solve the problem of high CPU usage in MongoDB

Source: Internet
Author: User
Tags mongodb cpu usage high cpu usage

Web services deployed on Alibaba Cloud, MongoDB is a three-copy master-slave node, with a timed task running every night, mainly through aggregate statistical analysis of each user's operational behavior. Running for a while, with the user's growth, it is found that statistical analysis processing originally slower, more than 12 hours a day, and CPU occupancy rate of up to 90%. Through the slow query records found constantly in the creation of the deletion of some tables, and then keep the master-slave synchronization, the time is obviously very long. The Java code found that the call is aggregate out, the method will overwrite the results of the aggregate output to the temporary table, so the deletion of the table will continue to be created. It would be nice to change out to aggregate. 30 minutes after the completion of the scheduled task, CPU utilization decreased to 60%

Pre-modification Code

iterator<recordstatistics> aggregate = Getds (). Createaggregation (Learningrecord.  Class)
        . Match (query)
        Group ("UserId",
                grouping ("avgscore  " Accumulator ("$avg","Score"),
                grouping ("avgtime"Accumulator (" $avg "," Learntime ")),
                grouping ("count "Accumulator ("$sum " , 1)),
                grouping ("totaltime"Accumulator ("$sum","Learntime"))
                )
        . Out (recordstatistics.  Class);

Post-Modification Code

iterator<recordstatistics> aggregate = Getds (). Createaggregation (Learningrecord.  Class)
        . Match (query)
        Group ("UserId",
                grouping ("avgscore  " Accumulator ("$avg","Score"),
                grouping ("avgtime"Accumulator (" $avg "," Learntime ")),
                grouping ("count "Accumulator ("$sum " , 1)),
                grouping ("totaltime"Accumulator ("$sum","Learntime"))
                )
        . Aggregate (recordstatistics.  Class);

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.