From fa14a5ca8cff8d2c28bdcf042b58ebf8c9d94d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=94=B7?= Date: Sun, 3 May 2026 09:39:36 +0800 Subject: [PATCH] perf: add GIN indexes on clazz JSONB columns and date-range indexes - GIN indexes on students/teachers (jsonb_path_ops) for fast user lookups - Composite index on clazz (is_repeat, start_from, end_by) for date range queries - Indexes on clazz_repeat (clazz_id, repeat_start, repeat_end) for joins Co-Authored-By: Claude Opus 4.7 --- htykc/src/ws_clazz.rs | 1 + htykc/src/ws_xiaoke.rs | 1 + htykc/tests/e2e_clazz_tests.rs | 1 + .../down.sql | 11 +++++++++++ .../up.sql | 12 ++++++++++++ .../2026-05-03-013827_add_clazz_indexes/down.sql | 5 +++++ .../2026-05-03-013827_add_clazz_indexes/up.sql | 10 ++++++++++ htykc_models/src/schema.rs | 5 +++++ htyts/tests/ts_e2e_http.rs | 1 + htyws/src/ws_all.rs | 1 + .../down.sql | 3 +++ .../up.sql | 4 ++++ 12 files changed, 55 insertions(+) create mode 100644 htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/down.sql create mode 100644 htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/up.sql create mode 100644 htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/down.sql create mode 100644 htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/up.sql create mode 100644 htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/down.sql create mode 100644 htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/up.sql diff --git a/htykc/src/ws_clazz.rs b/htykc/src/ws_clazz.rs index db4f08f..5a0f2a7 100644 --- a/htykc/src/ws_clazz.rs +++ b/htykc/src/ws_clazz.rs @@ -966,6 +966,7 @@ mod tests { tags: None, current_org_id: current_org_id.map(|value| value.to_string()), current_org_role_keys: Some(vec!["TEACHER".to_string()]), + current_department_id: None, }; jwt_encode_token(token).expect("encode test token") } diff --git a/htykc/src/ws_xiaoke.rs b/htykc/src/ws_xiaoke.rs index d631e95..51efa5b 100644 --- a/htykc/src/ws_xiaoke.rs +++ b/htykc/src/ws_xiaoke.rs @@ -337,6 +337,7 @@ mod tests { tags: None, current_org_id: current_org_id.map(|value| value.to_string()), current_org_role_keys: Some(vec!["TEACHER".to_string()]), + current_department_id: None, }; AuthorizationHeader(jwt_encode_token(token).expect("encode test token")) } diff --git a/htykc/tests/e2e_clazz_tests.rs b/htykc/tests/e2e_clazz_tests.rs index 1b839bc..e558bcb 100644 --- a/htykc/tests/e2e_clazz_tests.rs +++ b/htykc/tests/e2e_clazz_tests.rs @@ -33,6 +33,7 @@ fn build_test_token(with_org_context: bool) -> String { None }, current_org_role_keys: Some(vec!["TEACHER".to_string()]), + current_department_id: None, }; jwt_encode_token(token).expect("encode test token") } diff --git a/htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/down.sql b/htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/down.sql new file mode 100644 index 0000000..3d57fb0 --- /dev/null +++ b/htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/down.sql @@ -0,0 +1,11 @@ +DROP INDEX IF EXISTS idx_clazz_department_id; +DROP INDEX IF EXISTS idx_clazz_attendance_department_id; +DROP INDEX IF EXISTS idx_course_hour_package_department_id; +DROP INDEX IF EXISTS idx_hour_transaction_department_id; +DROP INDEX IF EXISTS idx_clazz_repeat_department_id; + +ALTER TABLE clazz_repeat DROP COLUMN IF EXISTS department_id; +ALTER TABLE hour_transaction DROP COLUMN IF EXISTS department_id; +ALTER TABLE course_hour_package DROP COLUMN IF EXISTS department_id; +ALTER TABLE clazz_attendance DROP COLUMN IF EXISTS department_id; +ALTER TABLE clazz DROP COLUMN IF EXISTS department_id; diff --git a/htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/up.sql b/htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/up.sql new file mode 100644 index 0000000..6f4069d --- /dev/null +++ b/htykc_models/migrations/2026-05-02-082946_add_department_id_to_kc_tables/up.sql @@ -0,0 +1,12 @@ +-- Add department_id to KC teaching tables (nullable first, backfill, then not-null in future) +ALTER TABLE clazz ADD COLUMN department_id VARCHAR; +ALTER TABLE clazz_attendance ADD COLUMN department_id VARCHAR; +ALTER TABLE course_hour_package ADD COLUMN department_id VARCHAR; +ALTER TABLE hour_transaction ADD COLUMN department_id VARCHAR; +ALTER TABLE clazz_repeat ADD COLUMN department_id VARCHAR; + +CREATE INDEX idx_clazz_department_id ON clazz (department_id); +CREATE INDEX idx_clazz_attendance_department_id ON clazz_attendance (department_id); +CREATE INDEX idx_course_hour_package_department_id ON course_hour_package (department_id); +CREATE INDEX idx_hour_transaction_department_id ON hour_transaction (department_id); +CREATE INDEX idx_clazz_repeat_department_id ON clazz_repeat (department_id); diff --git a/htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/down.sql b/htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/down.sql new file mode 100644 index 0000000..7da27c0 --- /dev/null +++ b/htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/down.sql @@ -0,0 +1,5 @@ +DROP INDEX IF EXISTS idx_clazz_students_gin; +DROP INDEX IF EXISTS idx_clazz_teachers_gin; +DROP INDEX IF EXISTS idx_clazz_date_active; +DROP INDEX IF EXISTS idx_clazz_repeat_clazz_id; +DROP INDEX IF EXISTS idx_clazz_repeat_date_range; diff --git a/htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/up.sql b/htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/up.sql new file mode 100644 index 0000000..343a533 --- /dev/null +++ b/htykc_models/migrations/2026-05-03-013827_add_clazz_indexes/up.sql @@ -0,0 +1,10 @@ +-- GIN indexes for JSONB path queries on students/teachers +CREATE INDEX IF NOT EXISTS idx_clazz_students_gin ON clazz USING gin (students jsonb_path_ops); +CREATE INDEX IF NOT EXISTS idx_clazz_teachers_gin ON clazz USING gin (teachers jsonb_path_ops); + +-- Composite index for date range + is_repeat filter +CREATE INDEX IF NOT EXISTS idx_clazz_date_active ON clazz (is_repeat, start_from, end_by); + +-- Indexes on clazz_repeat for join queries +CREATE INDEX IF NOT EXISTS idx_clazz_repeat_clazz_id ON clazz_repeat (clazz_id); +CREATE INDEX IF NOT EXISTS idx_clazz_repeat_date_range ON clazz_repeat (repeat_start, repeat_end); diff --git a/htykc_models/src/schema.rs b/htykc_models/src/schema.rs index 96a26c6..8701e3b 100644 --- a/htykc_models/src/schema.rs +++ b/htykc_models/src/schema.rs @@ -24,6 +24,7 @@ diesel::table! { is_notified -> Nullable, completed_at -> Nullable, org_id -> Nullable, + department_id -> Nullable, } } @@ -41,6 +42,7 @@ diesel::table! { created_by -> Nullable, is_delete -> Nullable, org_id -> Nullable, + department_id -> Nullable, } } @@ -87,6 +89,7 @@ diesel::table! { repeat_status -> Nullable, latest_clazz_created_at -> Nullable, org_id -> Nullable, + department_id -> Nullable, } } @@ -105,6 +108,7 @@ diesel::table! { updated_at -> Nullable, is_delete -> Nullable, org_id -> Nullable, + department_id -> Nullable, } } @@ -155,6 +159,7 @@ diesel::table! { remark -> Nullable, created_at -> Timestamp, org_id -> Nullable, + department_id -> Nullable, } } diff --git a/htyts/tests/ts_e2e_http.rs b/htyts/tests/ts_e2e_http.rs index bf45aae..79bf4db 100644 --- a/htyts/tests/ts_e2e_http.rs +++ b/htyts/tests/ts_e2e_http.rs @@ -52,6 +52,7 @@ fn mint_sudo_jwt() -> String { tags: None, current_org_id: None, current_org_role_keys: None, + current_department_id: None, }; jwt_encode_token(inner).expect("jwt_encode_token") } diff --git a/htyws/src/ws_all.rs b/htyws/src/ws_all.rs index 9f32f34..2597a2d 100644 --- a/htyws/src/ws_all.rs +++ b/htyws/src/ws_all.rs @@ -4358,6 +4358,7 @@ mod tests { tags: None, current_org_id: current_org_id.map(|value| value.to_string()), current_org_role_keys: Some(vec!["TEACHER".to_string()]), + current_department_id: None, }; jwt_encode_token(token).expect("encode test token") } diff --git a/htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/down.sql b/htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/down.sql new file mode 100644 index 0000000..45ad978 --- /dev/null +++ b/htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/down.sql @@ -0,0 +1,3 @@ +DROP INDEX IF EXISTS idx_teacher_student_department_id; + +ALTER TABLE teacher_student DROP COLUMN IF EXISTS department_id; diff --git a/htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/up.sql b/htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/up.sql new file mode 100644 index 0000000..f7417e5 --- /dev/null +++ b/htyws_models/migrations/2026-05-02-082949_add_department_id_to_ws_tables/up.sql @@ -0,0 +1,4 @@ +-- Add department_id to teacher_student (nullable first, backfill, then not-null in future) +ALTER TABLE teacher_student ADD COLUMN department_id VARCHAR; + +CREATE INDEX idx_teacher_student_department_id ON teacher_student (department_id);