perf(console): optimize game metric ranking queries

This commit is contained in:
hy001 2026-05-13 18:00:56 +08:00
parent 83b61e94dd
commit f79d460f10
3 changed files with 74 additions and 11 deletions

View File

@ -89,13 +89,14 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
Long total = countryDashboardDAO.countGamePeriodMetrics( Long total = countryDashboardDAO.countGamePeriodMetrics(
condition.periodType, condition.startTime, condition.endTime, condition.periodType, condition.startTime, condition.endTime,
PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null,
condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone,
condition.sysOrigin); condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin);
List<CountryDashboardGameMetricCO> records = countryDashboardDAO.listGamePeriodMetrics( List<CountryDashboardGameMetricCO> records = countryDashboardDAO.listGamePeriodMetrics(
condition.periodType, condition.startTime, condition.endTime, condition.periodType, condition.startTime, condition.endTime,
PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null,
condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone,
condition.sysOrigin, sortSpec.column(), sortSpec.direction(), offset, limit) condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin,
sortSpec.column(), sortSpec.direction(), offset, limit)
.stream() .stream()
.map(this::toCO) .map(this::toCO)
.toList(); .toList();
@ -133,13 +134,14 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
Long total = countryDashboardDAO.countGameSummaryMetrics( Long total = countryDashboardDAO.countGameSummaryMetrics(
condition.periodType, condition.startTime, condition.endTime, condition.periodType, condition.startTime, condition.endTime,
PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null,
condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone,
condition.sysOrigin); condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin);
List<CountryDashboardGameMetricCO> records = countryDashboardDAO.listGameSummaryMetrics( List<CountryDashboardGameMetricCO> records = countryDashboardDAO.listGameSummaryMetrics(
condition.periodType, condition.startTime, condition.endTime, condition.periodType, condition.startTime, condition.endTime,
PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null,
condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone,
condition.sysOrigin, sortSpec.column(), sortSpec.direction(), offset, limit) condition.countryKeyword, countryCodes, gameProvider, gameKeyword, condition.sysOrigin,
sortSpec.column(), sortSpec.direction(), offset, limit)
.stream() .stream()
.map(this::toCO) .map(this::toCO)
.toList(); .toList();
@ -168,8 +170,9 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
return countryDashboardDAO.listGameCountryRankMetrics( return countryDashboardDAO.listGameCountryRankMetrics(
condition.periodType, condition.startTime, condition.endTime, condition.periodType, condition.startTime, condition.endTime,
PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null, PERIOD_ALL.equals(condition.periodType) ? PERIOD_ALL : null,
condition.statTimezone, condition.countryKeyword, countryCodes, gameProvider, null, condition.startPeriodKey, condition.endPeriodKey, condition.statTimezone,
gameId, condition.sysOrigin, limit) condition.countryKeyword, countryCodes, gameProvider, null, gameId, condition.sysOrigin,
limit)
.stream() .stream()
.map(this::toCO) .map(this::toCO)
.toList(); .toList();
@ -639,12 +642,15 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
private final LocalDate endDateValue; private final LocalDate endDateValue;
private final LocalDateTime startTime; private final LocalDateTime startTime;
private final LocalDateTime endTime; private final LocalDateTime endTime;
private final String startPeriodKey;
private final String endPeriodKey;
private final String countryKeyword; private final String countryKeyword;
private final String sysOrigin; private final String sysOrigin;
private QueryCondition(String periodType, String statTimezone, ZoneId statZoneId, private QueryCondition(String periodType, String statTimezone, ZoneId statZoneId,
LocalDate startDateValue, LocalDate endDateValue, LocalDateTime startTime, 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.periodType = periodType;
this.statTimezone = statTimezone; this.statTimezone = statTimezone;
this.statZoneId = statZoneId; this.statZoneId = statZoneId;
@ -652,6 +658,8 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
this.endDateValue = endDateValue; this.endDateValue = endDateValue;
this.startTime = startTime; this.startTime = startTime;
this.endTime = endTime; this.endTime = endTime;
this.startPeriodKey = startPeriodKey;
this.endPeriodKey = endPeriodKey;
this.countryKeyword = countryKeyword; this.countryKeyword = countryKeyword;
this.sysOrigin = sysOrigin; this.sysOrigin = sysOrigin;
} }
@ -693,6 +701,10 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
: LocalDateTime.ofInstant(startInstant, STORAGE_ZONE_ID); : LocalDateTime.ofInstant(startInstant, STORAGE_ZONE_ID);
LocalDateTime storageEndTime = endInstant == null ? null LocalDateTime storageEndTime = endInstant == null ? null
: LocalDateTime.ofInstant(endInstant, STORAGE_ZONE_ID); : 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( return new QueryCondition(
periodType, periodType,
statZoneId.getId(), statZoneId.getId(),
@ -701,6 +713,8 @@ public class CountryDashboardGameMetricServiceImpl implements CountryDashboardGa
end, end,
storageStartTime, storageStartTime,
storageEndTime, storageEndTime,
startPeriodKey,
endPeriodKey,
trimToNull(cmd == null ? null : cmd.getCountryKeyword()), trimToNull(cmd == null ? null : cmd.getCountryKeyword()),
trimToNull(cmd == null ? null : cmd.getSysOrigin()) trimToNull(cmd == null ? null : cmd.getSysOrigin())
); );

