SET @schema_name := DATABASE();

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE `la_article` ADD INDEX `idx_kb_recent_article` (`update_time`, `id`, `is_show`, `delete_time`)',
        'SELECT 1'
    )
    FROM `information_schema`.`statistics`
    WHERE `table_schema` = @schema_name
      AND `table_name` = 'la_article'
      AND `index_name` = 'idx_kb_recent_article'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE `la_match` ADD INDEX `idx_kb_recent_match` (`update_time`, `id`, `is_show`, `delete_time`)',
        'SELECT 1'
    )
    FROM `information_schema`.`statistics`
    WHERE `table_schema` = @schema_name
      AND `table_name` = 'la_match'
      AND `index_name` = 'idx_kb_recent_match'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE `la_community_post` ADD INDEX `idx_kb_recent_post` (`update_time`, `id`, `status`, `delete_time`)',
        'SELECT 1'
    )
    FROM `information_schema`.`statistics`
    WHERE `table_schema` = @schema_name
      AND `table_name` = 'la_community_post'
      AND `index_name` = 'idx_kb_recent_post'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE `la_lottery_draw_result` ADD INDEX `idx_kb_recent_draw` (`update_time`, `id`)',
        'SELECT 1'
    )
    FROM `information_schema`.`statistics`
    WHERE `table_schema` = @schema_name
      AND `table_name` = 'la_lottery_draw_result'
      AND `index_name` = 'idx_kb_recent_draw'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql := (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE `la_lottery_ai_analysis` ADD INDEX `idx_kb_recent_ai_history` (`update_time`, `id`)',
        'SELECT 1'
    )
    FROM `information_schema`.`statistics`
    WHERE `table_schema` = @schema_name
      AND `table_name` = 'la_lottery_ai_analysis'
      AND `index_name` = 'idx_kb_recent_ai_history'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
