fix: 调整event/ue查询排序字段提升查询索引命中

This commit is contained in:
TsMask
2025-08-20 16:49:42 +08:00
parent 30d563bbf6
commit 34f8456721
6 changed files with 96 additions and 6 deletions

View File

@@ -132,13 +132,103 @@ func (r PerfKPI) SelectKPI(query model.GoldKPIQuery) []model.KpiReport {
// SelectGoldKPITitle 网元对应的指标名称
func (r *PerfKPI) SelectGoldKPITitle(neType string) []model.GoldKPITitle {
result := []model.GoldKPITitle{}
tx := datasource.DefaultDB().Table("kpi_title").Where("ne_type = ?", neType).Find(&result)
tx := datasource.DefaultDB().Table("kpi_title").Where("ne_type = ? and status_flag = '1'", neType).Find(&result)
if err := tx.Error; err != nil {
logger.Errorf("Find err => %v", err)
}
return result
}
// SelectByPageTitle 分页查询集合
func (r PerfKPI) TitleSelectByPage(query map[string]string) ([]model.GoldKPITitle, int64) {
tx := datasource.DB("").Model(&model.GoldKPITitle{})
// 查询条件拼接
if v, ok := query["neType"]; ok && v != "" {
tx = tx.Where("ne_type = ?", v)
}
if v, ok := query["title"]; ok && v != "" {
tx = tx.Where("en_title like concat(concat('%', ?), '%')", v)
}
if v, ok := query["statusFlag"]; ok && v != "" {
tx = tx.Where("status_flag = ?", v)
}
// 查询结果
var total int64 = 0
rows := []model.GoldKPITitle{}
// 查询数量 长度为0直接返回
if err := tx.Count(&total).Error; err != nil || total <= 0 {
return rows, total
}
// 分页
pageNum, pageSize := datasource.PageNumSize(query["pageNum"], query["pageSize"])
tx = tx.Offset(int(pageNum * pageSize)).Limit(int(pageSize))
// 排序
if v, ok := query["sortField"]; ok && v != "" {
sortSql := v
if o, ok := query["sortOrder"]; ok && o != "" {
if o == "desc" {
sortSql += " desc "
} else {
sortSql += " asc "
}
}
tx = tx.Order(sortSql)
}
// 查询数据
if err := tx.Find(&rows).Error; err != nil {
logger.Errorf("query err => %v", err)
}
return rows, total
}
// TitleSelect 网元对应的指标名称
func (r PerfKPI) TitleSelect(param model.GoldKPITitle) []model.GoldKPITitle {
tx := datasource.DB("").Model(&model.GoldKPITitle{})
// 构建查询条件
if param.ID != "" {
tx = tx.Where("id =?", param.ID)
}
if param.NeType != "" {
tx = tx.Where("ne_type =?", param.NeType)
}
if param.KPIID != "" {
tx = tx.Where("kpi_id =?", param.KPIID)
}
if param.StatusFlag != "" {
tx = tx.Where("status_flag = ?", param.StatusFlag)
}
// 查询数据
rows := []model.GoldKPITitle{}
if err := tx.Find(&rows).Error; err != nil {
logger.Errorf("query find err => %v", err.Error())
return rows
}
return rows
}
// TitleUpdate 修改信息
func (r PerfKPI) TitleUpdate(param model.GoldKPITitle) int64 {
if param.ID == "" {
return 0
}
tx := datasource.DB("").Model(&model.GoldKPITitle{})
// 构建查询条件
tx = tx.Where("id = ?", param.ID)
tx = tx.Omit("id", "kpi_id", "title_json")
// 执行更新
if err := tx.Updates(param).Error; err != nil {
logger.Errorf("update err => %v", err.Error())
return 0
}
return tx.RowsAffected
}
// SelectUPFTotalFlow 查询UPF总流量 N3上行 N6下行
func (r *PerfKPI) SelectUPFTotalFlow(neType, rmUID, startDate, endDate string) map[string]any {
// 查询条件拼接

View File

@@ -181,7 +181,7 @@ func (r *CDREventIMS) SelectPage(querys model.CDREventIMSQuery) map[string]any {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
orderSql = fmt.Sprintf(" order by %s,id desc ", sortSql)
}
// 查询数据

View File

@@ -168,7 +168,7 @@ func (r *CDREventSMF) SelectPage(querys model.CDREventSMFQuery) map[string]any {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
orderSql = fmt.Sprintf(" order by %s,id desc ", sortSql)
}
// 查询数据

View File

@@ -182,7 +182,7 @@ func (r *CDREventSMSC) SelectPage(querys model.CDREventSMSCQuery) map[string]any
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
orderSql = fmt.Sprintf(" order by %s,id desc ", sortSql)
}
// 查询数据

View File

@@ -168,7 +168,7 @@ func (r *UEEventAMF) SelectPage(querys model.UEEventAMFQuery) map[string]any {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
orderSql = fmt.Sprintf(" order by %s,id desc ", sortSql)
}
// 查询数据

View File

@@ -168,7 +168,7 @@ func (r *UEEventMME) SelectPage(querys model.UEEventMMEQuery) map[string]any {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by id desc, %s ", sortSql)
orderSql = fmt.Sprintf(" order by %s,id desc ", sortSql)
}
// 查询数据