From f79d460f10abf975ab06ab3277bcb47362c6e4e8 Mon Sep 17 00:00:00 2001 From: hy001 Date: Wed, 13 May 2026 18:00:56 +0800 Subject: [PATCH] perf(console): optimize game metric ranking queries --- ...CountryDashboardGameMetricServiceImpl.java | 36 +++++++++++------ .../rds/dao/datav/CountryDashboardDAO.java | 10 +++++ .../dao/datav/CountryDashboardDAO.xml | 39 +++++++++++++++++++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/rc-service/rc-service-console/console-application/src/main/java/com/red/circle/console/app/service/app/datav/CountryDashboardGameMetricServiceImpl.java b/rc-service/rc-service-console/console-application/src/main/java/com/red/circle/console/app/service/app/datav/CountryDashboardGameMetricServiceImpl.java index 0186b0ff..ef048476 100644 --- a/rc-service/rc-service-console/console-application/src/main/java/com/red/circle/console/app/service/app/datav/CountryDashboardGameMetricServiceImpl.java +++ b/rc-service/rc-service-console/console-application/src/main/java/com/red/circle/console/app/service/app/datav/CountryDashboardGameMetricServiceImpl.java @@ -89,13 +89,14 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa Long total = countryDashboardDAO.countGamePeriodMetrics( condition.periodType, condition.startTime, condition.endTime, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, - condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, - condition.sysOrigin); + condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone, + condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin); List records = countryDashboardDAO.listGamePeriodMetrics( condition.periodType, condition.startTime, condition.endTime, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, - condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, - condition.sysOrigin, sortSpec.column(), sortSpec.direction(), offset, limit) + condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone, + condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin, + sortSpec.column(), sortSpec.direction(), offset, limit) .stream() .map(this::toCO) .toList(); @@ -133,13 +134,14 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa Long total = countryDashboardDAO.countGameSummaryMetrics( condition.periodType, condition.startTime, condition.endTime, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, - condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, - condition.sysOrigin); + condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone, + condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin); List records = countryDashboardDAO.listGameSummaryMetrics( condition.periodType, condition.startTime, condition.endTime, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, - condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, - condition.sysOrigin, sortSpec.column(), sortSpec.direction(), offset, limit) + condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone, + condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin, + sortSpec.column(), sortSpec.direction(), offset, limit) .stream() .map(this::toCO) .toList(); @@ -168,8 +170,9 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa return countryDashboardDAO.listGameCountryRankMetrics( condition.periodType, condition.startTime, condition.endTime, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, - condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, null, - gameId, condition.sysOrigin, limit) + condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone, + condition.countryKeyword, countryCodes, gameProvider, null, gameId, condition.sysOrigin, + limit) .stream() .map(this::toCO) .toList(); @@ -639,12 +642,15 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa private final LocalDate endDateValue; private final LocalDateTime startTime; private final LocalDateTime endTime; + private final String startPeriodKey; + private final String endPeriodKey; private final String countryKeyword; private final String sysOrigin; private QueryCondition(String periodType, String statTimezone, ZoneId statZoneId, LocalDate startDateValue, LocalDate endDateValue, LocalDateTime startTime, - LocalDateTime endTime, String countryKeyword, String sysOrigin) { + LocalDateTime endTime, String startPeriodKey, String endPeriodKey, + String countryKeyword, String sysOrigin) { this.periodType = periodType; this.statTimezone = statTimezone; this.statZoneId = statZoneId; @@ -652,6 +658,8 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa this.endDateValue = endDateValue; this.startTime = startTime; this.endTime = endTime; + this.startPeriodKey = startPeriodKey; + this.endPeriodKey = endPeriodKey; this.countryKeyword = countryKeyword; this.sysOrigin = sysOrigin; } @@ -693,6 +701,10 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa : LocalDateTime.ofInstant(startInstant, STORAGE_ZONE_ID); LocalDateTime storageEndTime = endInstant == null ? null : LocalDateTime.ofInstant(endInstant, STORAGE_ZONE_ID); + String startPeriodKey = PERIOD_DAY.equals(periodType) && start != null + ? start.format(DATE_FORMATTER) : null; + String endPeriodKey = PERIOD_DAY.equals(periodType) && end != null + ? end.format(DATE_FORMATTER) : null; return new QueryCondition( periodType, statZoneId.getId(), @@ -701,6 +713,8 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa end, storageStartTime, storageEndTime, + startPeriodKey, + endPeriodKey, trimToNull(cmd == null ? null : cmd.getCountryKeyword()), trimToNull(cmd == null ? null : cmd.getSysOrigin()) ); diff --git a/rc-service/rc-service-console/console-infrastructure/src/main/java/com/red/circle/console/infra/database/rds/dao/datav/CountryDashboardDAO.java b/rc-service/rc-service-console/console-infrastructure/src/main/java/com/red/circle/console/infra/database/rds/dao/datav/CountryDashboardDAO.java index 728b2a01..7b2b5e3e 100644 --- a/rc-service/rc-service-console/console-infrastructure/src/main/java/com/red/circle/console/infra/database/rds/dao/datav/CountryDashboardDAO.java +++ b/rc-service/rc-service-console/console-infrastructure/src/main/java/com/red/circle/console/infra/database/rds/dao/datav/CountryDashboardDAO.java @@ -342,6 +342,8 @@ public interface CountryDashboardDAO extends BaseDAO { @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("periodKey") String periodKey, + @Param("startPeriodKey") String startPeriodKey, + @Param("endPeriodKey") String endPeriodKey, @Param("statTimezone") String statTimezone, @Param("countryKeyword") String countryKeyword, @Param("countryCodes") String countryCodes, @@ -354,6 +356,8 @@ public interface CountryDashboardDAO extends BaseDAO { @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("periodKey") String periodKey, + @Param("startPeriodKey") String startPeriodKey, + @Param("endPeriodKey") String endPeriodKey, @Param("statTimezone") String statTimezone, @Param("countryKeyword") String countryKeyword, @Param("countryCodes") String countryCodes, @@ -370,6 +374,8 @@ public interface CountryDashboardDAO extends BaseDAO { @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("periodKey") String periodKey, + @Param("startPeriodKey") String startPeriodKey, + @Param("endPeriodKey") String endPeriodKey, @Param("statTimezone") String statTimezone, @Param("countryKeyword") String countryKeyword, @Param("countryCodes") String countryCodes, @@ -382,6 +388,8 @@ public interface CountryDashboardDAO extends BaseDAO { @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("periodKey") String periodKey, + @Param("startPeriodKey") String startPeriodKey, + @Param("endPeriodKey") String endPeriodKey, @Param("statTimezone") String statTimezone, @Param("countryKeyword") String countryKeyword, @Param("countryCodes") String countryCodes, @@ -398,6 +406,8 @@ public interface CountryDashboardDAO extends BaseDAO { @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("periodKey") String periodKey, + @Param("startPeriodKey") String startPeriodKey, + @Param("endPeriodKey") String endPeriodKey, @Param("statTimezone") String statTimezone, @Param("countryKeyword") String countryKeyword, @Param("countryCodes") String countryCodes, diff --git a/rc-service/rc-service-console/console-infrastructure/src/main/resources/dao/datav/CountryDashboardDAO.xml b/rc-service/rc-service-console/console-infrastructure/src/main/resources/dao/datav/CountryDashboardDAO.xml index ea5537d0..3fe0e1c5 100644 --- a/rc-service/rc-service-console/console-infrastructure/src/main/resources/dao/datav/CountryDashboardDAO.xml +++ b/rc-service/rc-service-console/console-infrastructure/src/main/resources/dao/datav/CountryDashboardDAO.xml @@ -121,6 +121,41 @@ KEY `idx_game_lookup` (`sys_origin`, `stat_timezone`, `game_provider`, `game_id`, `period_type`, `period_key`), KEY `idx_game_period_sort` (`sys_origin`, `stat_timezone`, `period_type`, `period_key`, `consume_amount`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='国家数据大屏游戏维度周期预聚合' + ; + + SET @current_schema = DATABASE(); + + SET @index_columns = ( + SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = @current_schema + AND TABLE_NAME = 'country_dashboard_game_period_metric' + AND INDEX_NAME = 'idx_game_period_game' + ); + SET @ddl = IF( + @index_columns IS NULL, + 'ALTER TABLE `country_dashboard_game_period_metric` ADD KEY `idx_game_period_game` (`sys_origin`, `stat_timezone`, `period_type`, `period_key`, `game_provider`, `game_id`, `country_code`)', + 'SELECT 1' + ); + PREPARE stmt FROM @ddl; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + + SET @index_columns = ( + SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) + FROM INFORMATION_SCHEMA.STATISTICS + WHERE TABLE_SCHEMA = @current_schema + AND TABLE_NAME = 'country_dashboard_game_period_metric' + AND INDEX_NAME = 'idx_game_rank_period' + ); + SET @ddl = IF( + @index_columns IS NULL, + 'ALTER TABLE `country_dashboard_game_period_metric` ADD KEY `idx_game_rank_period` (`sys_origin`, `stat_timezone`, `game_provider`, `game_id`, `period_type`, `period_key`, `country_code`)', + 'SELECT 1' + ); + PREPARE stmt FROM @ddl; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; @@ -754,6 +789,10 @@ AND t.period_key = 'ALL' + + AND t.period_key >= #{startPeriodKey} + AND t.period_key <= #{endPeriodKey} + AND t.period_start_date >= DATE(#{startTime})