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