您的位置:首页 > 游戏 > 手游 > 提高MongoDB效率九大优化方式

提高MongoDB效率九大优化方式

2024/10/7 2:20:48 来源:https://blog.csdn.net/jia12216/article/details/139291128  浏览:    关键词:提高MongoDB效率九大优化方式

MongoDB九大优化方式:
第一:单次批量查询比循环逐条查询更快;
循环查询是开发者最常犯的错误写法。循环嵌套查询写代码快,逻辑简单,最耗时。在小批量数据处理时间一般远小于数据库查询时间。所以尽量避免增加查询次数,能把多次查询合并成单次批量查询尽量查询。解决办法:先预处理把查询字段合并成一个数组,然后数组中的同类项,接着用批量查询(包括管道操作),转成map,最后用map的get方法查找数据。

let list = await VRRecord.find({type: '航拍',status: {$in: ['', null, 'FINISH']}}, '-_id -estate_id -views_num').sort({create_time: -1}).lean().exec();// let time1 =  new Date().getTime();// logger.debug('time1-time0:', time1-time0, 'list.length:', list.length);// 使用map和concat来遍历list,并提取每个元素的estates数组let newList = [].concat(...list.map(item => {// 确保estates是一个数组return Array.isArray(item.estates) ? item.estates : [];}));// 使用Set来去除重复的newList = Array.from(new Set(newList));// logger.debug('newList:', newList);let subUserMap = new Map();if(newList.length > 0){let realEstateList = await EstateModel.find({id: {$in: newList}},'-_id id real_estate_name').lean().exec();subUserMap = new Map(realEstateList.map(item => [item.id, item]));}// logger.debug('subUserMap:', subUserMap);let newList2 = [].concat(...list.map(item => {// 确保lands是一个数组return Array.isArray(item.lands) ? item.lands : [];}));// 使用Set来去除重复的newList2 = Array.from(new Set(newList2));// logger.debug('newList:', newList);let subUserMap2 = new Map();if(newList2.length > 0){let matchLands = await LandModel.find({id: {$in: newList2}},'-_id id name').lean().exec();subUserMap2 = new Map(matchLands.map(item => [item.id, item]));}let nameMap = {};for (let i=0; i<list.length; i++) {list[i].hasMarker = false;if (list[i].markers) {for (let key in list[i].markers) {if (list[i].markers[key].length) {list[i].hasMarker = true;break;}}}delete list[i].markers;let matchEstates = [];if (list[i].estates) {for (let k=0; k<list[i].estates.length; k++) {let id = list[i].estates[k];if (!nameMap[id]) {let realEstate = subUserMap.get(id);// await EstateModel.findOne({//     id: id// },'-_id id real_estate_name').lean().exec();nameMap[id] = realEstate;}if (nameMap[id]) {matchEstates.push(nameMap[id]);}}}list[i].real_estate_name = matchEstates.map(x => x.real_estate_name).join(',');list[i].matchEstates = matchEstates;if (list[i].lands) {let newLands = [];list[i].lands.forEach(land =>{if(land){let item = subUserMap2.get(land);if(item){newLands.push(item);}}});// let matchLands = await LandModel.find({//     id: {$in: list[i].lands}// }, 'id name -_id');list[i].matchLands = newLands;//matchLands;}}

这种修改可以把原来嵌套查询达到12秒的处理时间降低到0.3秒

第二:尽量避免嵌入不断增加的数据;
MongoDB存储数据的机制决定了对数组不断追加数据是很低效的。在正常使用中数组和对象大小应该相对固定。
第三:预填充数据;
比如统计每个小时的访问量,先建一个24个0的数组。
第四:尽可能预先分配空间;
知道文档开始比较小,后面会变为确定的大小的,可以先存个确定大小的填充字段,再删除( u n s e t ) 这个字段。如果存储文档时便为其分配将来所需的空间,之后便不再需要移动它。第五:文档要自给自足; M o n g o D B 只存取数据,不做计算。计算尽量在客户端(包括调用 M o n g o D B 的后台服务器)完成。第六:避免使用 unset)这个字段。如果存储文档时便为其分配将来所需的空间,之后便不再需要移动它。 第五:文档要自给自足; MongoDB只存取数据,不做计算。计算尽量在客户端(包括调用MongoDB的后台服务器)完成。 第六:避免使用 unset)这个字段。如果存储文档时便为其分配将来所需的空间,之后便不再需要移动它。第五:文档要自给自足;MongoDB只存取数据,不做计算。计算尽量在客户端(包括调用MongoDB的后台服务器)完成。第六:避免使用where;
第七:编写代码处理数据完整性问题;
在后台执行一些检查和保护数据的脚本,能解除很多后顾之忧,避免出现脏数据。
第八:使用正确的类型;
数据类型影响数据的查询方式、数据存放顺序、占用多少空间。数字,日期,字符串。
第九:用简单唯一的id替换掉_id。
节省空间,注意这个唯一id要是唯一的。原来_id具有递增不重复的作用,代替它的id也有具有该功能来避免出现脏数据。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com