wangkaiyue 9 달 전
부모
커밋
e40957f78f
1개의 변경된 파일31개의 추가작업 그리고 5개의 파일을 삭제
  1. 31 5
      userSign/userAnalysis/analysisManager.go

+ 31 - 5
userSign/userAnalysis/analysisManager.go

@@ -24,18 +24,44 @@ type (
 	}
 )
 
+// UpdateTag 更新标签对应的用户Bitmap,因sql太长所以拆分成batchSize插入
 func (ar *AnalysisRes) UpdateTag(ctx context.Context) {
-	list := make([]string, 0, len(ar.Data))
+	const batchSize = 5000
+	var (
+		updateBatch = [][]string{}
+		tmpArr      = make([]string, 0, batchSize)
+		total       = len(ar.Data)
+		index       = 0
+	)
 	for id, ok := range ar.Data {
 		if !ok {
 			continue
 		}
-		list = append(list, fmt.Sprintf("toUInt64(%d)", id))
+		tmpArr = append(tmpArr, fmt.Sprintf("toUInt64(%d)", id))
+		if len(tmpArr) == batchSize {
+			updateBatch = append(updateBatch, tmpArr)
+			tmpArr = make([]string, 0, batchSize)
+		}
+		index++
+		if index == total {
+			updateBatch = append(updateBatch, tmpArr)
+		}
 	}
-	execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapBuild([%v]) WHERE code = '%v';`, strings.Join(list, ","), ar.Code)
-	if _, err := g.DB().Exec(ctx, execSql); err != nil {
-		g.Log().Errorf(ctx, "更新标签%s异常 %v", ar.Code, err)
+
+	for i, batch := range updateBatch {
+		if i == 0 {
+			execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapBuild([%v]) WHERE code = '%v';`, strings.Join(batch, ","), ar.Code)
+			if _, err := g.DB().Exec(ctx, execSql); err != nil {
+				g.Log().Errorf(ctx, "更新标签%s [%d]异常 %v", ar.Code, i, err)
+			}
+		} else {
+			execSql := fmt.Sprintf(`ALTER TABLE dwd_d_tag UPDATE bitobj = bitmapOr(bitobj,bitmapBuild([%v])) WHERE code = '%v';`, strings.Join(batch, ","), ar.Code)
+			if _, err := g.DB().Exec(ctx, execSql); err != nil {
+				g.Log().Errorf(ctx, "更新标签%s [%d]异常 %v", ar.Code, i, err)
+			}
+		}
 	}
+	fmt.Println(ar.Code, "len", len(ar.Data))
 }
 
 func NewManager() *UserAnalysis {