nosql mongodb group分组统计及索引优化

前端之家收集整理的这篇文章主要介绍了nosql mongodb group分组统计及索引优化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在做mongodb的分析统计,发现group分组有多种做法,和sql还真是不太样,需要写点js代码,直接贴示例代码

这是我的collection,名字:t_user_score
public class Userscore implements Serializable {

private Integer userId;
private BigDecimal userscore;

}

int userCount = 0;

GroupBy groupBy = GroupBy.key("userId")
                .initialDocument("userscoreTotal:0,userscoreCount:0,userscoreAvg:0}") 
                .reduceFunction("function(doc,prev){ if(doc.userscore != null){"
                                                         +"prev.userscoreTotal += parseFloat(doc.userscore);"
                                                         +"prev.userscoreCount + =1;"
                                                         +"}"
                                                      +"}")                                                         
               .finalizeFunction("function(prev){ if(prev.userscoreCount > 0) {"
                                                       +"prev.userscoreAvg=prev.userscoreTotal / prev.userscoreCount;"
                                                       +"}"
                                                 +"}");

GroupByResults<Userscore> r = evaluationTemplate.group("t_user_score",groupBy,Userscore.class);
BasicDBList list = (BasicDBList)r.getRawResults().get("retval"); 

for (int i = 0; i < list.size(); i ++) { 
    BasicDBObject obj = (BasicDBObject)list.get(i); 
    int userId = Double.valueOf(String.valueOf(obj.get("userId"))).intValue();

    logger.info(String.format("userId=%s json=%s ",userId,Utils.toJSONString(obj)));
}

登录控制台,建唯一索引(后台创建唯一的复合索引):
db.t_user_score.ensureIndex({“userId”:1},{background:1,unique:1});

–查看索引
db.t_user_score.getIndexes()

–查看索引大小
db.t_user_score.totalIndexSize();
–重建索引
db.t_user_score.reIndex()
删除索引
–db.t_user_score.dropIndex(“id_1”)

–使用索引的结果
db.t_user_score.find({“userId”:1000}).explain();

更多查询优化:AnalyzeQuery Performance :http://docs.mongodb.org/manual/tutorial/analyze-query-plan/

原文链接:https://www.f2er.com/nosql/203642.html

猜你在找的NoSQL相关文章