-- ================================================ -- 回滚数据库优化(删除可能导致问题的索引) -- ================================================ -- 如果优化后系统变慢,执行此脚本回滚 -- ================================================ USE study; SELECT '开始回滚数据库优化...' AS info; -- ================================================ -- 删除 sys_user 表的索引(保留主键和必要索引) -- ================================================ SELECT '回滚 sys_user 表索引...' AS info; -- 删除可能添加的索引(如果存在) SET @drop_idx_user_name = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_user_name' ), 'ALTER TABLE sys_user DROP INDEX idx_user_name', 'SELECT "idx_user_name 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_user_name; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_nick_name = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_nick_name' ), 'ALTER TABLE sys_user DROP INDEX idx_nick_name', 'SELECT "idx_nick_name 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_nick_name; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_email = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_email' ), 'ALTER TABLE sys_user DROP INDEX idx_email', 'SELECT "idx_email 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_email; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_dept_id = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_dept_id' ), 'ALTER TABLE sys_user DROP INDEX idx_dept_id', 'SELECT "idx_dept_id 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_dept_id; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_status = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_status' ), 'ALTER TABLE sys_user DROP INDEX idx_status', 'SELECT "idx_status 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_status; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_del_flag = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_del_flag' ), 'ALTER TABLE sys_user DROP INDEX idx_del_flag', 'SELECT "idx_del_flag 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_del_flag; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_register_type = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_register_type' ), 'ALTER TABLE sys_user DROP INDEX idx_register_type', 'SELECT "idx_register_type 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_register_type; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_student_status = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user' AND INDEX_NAME = 'idx_student_status' ), 'ALTER TABLE sys_user DROP INDEX idx_student_status', 'SELECT "idx_student_status 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_student_status; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- ================================================ -- 删除 student_class 表的索引(保留主键) -- ================================================ SELECT '回滚 student_class 表索引...' AS info; SET @drop_idx_student_id = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'student_class' AND INDEX_NAME = 'idx_student_id' ), 'ALTER TABLE student_class DROP INDEX idx_student_id', 'SELECT "idx_student_id 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_student_id; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_class_id = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'student_class' AND INDEX_NAME = 'idx_class_id' ), 'ALTER TABLE student_class DROP INDEX idx_class_id', 'SELECT "idx_class_id 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_class_id; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_student_status = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'student_class' AND INDEX_NAME = 'idx_student_status' ), 'ALTER TABLE student_class DROP INDEX idx_student_status', 'SELECT "idx_student_status 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_student_status; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET @drop_idx_status = IF( EXISTS( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'student_class' AND INDEX_NAME = 'idx_status' ), 'ALTER TABLE student_class DROP INDEX idx_status', 'SELECT "idx_status 索引不存在,跳过" AS result' ); PREPARE stmt FROM @drop_idx_status; EXECUTE stmt; DEALLOCATE PREPARE stmt; -- ================================================ -- 重新分析表 -- ================================================ SELECT '重新分析表...' AS info; ANALYZE TABLE sys_user; ANALYZE TABLE student_class; -- ================================================ -- 验证回滚结果 -- ================================================ SELECT '回滚完成,验证结果...' AS info; SELECT TABLE_NAME AS '表名', COUNT(DISTINCT INDEX_NAME) AS '剩余索引数量', GROUP_CONCAT(DISTINCT INDEX_NAME) AS '索引列表' FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME IN ('sys_user', 'student_class') GROUP BY TABLE_NAME; SELECT '回滚完成!' AS info;