66 lines
2.0 KiB
SQL
66 lines
2.0 KiB
SQL
-- ================================================
|
|
-- 手动修复索引(简化版,直接执行)
|
|
-- ================================================
|
|
|
|
USE study;
|
|
|
|
-- ================================================
|
|
-- 1. 删除冗余索引(如果存在)
|
|
-- ================================================
|
|
|
|
-- 删除 student_class 的冗余索引
|
|
ALTER TABLE student_class DROP INDEX IF EXISTS idx_student_id;
|
|
|
|
SELECT '✅ student_class.idx_student_id 已删除(如果存在)' AS result;
|
|
|
|
-- ================================================
|
|
-- 2. 创建必要的索引(如果不存在)
|
|
-- ================================================
|
|
|
|
-- sys_user_role 表索引
|
|
ALTER TABLE sys_user_role ADD INDEX IF NOT EXISTS idx_user_id (user_id);
|
|
ALTER TABLE sys_user_role ADD INDEX IF NOT EXISTS idx_role_id (role_id);
|
|
|
|
SELECT '✅ sys_user_role 索引已创建' AS result;
|
|
|
|
-- sys_role 表索引
|
|
ALTER TABLE sys_role ADD INDEX IF NOT EXISTS idx_del_flag (del_flag);
|
|
|
|
SELECT '✅ sys_role 索引已创建' AS result;
|
|
|
|
-- courseware 表索引
|
|
ALTER TABLE courseware ADD INDEX IF NOT EXISTS idx_course_id (course_id);
|
|
|
|
SELECT '✅ courseware 索引已创建' AS result;
|
|
|
|
-- learning_detail 表索引
|
|
ALTER TABLE learning_detail ADD INDEX IF NOT EXISTS idx_student_course (student_id, course_id);
|
|
|
|
SELECT '✅ learning_detail 索引已创建' AS result;
|
|
|
|
-- ================================================
|
|
-- 3. 分析表
|
|
-- ================================================
|
|
|
|
ANALYZE TABLE sys_user_role;
|
|
ANALYZE TABLE sys_role;
|
|
ANALYZE TABLE courseware;
|
|
ANALYZE TABLE learning_detail;
|
|
ANALYZE TABLE student_class;
|
|
ANALYZE TABLE sys_user;
|
|
|
|
SELECT '✅ 所有表已分析' AS result;
|
|
|
|
-- ================================================
|
|
-- 4. 测试性能
|
|
-- ================================================
|
|
|
|
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)';
|
|
|
|
SELECT '✅ 所有优化完成!' AS result;
|