Jelajahi Sumber

优化count查询速度

lingxin 1 bulan lalu
induk
melakukan
51c8eb21a2
1 mengubah file dengan 37 tambahan dan 1 penghapusan
  1. 37 1
      lxDb/sql.go

+ 37 - 1
lxDb/sql.go

@@ -241,7 +241,12 @@ func SqlQuery(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, par
 		if needDoCount(q) {
 			var total int64
 			//tx = tx.Count(&total)
-			tx.Raw("SELECT COUNT(*) as total FROM ("+sql2+") aaaa", params...).Take(&total)
+			//tx.Raw("SELECT COUNT(*) as total FROM ("+sql2+") aaaa", params...).Take(&total)
+			//todo lcs 优化速度 start
+			// 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY
+			replacedSQL := replaceSelectAndRemoveGroupBy(sql2)
+			tx.Raw(replacedSQL, params...).Take(&total)
+			//todo end
 			q.Total = int(total)
 			if total == 0 { // 如果查了记录条数并且是0, 就不需要查记录和汇总了
 				return
@@ -396,3 +401,34 @@ func Transaction(txItem, txMain *gorm.DB, fun func(txItem, txMain *gorm.DB) (err
 	}
 	return
 }
+
+// replaceSelectAndRemoveGroupBy 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY
+func replaceSelectAndRemoveGroupBy(sql string) string {
+	// 找到 SELECT 和 FROM 的位置
+	selectIndex := strings.Index(strings.ToUpper(sql), "SELECT")
+	fromIndex := strings.Index(strings.ToUpper(sql), "FROM")
+
+	if selectIndex == -1 || fromIndex == -1 {
+		return sql // 如果没有找到 SELECT 或 FROM,返回原始 SQL
+	}
+
+	// 替换 SELECT 和 FROM 之间的部分
+	newSelectClause := "COUNT(DISTINCT ***) AS total"
+	replacedSQL := sql[:selectIndex+6] + " " + newSelectClause + " " + sql[fromIndex:]
+
+	// 去掉 GROUP BY 子句
+	groupByIndex := strings.Index(strings.ToUpper(replacedSQL), "GROUP BY")
+	if groupByIndex != -1 {
+		c := replacedSQL[groupByIndex+8:]
+		c = strings.TrimSpace(c)
+		c = strings.ReplaceAll(c, ";", "")
+		c = strings.Split(c, ",")[0]
+		replacedSQL = replacedSQL[:groupByIndex]
+		replacedSQL = strings.ReplaceAll(replacedSQL, "DISTINCT ***", "DISTINCT "+c)
+	} else {
+		//没有group by 的
+		replacedSQL = strings.ReplaceAll(replacedSQL, "DISTINCT ***", "*")
+	}
+
+	return replacedSQL
+}