-- ================================================
-- 修复慢查询问题
-- ================================================
-- 问题：角色查询、课程查询、学习详情查询都很慢（0.5-0.8秒）
-- 原因：缺少必要的索引
-- ================================================

USE study;

SELECT '开始优化慢查询索引...' AS info;

-- ================================================
-- 1. 检查并优化 sys_user_role 表索引
-- ================================================

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

-- 查看当前索引
SELECT DISTINCT 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;

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

-- 如果不存在则创建
SET @sql = IF(@user_id_index_exists = 0,
    'ALTER TABLE sys_user_role ADD INDEX idx_user_id (user_id)',
    'SELECT "idx_user_id 已存在" AS result'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

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

-- 如果不存在则创建
SET @sql = IF(@role_id_index_exists = 0,
    'ALTER TABLE sys_user_role ADD INDEX idx_role_id (role_id)',
    'SELECT "idx_role_id 已存在" AS result'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ================================================
-- 2. 检查并优化 sys_role 表索引
-- ================================================

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

-- 查看当前索引
SELECT DISTINCT INDEX_NAME, COLUMN_NAME, SEQ_IN_INDEX
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'sys_role'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

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

-- 如果不存在则创建
SET @sql = IF(@del_flag_index_exists = 0,
    'ALTER TABLE sys_role ADD INDEX idx_del_flag (del_flag)',
    'SELECT "idx_del_flag 已存在" AS result'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ================================================
-- 3. 检查并优化 courseware 表索引
-- ================================================

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

-- 查看当前索引
SELECT DISTINCT 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;

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

-- 如果不存在则创建
SET @sql = IF(@course_id_index_exists = 0,
    'ALTER TABLE courseware ADD INDEX idx_course_id (course_id)',
    'SELECT "idx_course_id 已存在" AS result'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ================================================
-- 4. 检查并优化 learning_detail 表索引
-- ================================================

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

-- 查看当前索引
SELECT DISTINCT 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;

-- 检查复合索引 (student_id, course_id) 是否存在
SELECT COUNT(*) INTO @student_course_index_exists
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'learning_detail' 
AND INDEX_NAME = 'idx_student_course';

-- 如果不存在则创建
SET @sql = IF(@student_course_index_exists = 0,
    'ALTER TABLE learning_detail ADD INDEX idx_student_course (student_id, course_id)',
    'SELECT "idx_student_course 已存在" AS result'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ================================================
-- 5. 分析表以更新统计信息
-- ================================================

SELECT '分析表以更新统计信息...' AS info;

ANALYZE TABLE sys_user_role;
ANALYZE TABLE sys_role;
ANALYZE TABLE courseware;
ANALYZE TABLE learning_detail;
ANALYZE TABLE sys_user;

-- ================================================
-- 6. 显示优化后的索引情况
-- ================================================

SELECT '优化完成！以下是各表的索引情况：' AS info;

SELECT '=== sys_user_role 表索引 ===' AS info;
SELECT DISTINCT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'sys_user_role'
GROUP BY INDEX_NAME;

SELECT '=== sys_role 表索引 ===' AS info;
SELECT DISTINCT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'sys_role'
GROUP BY INDEX_NAME;

SELECT '=== courseware 表索引 ===' AS info;
SELECT DISTINCT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'courseware'
GROUP BY INDEX_NAME;

SELECT '=== learning_detail 表索引 ===' AS info;
SELECT DISTINCT INDEX_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'study' 
AND TABLE_NAME = 'learning_detail'
GROUP BY INDEX_NAME;

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

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)';

SELECT '所有优化完成！预期查询速度提升到 10-50ms' AS info;