View File

@ -342,6 +342,8 @@ public interface CountryDashboardDAO extends BaseDAO<IUserBaseInfo> {
@Param("startTime") LocalDateTime startTime, @Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime, @Param("endTime") LocalDateTime endTime,
@Param("periodKey") String periodKey, @Param("periodKey") String periodKey,
@Param("startPeriodKey") String startPeriodKey,
@Param("endPeriodKey") String endPeriodKey,
@Param("statTimezone") String statTimezone, @Param("statTimezone") String statTimezone,
@Param("countryKeyword") String countryKeyword, @Param("countryKeyword") String countryKeyword,
@Param("countryCodes") String countryCodes, @Param("countryCodes") String countryCodes,
@ -354,6 +356,8 @@ public interface CountryDashboardDAO extends BaseDAO<IUserBaseInfo> {
@Param("startTime") LocalDateTime startTime, @Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime, @Param("endTime") LocalDateTime endTime,
@Param("periodKey") String periodKey, @Param("periodKey") String periodKey,
@Param("startPeriodKey") String startPeriodKey,
@Param("endPeriodKey") String endPeriodKey,
@Param("statTimezone") String statTimezone, @Param("statTimezone") String statTimezone,
@Param("countryKeyword") String countryKeyword, @Param("countryKeyword") String countryKeyword,
@Param("countryCodes") String countryCodes, @Param("countryCodes") String countryCodes,
@ -370,6 +374,8 @@ public interface CountryDashboardDAO extends BaseDAO<IUserBaseInfo> {
@Param("startTime") LocalDateTime startTime, @Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime, @Param("endTime") LocalDateTime endTime,
@Param("periodKey") String periodKey, @Param("periodKey") String periodKey,
@Param("startPeriodKey") String startPeriodKey,
@Param("endPeriodKey") String endPeriodKey,
@Param("statTimezone") String statTimezone, @Param("statTimezone") String statTimezone,
@Param("countryKeyword") String countryKeyword, @Param("countryKeyword") String countryKeyword,
@Param("countryCodes") String countryCodes, @Param("countryCodes") String countryCodes,
@ -382,6 +388,8 @@ public interface CountryDashboardDAO extends BaseDAO<IUserBaseInfo> {
@Param("startTime") LocalDateTime startTime, @Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime, @Param("endTime") LocalDateTime endTime,
@Param("periodKey") String periodKey, @Param("periodKey") String periodKey,
@Param("startPeriodKey") String startPeriodKey,
@Param("endPeriodKey") String endPeriodKey,
@Param("statTimezone") String statTimezone, @Param("statTimezone") String statTimezone,
@Param("countryKeyword") String countryKeyword, @Param("countryKeyword") String countryKeyword,
@Param("countryCodes") String countryCodes, @Param("countryCodes") String countryCodes,
@ -398,6 +406,8 @@ public interface CountryDashboardDAO extends BaseDAO<IUserBaseInfo> {
@Param("startTime") LocalDateTime startTime, @Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime, @Param("endTime") LocalDateTime endTime,
@Param("periodKey") String periodKey, @Param("periodKey") String periodKey,
@Param("startPeriodKey") String startPeriodKey,
@Param("endPeriodKey") String endPeriodKey,
@Param("statTimezone") String statTimezone, @Param("statTimezone") String statTimezone,
@Param("countryKeyword") String countryKeyword, @Param("countryKeyword") String countryKeyword,
@Param("countryCodes") String countryCodes, @Param("countryCodes") String countryCodes,

View File

@ -121,6 +121,41 @@
KEY `idx_game_lookup` (`sys_origin`, `stat_timezone`, `game_provider`, `game_id`, `period_type`, `period_key`), 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`) 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='国家数据大屏游戏维度周期预聚合' ) 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;
</update> </update>
<update id="createGamePeriodUserMetricTable"> <update id="createGamePeriodUserMetricTable">
@ -754,6 +789,10 @@
<when test="periodType == 'ALL'"> <when test="periodType == 'ALL'">
AND t.period_key = 'ALL' AND t.period_key = 'ALL'
</when> </when>
<when test="periodType == 'DAY' and startPeriodKey != null and startPeriodKey != '' and endPeriodKey != null and endPeriodKey != ''">
AND t.period_key &gt;= #{startPeriodKey}
AND t.period_key &lt;= #{endPeriodKey}
</when>
<otherwise> <otherwise>
<if test="startTime != null"> <if test="startTime != null">
AND t.period_start_date &gt;= DATE(#{startTime}) AND t.period_start_date &gt;= DATE(#{startTime})