-- dashboard-cdc-worker 每 5 分钟按 create_time 24 小时窗口回读 game_lucky_gift_count（1400 万行），
-- 原有索引均以 sys_origin,user_id 开头，无法按时间窗口定位，只能从 user_base_info 反向驱动全量探测
-- （单次 examined ~18 万行，是线上 MySQL CPU 波动来源之一）。
-- 本索引已于 2026-07-10 通过 ALGORITHM=INPLACE, LOCK=NONE 在生产手工应用（耗时 19s），此文件用于存档与新环境初始化。

SET @current_schema := DATABASE();

SET @add_game_lucky_gift_count_sorigin_ctime_sql := IF(
  NOT EXISTS (
    SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
    WHERE table_schema = @current_schema
      AND table_name = 'game_lucky_gift_count'
      AND index_name = 'idx_game_lucky_gift_count_sorigin_ctime'
  ),
  'ALTER TABLE `game_lucky_gift_count` ADD INDEX `idx_game_lucky_gift_count_sorigin_ctime` (`sys_origin`, `create_time`), ALGORITHM=INPLACE, LOCK=NONE',
  'SELECT 1'
);
PREPARE add_game_lucky_gift_count_sorigin_ctime_stmt FROM @add_game_lucky_gift_count_sorigin_ctime_sql;
EXECUTE add_game_lucky_gift_count_sorigin_ctime_stmt;
DEALLOCATE PREPARE add_game_lucky_gift_count_sorigin_ctime_stmt;
