当前位置:网站首页>MongoDB - 千万级数据脚本过滤笔记
MongoDB - 千万级数据脚本过滤笔记
2022-07-30 07:08:00 【stark张宇】
概述
根据业务部分的需要,有一部分用户对进行刷评论来获取成就的行为,所以对全量评论对不合格的运算,进行标记,全量评论大概有2450w左右,之前的评论使用Mysql进行分表处理,后台使用MongoDB进行数据聚合,大概情况是这样。
1.使用分页处理
$where['status'] = 1;
$page = 1;
$limit = 1000;
while (true) {
$listData = Nosql_Comment_NovelMongoDB::getInstance()
->getCommentListByConditionAndPage($where, $page, $limit);
if (!$listData) {
echo '执行结束';
break;
}
foreach ($listData as $item) {
$flag = 0;
$bool = Service_Comment_Tools::checkRepeatContents($item['content']);
if($bool === false){
$flag = 1;
}
$nid = intval($item['nid']);
$id = intval($item['id']);
Nosql_Comment_NovelMongoDB::getInstance()->updateFilter($nid, $id, $flag);
}
$page++;
}
每次处理1000条,时间倒序处理,结果不理想,当跳过292万的时候,需要8秒,时间太长了,后来找到原因是这样的,skip 跳过少量数据没有问题,但是跳过100万,意味着要先找到100万条数据然后扔掉,越到后面跳过越多,数据库计算量越大,需要想办法优化。
2.精确mongo条件,使用主键对数据进行更新
添加了时间区间、状态、礼物等条件尽量的去掉不合适的数据。
$where['status'] = 1;
$where['begDate'] = strtotime('2018-01-02 12:17:03');
$where['endDate'] = strtotime('2017-07-20 11:24:05');
$limit = 1000;
while (true) {
$listData = ChangpeiNosql_Comment_NovelMongoDB::getInstance()
->getHistoryComment($where, $limit);
if (!$listData) {
echo '执行结束';
break;
}
foreach ($listData as $item) {
$flag = 0;
$bool = ChangpeiService_Comment_Tools::checkRepeatContents($item['content']);
if ($bool === false) {
$flag = 1;
}
$_id = $item['_id'];
ChangpeiNosql_Comment_NovelMongoDB::getInstance()->updateFilter($_id, $flag);
}
$lastData = end($listData);
$where['endDate'] = (int)$lastData['create_time'] + 1 ;
}
在更新MongoDB时,查询日志一切都ok,可以判断,应该就是更新消耗时间了,下一步,对更新进行优化。更新慢的原因是 update 是包含 select 的 先要找到再改,使用主键最快,可以进行批量修改
public function updateFilter($_id, int $flag)
{
$update['filter_status'] = intval($flag);
$count = 0;
if ($update) {
$query = [
'_id' => $_id
];
$res = $this->_client->updateOne($query, [
'$set' => $update
]);
$count = $res->getModifiedCount();
}
return $count;
}
一波三折,2450w的数据终于跑完了,任务也算圆满结束了,对Mongo操作的类库进行了脱敏处理,有需要的同学可以点击查看 https://github.com/stark0824/php7_mongo
边栏推荐
- Leetcode 2.两数相加 两个非空的链表,表示两个非负的整数。它们每位数字都是按照逆序的方式存储的。
- 01 多线程与高并发 - 基础概念
- C language custom types, rounding
- IDEA搜索插件无结果一直转圈圈的解决办法
- C13—使用innosetup工具制作软件安装向导2022-07-23
- K-Net:Towards Unified Image Segmentation,基于动态内核的通用分割网络,(NMS-free and Box-free),从语义/实例分割到全景分割。
- C语言力扣第46题之全排列。回溯法
- 防止资源导出失败
- SQL的substring_index()用法——MySQL字符串截取
- 【小程序专栏】总结uniapp开发小程序的开发规范
猜你喜欢
The difference between typescript3-ts and js
40.【vector的运用】
WinForm(一):开始一个WinForm程序
muduo库学习记录(一)
ARM体系结构概述
How to calculate the daily cumulative capital flow one by one in real time
What convenience does the RFID fixed asset inventory system bring to enterprises?
Mybitatis相关配置文件
你好,我的新名字叫 “铜锁 / Tongsuo”
Mybitatis related configuration files
随机推荐
申请内存,std::transform和AVX256指令集用例和执行速度比较
C语言力扣第46题之全排列。回溯法
k8s 部署mysql8(PV和PVC 版本)
立创EDA——PCB的走线(五)
SQL injection vulnerability (postgresql injection)
IDEA 中CheckStyle安装及使用
linux安装mysql8参考指引
SQL注入漏洞(postgresql注入)
mysql URL链接
typescript8 - type annotations
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
MySQL basics [naming convention]
typescript5 - compile and install ts code
你好,我的新名字叫 “铜锁 / Tongsuo”
mysql8的my.conf配置文件参考指引
Limit injection record of mysql injection in No. 5 dark area shooting range
函数(1)
redis常用指令
Basic usage of tree arrays
2022牛客暑期多校训练营3(ACFGJ)