Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] Optimize the time consumption of scheduled tasks #2917

Open
ccfish86 opened this issue Dec 4, 2024 · 1 comment
Open
Assignees
Labels
enhancement New feature or request

Comments

@ccfish86
Copy link

ccfish86 commented Dec 4, 2024

Why this feature?

image
如图,如果conversation集合中文档比较多的时候, 如果每次只处理100条数据,将会导致mongo查询条件,"skip":放大文档扫描行数
image

Suggested Solution

1.建议处理时通过查询条件lt/gt加limit进行处理,取代skip、
2.加大limit数,减少skip执行次数。

Additional Information

这是其他耗时比较多的地方(mongo慢日志)
image

@ccfish86 ccfish86 added the enhancement New feature or request label Dec 4, 2024
@OpenIM-Robot OpenIM-Robot changed the title [FEATURE REQUEST] 优化定时任务耗时 [FEATURE REQUEST] Optimize the time consumption of scheduled tasks Dec 4, 2024
@ccfish86
Copy link
Author

ccfish86 commented Dec 10, 2024

谁帮忙看看这样改没问题吧?处理速度是提上来了
image


 // internal\rpc\conversation\conversation.go
func (c *conversationServer) GetConversationsNeedDestructMsgs(ctx context.Context, _ *pbconversation.GetConversationsNeedDestructMsgsReq) (*pbconversation.GetConversationsNeedDestructMsgsResp, error) {
	num, err := c.conversationDatabase.GetAllConversationIDsNumber(ctx)
	if err != nil {
		log.ZError(ctx, "GetAllConversationIDsNumber failed", err)
		return nil, err
	}
	const batchNum = 200
	maxPage := (num + batchNum - 1) / batchNum

	temp := make([]*model.Conversation, 0, batchNum)
	var curConversationID string

	for pageNumber := 0; pageNumber < int(maxPage); pageNumber++ {
		pagination := &sdkws.RequestPagination{
			PageNumber: 0,
			ShowNumber: batchNum,
		}

		conversationIDs, err := c.conversationDatabase.GetConversationIDsGtID(ctx, curConversationID, pagination)
		if err != nil {
			// log.ZError(ctx, "PageConversationIDs failed", err, "pageNumber", pageNumber)
			continue
		}

		// log.ZDebug(ctx, "PageConversationIDs success", "pageNumber", pageNumber, "conversationIDsNum", len(conversationIDs), "conversationIDs", conversationIDs)
		if len(conversationIDs) == 0 {
			break
		}

		conversations, err := c.conversationDatabase.GetConversationsByConversationID(ctx, conversationIDs)
		if err != nil {
			log.ZError(ctx, "GetConversationsByConversationID failed", err, "conversationIDs", conversationIDs)
			continue
		}

		for _, conversation := range conversations {
			if conversation.IsMsgDestruct && conversation.MsgDestructTime != 0 && ((time.Now().UnixMilli() > (conversation.MsgDestructTime + conversation.LatestMsgDestructTime.UnixMilli() + 8*60*60)) || // 8*60*60 is UTC+8
				conversation.LatestMsgDestructTime.IsZero()) {
				temp = append(temp, conversation)
			}
			curConversationID = conversation.ConversationID
		}
	}

	return &pbconversation.GetConversationsNeedDestructMsgsResp{Conversations: convert.ConversationsDB2Pb(temp)}, nil
}


 // pkg\common\storage\database\mgo\conversation.go

func (c *ConversationMgo) GetConversationIDsGtID(ctx context.Context, fromConversationID string, pagination pagination.Pagination) (conversationIDs []string, err error) {
	var filter bson.M
	if fromConversationID != "" {
		filter = bson.M{"conversation_id": bson.M{"$gt": fromConversationID}}
	}
	return mongoutil.FindPageOnly[string](ctx, c.coll, filter, pagination, options.Find().SetSort("conversation_id").SetProjection(bson.M{"conversation_id": 1}))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants