-- ================================================
-- 删除冗余索引，恢复系统速度
-- ================================================
-- 问题：idx_student_id 与 uk_student_class 的第一列重复
-- 导致：写入性能下降，系统变慢
-- ================================================

USE study;

SELECT '开始删除冗余索引...' AS info;

-- ================================================
-- 1. 删除 student_class 表的冗余索引
-- ================================================

SELECT '删除 student_class.idx_student_id (冗余，uk_student_class已覆盖)' AS info;

-- 检查索引是否存在
SELECT COUNT(*) INTO @index_exists
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'student_class' 
AND INDEX_NAME = 'idx_student_id';

-- 如果存在则删除
SET @sql = IF(@index_exists > 0,
    'ALTER TABLE student_class DROP INDEX idx_student_id',
    'SELECT "idx_student_id 不存在" AS result'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ================================================
-- 2. 删除 idx_class_id（如果不需要）
-- ================================================
-- 注意：只有当确认不需要单独的 class_id 查询时才删除
-- 如果有 WHERE class_id = ? 的查询，建议保留

SELECT '检查 idx_class_id 是否需要保留...' AS info;

-- 暂时保留 idx_class_id，因为可能有按 class_id 查询的需求
-- 如果确认不需要，取消下面的注释
-- ALTER TABLE student_class DROP INDEX idx_class_id;

SELECT 'idx_class_id 已保留（如果不需要可手动删除）' AS result;

-- ================================================
-- 3. 检查 sys_user 表的索引（可能也有冗余）
-- ================================================

SELECT '检查 sys_user 表索引...' AS info;

SELECT 
    INDEX_NAME AS '索引名',
    GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS '列',
    INDEX_TYPE AS '类型',
    CASE NON_UNIQUE WHEN 0 THEN '唯一' ELSE '非唯一' END AS '属性'
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study'
AND TABLE_NAME = 'sys_user'
GROUP BY INDEX_NAME, INDEX_TYPE, NON_UNIQUE
ORDER BY INDEX_NAME;

-- ================================================
-- 建议删除的 sys_user 索引（如果是优化时添加的）
-- ================================================

SELECT '删除 sys_user 的优化索引（如果存在）...' AS info;

-- idx_user_name（如果有唯一索引或其他索引覆盖）
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_nick_name
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_email
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_dept_id
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_status
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_del_flag
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_register_type
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_student_status
SET @sql = 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 @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ================================================
-- 4. 重新分析表
-- ================================================

SELECT '重新分析表...' AS info;

ANALYZE TABLE sys_user;
ANALYZE TABLE student_class;

-- ================================================
-- 5. 验证优化结果
-- ================================================

SELECT '优化完成，验证结果...' AS info;

-- 查看 student_class 剩余索引
SELECT '剩余索引 - student_class:' AS info;
SELECT 
    INDEX_NAME AS '索引名',
    GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS '列'
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study'
AND TABLE_NAME = 'student_class'
GROUP BY INDEX_NAME
ORDER BY INDEX_NAME;

-- 查看 sys_user 剩余索引
SELECT '剩余索引 - sys_user:' AS info;
SELECT 
    INDEX_NAME AS '索引名',
    GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS '列'
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study'
AND TABLE_NAME = 'sys_user'
GROUP BY INDEX_NAME
ORDER BY INDEX_NAME;

-- 统计索引数量
SELECT 
    TABLE_NAME AS '表名',
    COUNT(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;
SELECT '如果需要保留某些索引，请手动添加回来。' AS info;
