优化count查询速度

This commit is contained in:
lingxin 2025-03-17 09:49:06 +08:00
parent ec2caa723f
commit 51c8eb21a2

View File

@ -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
}