-- ================================================
-- 导入性能优化SQL（安全版本）
-- 功能：添加必要的索引，提升导入速度
-- 特点：自动检查索引是否存在，避免重复创建
-- 执行前：备份数据库
-- ================================================

USE study;

-- ================================================
-- 1. 检查现有索引
-- ================================================

SELECT '查看 sys_user 表的索引' AS info;
SHOW INDEX FROM sys_user;

SELECT '查看 student_class 表的索引' AS info;
SHOW INDEX FROM student_class;

-- ================================================
-- 2. 安全地添加索引（跳过已存在的索引）
-- ================================================

-- 为 sys_user 表添加索引
SELECT '开始为 sys_user 表添加索引...' AS info;

-- idx_user_name
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE sys_user ADD INDEX idx_user_name (user_name)',
        'SELECT ''索引 idx_user_name 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'sys_user' 
    AND index_name = 'idx_user_name'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_nick_name
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE sys_user ADD INDEX idx_nick_name (nick_name)',
        'SELECT ''索引 idx_nick_name 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'sys_user' 
    AND index_name = 'idx_nick_name'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_prison_area
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE sys_user ADD INDEX idx_prison_area (prison_area)',
        'SELECT ''索引 idx_prison_area 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'sys_user' 
    AND index_name = 'idx_prison_area'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 为 student_class 表添加索引
SELECT '开始为 student_class 表添加索引...' AS info;

-- idx_student_id
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE student_class ADD INDEX idx_student_id (student_id)',
        'SELECT ''索引 idx_student_id 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'student_class' 
    AND index_name = 'idx_student_id'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_class_id
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE student_class ADD INDEX idx_class_id (class_id)',
        'SELECT ''索引 idx_class_id 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'student_class' 
    AND index_name = 'idx_class_id'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_status
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE student_class ADD INDEX idx_status (status)',
        'SELECT ''索引 idx_status 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'student_class' 
    AND index_name = 'idx_status'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- idx_student_status (复合索引)
SET @sql = (
    SELECT IF(
        COUNT(*) = 0,
        'ALTER TABLE student_class ADD INDEX idx_student_status (student_id, status)',
        'SELECT ''索引 idx_student_status 已存在，跳过'' AS result'
    )
    FROM information_schema.statistics 
    WHERE table_schema = 'study' 
    AND table_name = 'student_class' 
    AND index_name = 'idx_student_status'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT '索引添加完成！' AS info;

-- ================================================
-- 3. 分析表统计信息
-- ================================================

SELECT '更新表统计信息...' AS info;
ANALYZE TABLE sys_user;
ANALYZE TABLE student_class;
ANALYZE TABLE study_class;

-- ================================================
-- 4. 验证索引创建结果
-- ================================================

SELECT '验证 sys_user 表索引' AS info;
SHOW INDEX FROM sys_user;

SELECT '验证 student_class 表索引' AS info;
SHOW INDEX FROM student_class;

-- 详细的索引列表
SELECT 
    TABLE_NAME AS '表名',
    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 IN ('sys_user', 'student_class')
GROUP BY TABLE_NAME, INDEX_NAME, INDEX_TYPE, NON_UNIQUE
ORDER BY TABLE_NAME, INDEX_NAME;

-- ================================================
-- 5. 性能测试查询
-- ================================================

SELECT '测试索引效果...' AS info;

-- 测试1：按user_name查询（应使用idx_user_name）
EXPLAIN SELECT * FROM sys_user WHERE user_name = '201';

-- 测试2：按student_id查询（应使用idx_student_id）
EXPLAIN SELECT * FROM student_class WHERE student_id = 234;

-- 测试3：按student_id和status查询（应使用idx_student_status）
EXPLAIN SELECT * FROM student_class WHERE student_id = 234 AND status = 1;

-- ================================================
-- 完成！
-- ================================================

SELECT '优化完成！' AS info;
SELECT 'Indexes added successfully! Please test import performance.' AS result;
