请教大家一个 mongodb 大数据量下, count 优化的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
这是一个创建于 575 天前的主题,其中的信息可能已经有所发展或是发生改变。
问题背景:
现在有个一个集合,每月大概会往里插入将近 600M 的数据,项目项目上线了将近 4 个月,有 2500W 数据,现在 count 一次,需要将近 1~2 分钟,即使使用了各类索引优化,还是没什么太大作用。

我想请教一下大家,遇到这中情况,该如何优化呢,或者说,有什么更合适的方案去在这种数据量下进行 count 查询呢?

谢谢!
第 1 条附言    2024-03-13 11:43:28 +08:00

大概的查询语句

集合schema

{ //...省略其他字段 "_id": ObjectId("65e9310b6c21fc6df939c43d"), "nodeType": "start", "executeTime": ISODate("2024-03-07T03:14:19.708Z"), "isDetail": false, } 

索引

{ "executeTime": -1, "nodeType": 1, "isDetail": 1, } 

查询语句

db.collection.countDocuments({ "executeTime": { $gte: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000), $lte: new Date() }, "nodeType": "interactive", "isDetail": true, }, { "executeTime": 1 "nodeType": 1, "isDetail": 1, "_id": 1, }) 
13 条回复    2024-03-13 13:28:12 +08:00
Yuan2One
    1
Yuan2One  
   2024-03-13 10:51:01 +08:00 via Android
只查总数吗,存 redis 可以吗
14v45mJPBYJW8dT7
    2
14v45mJPBYJW8dT7  
   2024-03-13 10:53:50 +08:00
定时去 count 缓存,这么多数据应该不需要实时精确数量吧?
coderxy
    3
coderxy  
   2024-03-13 10:58:56 +08:00
这个要看你索引的定义, 一般 count 条件语句的第一个字段非常重要,要用比较稀疏的字段, 还是要具体分析的。 你可以把你的字段和查询条件贴出来,具体分析一下看看。
defunct9
    4
defunct9  
   2024-03-13 11:14:57 +08:00   1
开 ssh ,让我上去看看
lilei2023
    5
lilei2023  
   2024-03-13 11:21:34 +08:00
@defunct9 你这个光头,每次都要 ssh !
Belmode
    6
Belmode  
OP
   2024-03-13 11:35:32 +08:00
@Yuan2One #1 虽然查询频率很低,但是还是会有条件变化的,redis 不适合这种场景。

@rimutuyuan #2 查询频率很低,但是条件会变化,所以定时任务也不太行

@coderxy #3 查询的第一个条件是时间。

@defunct9 #4 老哥老面孔了

@lilei2023 #5 上面的老哥是来挽尊的,气氛担当~
Belmode
    7
Belmode  
OP
   2024-03-13 11:41:49 +08:00
@coderxy #3 集合结构和查询语句

集合 schema
{
//...省略其他字段
"_id": ObjectId("65e9310b6c21fc6df939c43d"),
"nodeType": "start",
"executeTime": ISODate("2024-03-07T03:14:19.708Z"),
"isDetail": false,
}
索引
{
"executeTime": -1,
"nodeType": 1,
"isDetail": 1,
}

查询语句
db.collection.countDocuments({
"executeTime": {
$gte: new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000),
$lte: new Date()
},
"nodeType": "interactive",
"isDetail": true,
}, {
"executeTime": 1
"nodeType": 1,
"isDetail": 1,
"_id": 1,
})
coderxy
    8
coderxy  
   2024-03-13 11:47:52 +08:00
@Belmode nodeType 存在的值多不多? 多的话拿 nodeType 做联合索引第一位,isDetail=true 的情况多不多,多的话拿 isDetail 做第一位,反正索引要么是 nodeType_isDetail_executeTime 要么是 isDetail_nodeType_executeTime ,你再去试试,性能应该回好不少。 你现在的索引用法,估计 explain 分析一下,seek 特别多。 最终的效果可能相当于只有 executeTime 单索引的效果。
rrfeng
    9
rrfeng  
   2024-03-13 11:53:31 +08:00 via Android
这索引没啥可以优化的,只能走预计算了。
比如把每天的 count 算出来单独存一下,这个查询优化成 sum 29 天 统计数据+本日数据。
mingsz
    10
mingsz  
   2024-03-13 11:56:32 +08:00
executeTime 需要查询的那么精确吗,可以新增个字段,如果到天/小时,索引会小很多
ychost
    11
ychost  
   2024-03-13 12:02:48 +08:00
这个感觉没太大优化点了,每天提前把所有条件都查一遍吧,然后前端值允许查看预制的筛选条件
nomagick
    12
nomagick  
   2024-03-13 12:04:18 +08:00
是不是小内存机械盘?

起码得用个固态盘。。。
Belmode
    13
Belmode  
OP
   2024-03-13 13:28:12 +08:00
@coderxy @rrfeng @ychost @nomagick 多谢各位了,我去研究研究
关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2825 人在线   最高记录 6679       Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 26ms UTC 13:35 PVG 21:35 LAX 06:35 JFK 09:35
Do have faith in what you're doing.
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86