-- ================================================
-- 验证优化是否成功执行
-- ================================================

USE study;

-- ================================================
-- 1. 检查 student_class 表的冗余索引是否已删除
-- ================================================

SELECT '=== 检查 student_class 表索引 ===' AS info;

SELECT INDEX_NAME, COLUMN_NAME, SEQ_IN_INDEX, NON_UNIQUE
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'student_class'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

-- 检查 idx_student_id 是否存在
SELECT CASE 
    WHEN COUNT(*) > 0 THEN '❌ idx_student_id 仍然存在！需要手动删除！'
    ELSE '✅ idx_student_id 已成功删除'
END AS result
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'student_class' 
AND INDEX_NAME = 'idx_student_id';

-- ================================================
-- 2. 检查 sys_user_role 表索引
-- ================================================

SELECT '=== 检查 sys_user_role 表索引 ===' AS info;

SELECT INDEX_NAME, COLUMN_NAME, SEQ_IN_INDEX
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'sys_user_role'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

-- 检查必要的索引是否存在
SELECT CASE 
    WHEN (SELECT COUNT(*) FROM information_schema.STATISTICS 
          WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user_role' 
          AND INDEX_NAME = 'idx_user_id') > 0 
    THEN '✅ idx_user_id 存在'
    ELSE '❌ idx_user_id 不存在！需要创建！'
END AS user_id_index;

SELECT CASE 
    WHEN (SELECT COUNT(*) FROM information_schema.STATISTICS 
          WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'sys_user_role' 
          AND INDEX_NAME = 'idx_role_id') > 0 
    THEN '✅ idx_role_id 存在'
    ELSE '❌ idx_role_id 不存在！需要创建！'
END AS role_id_index;

-- ================================================
-- 3. 检查 courseware 表索引
-- ================================================

SELECT '=== 检查 courseware 表索引 ===' AS info;

SELECT INDEX_NAME, COLUMN_NAME, SEQ_IN_INDEX
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'courseware'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

SELECT CASE 
    WHEN (SELECT COUNT(*) FROM information_schema.STATISTICS 
          WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'courseware' 
          AND INDEX_NAME = 'idx_course_id') > 0 
    THEN '✅ idx_course_id 存在'
    ELSE '❌ idx_course_id 不存在！需要创建！'
END AS course_id_index;

-- ================================================
-- 4. 检查 learning_detail 表索引
-- ================================================

SELECT '=== 检查 learning_detail 表索引 ===' AS info;

SELECT INDEX_NAME, COLUMN_NAME, SEQ_IN_INDEX
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'learning_detail'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

SELECT CASE 
    WHEN (SELECT COUNT(*) FROM information_schema.STATISTICS 
          WHERE TABLE_SCHEMA = 'study' AND TABLE_NAME = 'learning_detail' 
          AND INDEX_NAME = 'idx_student_course') > 0 
    THEN '✅ idx_student_course 存在'
    ELSE '❌ idx_student_course 不存在！需要创建！'
END AS student_course_index;

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

SELECT '=== 测试查询性能 ===' AS info;

-- 测试角色查询
SET @start = NOW(6);
SELECT COUNT(*) FROM sys_user_role WHERE user_id = 455;
SET @end = NOW(6);
SELECT TIMESTAMPDIFF(MICROSECOND, @start, @end)/1000 AS '角色查询耗时(ms)';

-- 测试课程查询
SET @start = NOW(6);
SELECT COUNT(*) FROM courseware WHERE course_id = 6;
SET @end = NOW(6);
SELECT TIMESTAMPDIFF(MICROSECOND, @start, @end)/1000 AS '课程查询耗时(ms)';

-- 测试学习详情查询
SET @start = NOW(6);
SELECT COUNT(*) FROM learning_detail WHERE student_id = 9999 AND course_id = 6;
SET @end = NOW(6);
SELECT TIMESTAMPDIFF(MICROSECOND, @start, @end)/1000 AS '学习详情查询耗时(ms)';

-- ================================================
-- 6. 显示结论
-- ================================================

SELECT '=== 优化结果总结 ===' AS info;

SELECT 
    '如果所有索引都是 ✅，且查询耗时 <50ms，则优化成功！' AS '结论',
    '如果有 ❌，请查看下面的手动修复SQL' AS '说明';
