MongoDB(online) 优化
MongoDB(online) 优化
1. find、findOne
- 项目实例
MongoTemplate mongoTemplate = mongodbClient.getMongoTemplate(); DBCollection cursor = mongoTemplate.getCollection("vip_batchsend_message"); BasicDBObject query = new BasicDBObject(); query.put("sms_code", sms_code); query.put("open_id", open_id); JSONObject message = new JSONObject(); DBCursor cursor1 = cursor.find(query); LOGGER.info("-------------sms_code:" + sms_code + "----open_id:" + open_id + "--" + cursor1.size()); if (cursor1.size() > 0) { DBObject dBObject = cursor1.next(); String corp_code = dBObject.get("corp_code").toString(); .....
- 简单阐述
- 代码意图是如果记录存在就修改
- 只需要判断是否存在就完成目的
- find返回的是DBCursor,这里不合适,我们只需要知道是否存在即可
- 修改建议
- 固定的条件提前预先组合
- find改成findOne
- 判断只需要查询一条记录即可,用findOne就可以,直接获取一个Object,判断后即可根据Key获取Value进行后续操作
- 建议修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | BasicDBObject query = new BasicDBObject(); query.put( "sms_code" , sms_code); query.put( "open_id" , open_id); MongoTemplate mongoTemplate = mongodbClient.getMongoTemplate(); JSONObject message = new JSONObject(); DBCollection cursor = mongoTemplate.getCollection( "vip_batchsend_message" ); DBObject dbObject = cursor.findOne(query); if (dbObject.isPartialObject()) { LOGGER.info( "-------------sms_code:" + sms_code + "----open_id:" + open_id + "--" + 1 ); String corp_code = dbObject.get( "corp_code" ).toString(); String vip_id = dbObject.get( "vip_id" ).toString(); Data data_corp_code = new Data( "corp_code" , corp_code, ValueType.PARAM); Data data_vip_id = new Data( "vip_ids" , vip_id, ValueType.PARAM); ...... |
2. 操作 vip_emp_relation 的一个公共方法
- 项目实例
1 2 3 4 5 6 7 8 9 10 11 | public DBCursor selectRelation(String app_user_name, String open_id) throws SQLException { MongoTemplate mongoTemplate = mongodbClient.getMongoTemplate(); DBCollection cursor = mongoTemplate.getCollection(WxConst.table_vip_emp_relation); Map keyMap = new HashMap(); keyMap.put( "_id" , app_user_name + open_id); BasicDBObject queryCondition = new BasicDBObject(); queryCondition.putAll(keyMap); DBCursor dbCursor = cursor.find(queryCondition); return dbCursor; } |
-
简单阐述
- 查看此方法的引用,基本是做判断使用,没必要返回一个Cursor
- 如果引用的方法,有长任务,然后再操作,就会等待很长时间不释放资源
-
建议修改
12345678910public
DBObject selectRelation(String app_user_name, String open_id)
throws
SQLException {
Map keyMap =
new
HashMap();
keyMap.put(
"_id"
, app_user_name + open_id);
BasicDBObject queryCondition =
new
BasicDBObject();
queryCondition.putAll(keyMap);
MongoTemplate mongoTemplate = mongodbClient.getMongoTemplate();
DBCollection cursor = mongoTemplate.getCollection(WxConst.table_vip_emp_relation);
DBObject dbCursor = cursor.findOne(queryCondition);
return
dbCursor;
}
3. 查询记录数
- 项目实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | BasicDBObject basicDBObject= new BasicDBObject(); basicDBObject.put( "activity_code" ,activity_code); basicDBObject.put( "open_id" ,open_id); basicDBObject.put( "status" , "1" ); int count=cursor.find(basicDBObject).count(); basicDBObject.put( "sign_status" , "Y" ); int sign_count=cursor.find(basicDBObject).count(); if (sign_count> 0 ){ result= "您已签到成功,请勿重复签到" ; } else { if (count > 0 ) { BasicDBObject query= new BasicDBObject(); query.put( "activity_code" ,activity_code); query.put( "open_id" ,open_id); ...... |
-
简单阐述
- 根据条件获取记录数
- 没必要先获取文档游标再查询记录数
-
建议修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | BasicDBObject basicDBObject= new BasicDBObject(); basicDBObject.put( "activity_code" ,activity_code); basicDBObject.put( "open_id" ,open_id); basicDBObject.put( "status" , "1" ); Long count=cursor.count(basicDBObject); basicDBObject.put( "sign_status" , "Y" ); Long sign_count=cursor.count(basicDBObject); if (sign_count> 0 ){ result= "您已签到成功,请勿重复签到" ; } else { if (count > 0 ) { BasicDBObject query= new BasicDBObject(); query.put( "activity_code" ,activity_code); query.put( "open_id" ,open_id); ...... |
4. save、insert
- 项目实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | if (cursor.find(basicDBObject).count() > 0 ){ BasicDBObject basicDBObject1= new BasicDBObject(); basicDBObject1.put( "sign_status" , "Y" ); basicDBObject1.put( "sign_date" ,Common.DATETIME_FORMAT.format( new Date())); BasicDBObject update= new BasicDBObject(); update.put( "$set" ,basicDBObject1); cursor.update(basicDBObj,update, true , false ); } else { BasicDBObject dbObject = new BasicDBObject(); dbObject.put( "_id" , app_id + "_" + activity_code + "_" + open_id); dbObject.put( "corp_code" , corp_code); dbObject.put( "sign_status" , "Y" ); dbObject.put( "sign_date" ,Common.DATETIME_FORMAT.format( new Date())); dbObject.put( "app_id" , app_id); dbObject.put( "activity_code" , activity_code); dbObject.put( "status" , "0" ); dbObject.put( "open_id" , open_id); dbObject.put( "vip" , vip_array.getJSONObject( 0 )); dbObject.put( "modified_date" , Common.DATETIME_FORMAT.format( new Date())); dbObject.put( "created_date" , Common.DATETIME_FORMAT.format( new Date())); cursor.save(dbObject); } ...... |
-
简单阐述
- save 是先根据_id查询再修改,如果已经确认记录不存在可以省去查找的功能直接insert
-
建议修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | if (cursor.find(basicDBObject).count() > 0 ){ BasicDBObject basicDBObject1= new BasicDBObject(); basicDBObject1.put( "sign_status" , "Y" ); basicDBObject1.put( "sign_date" ,Common.DATETIME_FORMAT.format( new Date())); BasicDBObject update= new BasicDBObject(); update.put( "$set" ,basicDBObject1); cursor.update(basicDBObj,update, true , false ); } else { BasicDBObject dbObject = new BasicDBObject(); dbObject.put( "_id" , app_id + "_" + activity_code + "_" + open_id); dbObject.put( "corp_code" , corp_code); dbObject.put( "sign_status" , "Y" ); dbObject.put( "sign_date" ,Common.DATETIME_FORMAT.format( new Date())); dbObject.put( "app_id" , app_id); dbObject.put( "activity_code" , activity_code); dbObject.put( "status" , "0" ); dbObject.put( "open_id" , open_id); dbObject.put( "vip" , vip_array.getJSONObject( 0 )); dbObject.put( "modified_date" , Common.DATETIME_FORMAT.format( new Date())); dbObject.put( "created_date" , Common.DATETIME_FORMAT.format( new Date())); cursor.insert(dbObject); } ...... |
5. 总结
- 以上是目前发现的问题及建议,会继续Review
- 涉及到IO的操作标配就是尽晚打开尽早释放
- 阿里的MongoDB默认没有启动读写分离(我已经测试确认过),我测试后会加上
- 业务实现过程,尽可能结构化数据,介绍拼接出错或Key不存在的异常
- 涉及到Cursor尽快手动关闭
- 其它项目也可以参考,或者抛出来
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端