数据 1800万 数据一直增加中
优化前 耗时:7.9s(未加索引,查询时CPU飞了)
filter = bson.D{
{"userid", userid}, {"height", height}}
Txs, err := c.Mgo.Collection(m.UserTxs).Find(context.Background(), filter, opts)
sCount, err := c.Mgo.Collection(m.UserTxs).CountDocuments(context.Background(), filter)
优化后 耗时: 8-15ms (这里毫秒) 添加索引(这里添加对当前条件索引)
索引
{
"height" : 1,
"userid" : 1
}
sCount, err := c.Mgo.Collection(m.UserTxs).CountDocuments(context.Background(), filter)
//这里使用CountDocuments 函数时可根据查询条件 指定强制使用某个索引
optCount := new(options.CountOptions