perf: build alert channel trees with indexed union branches

This commit is contained in:
Yvan
2026-11-06 05:53:53 +08:00
parent 75ef1216f7
commit ff580f4367
+10 -2
View File
@@ -10,8 +10,16 @@ import (
)
func alertTree(db *gorm.DB, roots []business.ChannelKey) *gorm.DB {
return db.Table("tab_promote root").Joins("JOIN tab_promote p ON p.source_db=root.source_db AND (p.id=root.id OR p.parent_id=root.id OR p.top_promote_id=root.id)").
Where("(root.source_db,root.id) IN ?", channelPairs(roots))
base := func() *gorm.DB {
return db.Table("tab_promote r").Where("(r.source_db,r.id) IN ?", channelPairs(roots))
}
self := base().Select("r.source_db,r.id,r.id AS leaf_id")
children := base().Joins("JOIN tab_promote child ON child.source_db=r.source_db AND child.parent_id=r.id").Select("r.source_db,r.id,child.id AS leaf_id")
descendants := base().Joins("JOIN tab_promote child ON child.source_db=r.source_db AND child.top_promote_id=r.id").Select("r.source_db,r.id,child.id AS leaf_id")
// Each branch can use its own equality index. UNION removes channels reached
// through both parent and top-level relationships without an OR join scan.
return db.Table("(? UNION ? UNION ?) root", self, children, descendants).
Joins("JOIN tab_promote p ON p.source_db=root.source_db AND p.id=root.leaf_id")
}
func alertMetricSpec(db *gorm.DB, metric int) (table, stamp, condition, amount string) {
if metric == 2 {