-- V121: 清理被覆盖索引完全覆盖的冗余二级索引(2026-09 全维度代码审查发现) -- -- 判定原则:被删索引的列集合是另一索引的**严格前缀**(或完全同列), -- 即所有能用它的查询都能被保留的索引覆盖,删除无功能损失,只减少写入时的索引维护与 buffer pool 占用。 -- -- 1) biz_file_result.idx_file_result_patrol_history_desc (module_type,user_id,created_at DESC,id DESC) -- 与 V42 的 idx_file_result_patrol_history (module_type,user_id,created_at,id) 同列。 -- MySQL 8 反向扫描 ASC 索引即可满足全 DESC 排序,DESC 变体只在「混合升降序」的 ORDER BY 下才有优势, -- 本表查询(巡逻删除历史)为全 DESC,故 V46 的这份可删。 -- 2) biz_task_result_item.idx_result_item_task (task_id,module_type) -- 是 idx_result_item_country_asin (task_id,module_type,country_code,asin) 的前缀。 -- (country_code 在写入侧恒 NULL、从不查询,被覆盖关系成立。) -- 3) biz_task_chunk.idx_task_scope (task_id,module_type,scope_hash) -- 是 uk_task_scope_chunk (task_id,module_type,scope_hash,chunk_index) 的前缀。 -- 4) biz_task_chunk.idx_task_module (task_id,module_type) -- 同为 uk_task_scope_chunk 的前缀。 -- -- 风险:DROP INDEX 为在线操作(INPLACE),但会立即改变执行计划;均为「被覆盖」判定, -- 保留索引可完整替代。若线上观察到计划退化,按末尾注释回滚即可。 -- -- 回滚: -- ALTER TABLE biz_file_result ADD INDEX idx_file_result_patrol_history_desc (module_type, user_id, created_at DESC, id DESC); -- ALTER TABLE biz_task_result_item ADD INDEX idx_result_item_task (task_id, module_type); -- ALTER TABLE biz_task_chunk ADD INDEX idx_task_scope (task_id, module_type, scope_hash); -- ALTER TABLE biz_task_chunk ADD INDEX idx_task_module (task_id, module_type); SET @db_name = DATABASE(); -- 1) biz_file_result.idx_file_result_patrol_history_desc SET @idx_exists := ( SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'biz_file_result' AND INDEX_NAME = 'idx_file_result_patrol_history_desc' ); SET @sql := IF(@idx_exists > 0, 'ALTER TABLE biz_file_result DROP INDEX idx_file_result_patrol_history_desc', 'SELECT 1' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- 2) biz_task_result_item.idx_result_item_task SET @idx_exists := ( SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'biz_task_result_item' AND INDEX_NAME = 'idx_result_item_task' ); SET @sql := IF(@idx_exists > 0, 'ALTER TABLE biz_task_result_item DROP INDEX idx_result_item_task', 'SELECT 1' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- 3) biz_task_chunk.idx_task_scope SET @idx_exists := ( SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'biz_task_chunk' AND INDEX_NAME = 'idx_task_scope' ); SET @sql := IF(@idx_exists > 0, 'ALTER TABLE biz_task_chunk DROP INDEX idx_task_scope', 'SELECT 1' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- 4) biz_task_chunk.idx_task_module SET @idx_exists := ( SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'biz_task_chunk' AND INDEX_NAME = 'idx_task_module' ); SET @sql := IF(@idx_exists > 0, 'ALTER TABLE biz_task_chunk DROP INDEX idx_task_module', 'SELECT 1' ); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;