50w数据做测试
看一下mongo支持批量更新的几种PAI
1.0 public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass) 2.0public UpdateResult updateMulti(Query query, UpdateDefinition update, String collectionName) 3.0public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName) 4.0
给字段phone加上了唯一索引
/*** 电话
*/
@Indexed(unique = true)
private String phone;
具体写法一: 1.查询集合内所有的数据 Query query0 = new Query(); query0.fields().include("phone"); long l0 = System.currentTimeMillis(); List<User> users1 = mongoTemplate.find(query0, User.class, "animals"); List<String> collect = users1.stream().map(User::getPhone).collect(Collectors.toList()); long l1 = System.currentTimeMillis(); System.out.println(">>>>>>>>>>>:"+(l1-l0)); 2.更新参数 Update update=new Update(); update.set("sex","未知"); update.set("email","[email protected]"); update.set("work","焊武大帝"); UpdateOptions updateOptions=new UpdateOptions(); updateOptions.upsert(false); Query query = new Query(); Criteria criteria=new Criteria(); criteria.and("phone").in(collect); query.addCriteria(criteria); 3.具体更新 long l2= System.currentTimeMillis(); mongoTemplate.updateMulti(query, update, User.class,"animals"); long l3= System.currentTimeMillis(); System.out.println(">>>>>>>>>>>>>>>:"+(l3-l2)); 4.结果 >>>>>>>>>>>:2379(查询整个集合一个字段出来在2.5s以内) >>>>>>>>>>>>>>>:7629(更新50w控制在8s以内) 总计在12s以内,还可以接受
具体写法二: 1.0查询: Query query0 = new Query(); query0.fields().include("phone"); long l0 = System.currentTimeMillis(); List<User> users1 = mongoTemplate.find(query0, User.class, "animals"); List<String> collect =users1.stream().map(User::getPhone).collect(Collectors.toList()); long l1 = System.currentTimeMillis(); System.out.println(">>>>>>>>>>>:"+(l1-l0)); Update update=new Update(); update.set("sex","未知0"); update.set("email","[email protected]"); update.set("work","焊武大帝0"); UpdateOptions updateOptions=new UpdateOptions(); 2.更新参数: updateOptions.upsert(false); Query query = new Query(); Criteria criteria=new Criteria(); criteria.and("phone").in(collect); query.addCriteria(criteria); long l2= System.currentTimeMillis(); 3.0具体执行: BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "animals"); bulkOps.updateMulti(query, update); bulkOps.execute(); long l3= System.currentTimeMillis(); System.out.println(">>>>>>>>>>>>>>>:"+(l3-l2)); 4.0执行结果 >>>>>>>>>>>:2408(查询组织参数) >>>>>>>>>>>>>>>:7769(50w执行)
具体写法三:(如果phone不加索引,会非常非常慢) 1.0 List<Pair<Query, Update>> updates=new ArrayList<>(); collect.stream().forEach(ele->{ Query query1 = new Query(); query1.addCriteria(new Criteria().and("phone").is(ele)); Update update0=new Update(); update0.set("sex","未知"); update0.set("email","[email protected]"); update0.set("work","焊武大帝"); Pair<Query, Update> of = Pair.of(query1, update0); updates.add(of); }); BulkOperations bulkOps = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, "animals"); bulkOps.updateMulti(updates); bulkOps.execute(); long l3= System.currentTimeMillis(); System.out.println(">>>>>>>>>>>>>>>:"+(l3-l2)); 2.0结果 >>>>>>>>>>>:2434(查询50w) >>>>>>>>>>>>>>>:32490(执行50w,比上次时间高出了4倍!)
描述: 方法一和方法二结果基本一致,这两个都是基本的更新某个或者某几个字段 方法三除了多了一次50w的循环, List<Pair<Query, Update>> updates=new ArrayList<>();里面会在mongo内部执行50w操作,所以耗时比较严重。
标签:set,mongo,批量,update,System,几种,Query,new,currentTimeMillis From: https://www.cnblogs.com/wangbiaohistory/p/17658767.html