From 1166e51239dad6b95299a8b376cd60ee36d12a10 Mon Sep 17 00:00:00 2001 From: Yvan <8574526@qq,com> Date: Thu, 5 Nov 2026 02:03:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=81=BF=E5=85=8D=E6=B8=A0=E9=81=93?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E6=9C=AA=E5=91=BD=E4=B8=AD=E6=97=B6=E5=8F=8D?= =?UTF-8?q?=E5=A4=8D=E6=89=AB=E6=8F=8F=E6=B8=A0=E9=81=93=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/data/business/analytics.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/data/business/analytics.go b/internal/data/business/analytics.go index 070c521..1e09daa 100644 --- a/internal/data/business/analytics.go +++ b/internal/data/business/analytics.go @@ -27,14 +27,16 @@ func analyticsPromotes(db *gorm.DB, scope business.AnalyticsScope, f business.An return q } keyword := strings.ToLower(strings.TrimSpace(f.PromoteName)) - hits := analyticsBasePromotes(db, scope, f, "hit").Select("1").Where("INSTR(LOWER(hit.account),?)>0 OR INSTR(LOWER(hit.nickname),?)>0", keyword, keyword).Where("hit.source_db=" + alias + ".source_db") + hits := analyticsBasePromotes(db, scope, f, "hit").Where("INSTR(LOWER(hit.account),?)>0 OR INSTR(LOWER(hit.nickname),?)>0", keyword, keyword) if scope.IncludeSub { root := "CASE WHEN hit.promote_level=1 THEN hit.id WHEN hit.top_promote_id>0 THEN hit.top_promote_id ELSE hit.id END" - hits = hits.Where("(" + alias + ".id=(" + root + ") OR " + alias + ".top_promote_id=(" + root + "))") - } else { - hits = hits.Where("hit.id=" + alias + ".id") + roots := hits.Select("DISTINCT hit.source_db," + root) + // Uncorrelated sets let MySQL materialize matching roots once. + // A correlated OR/EXISTS can rescan all channels for every outer row, + // especially when a keyword matches nothing. + return q.Where("("+alias+".source_db,"+alias+".id) IN (?) OR ("+alias+".source_db,"+alias+".top_promote_id) IN (?)", roots, roots) } - return q.Where("EXISTS (?)", hits) + return q.Where("("+alias+".source_db,"+alias+".id) IN (?)", hits.Select("hit.source_db,hit.id")) } func prepareAnalytics(db *gorm.DB, scope business.AnalyticsScope, f business.AnalyticsFilter) (bool, bool, error) { if !scope.Viewer.All {