Files
Edu/scripts/clickhouse-init.sql
SpecialX ca3780aa24 feat(data-ana): 完整实现 data-ana 数据分析服务
包含 CDC consumer、analytics/mastery/warning service、grpc server、repository、ClickHouse DDL 等
2026-07-10 19:09:27 +08:00

90 lines
4.1 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ClickHouse 数据库初始化脚本
-- 适用服务data-anaD6 智能洞察领域)
-- 5 张宽表ai-allocation §5 强制):
-- 1. student_dashboard_view 学生学情宽表(成绩/班级/知识点维度)
-- 2. student_errors 学生错题本
-- 3. mastery_snapshot 知识点掌握度历史快照
-- 4. ai_usage_log AI 用量计费记录ai 服务投递data-ana 消费)
-- 5. attendance_logs 学生考勤记录core-edu attendance 表 CDC 同步)
--
-- 引擎对齐 02-architecture-design.md §3ReplacingMergeTree 按 ORDER BY 去重 + version 列保留最新版本
-- 查询规范:所有 SELECT 必须加 FINAL 或使用 argMax 聚合确保去重生效P0 整改)
--
-- 使用方式(启用 ClickHouse 时执行一次):
-- clickhouse-client --multiquery < scripts/clickhouse-init.sql
-- 注意ClickHouse 为可选依赖,未配置时 data-ana 服务进入降级模式(返回骨架数据)。
-- 数据库
CREATE DATABASE IF NOT EXISTS edu_analytics;
-- ===== 1. 学生学情宽表(成绩写入产生一行,按 ORDER BY 去重保留最新版本) =====
CREATE TABLE IF NOT EXISTS edu_analytics.student_dashboard_view (
student_id String,
class_id String,
exam_id String,
subject_id String,
score Float64,
rank_in_class UInt32,
knowledge_point_id String,
mastery_level Float32, -- 0.0-1.0
error_count UInt32,
last_updated DateTime64(3, 'UTC')
) ENGINE = ReplacingMergeTree(last_updated)
PARTITION BY toYYYYMM(last_updated)
ORDER BY (student_id, exam_id, knowledge_point_id)
SETTINGS index_granularity = 8192;
-- ===== 2. 学生错题本 =====
CREATE TABLE IF NOT EXISTS edu_analytics.student_errors (
student_id String,
question_id String,
knowledge_point_id String,
error_count UInt32,
last_error_time DateTime64(3, 'UTC'),
content String
) ENGINE = ReplacingMergeTree(last_error_time)
PARTITION BY toYYYYMM(last_error_time)
ORDER BY (student_id, question_id);
-- ===== 3. 掌握度历史快照(每次掌握度计算产生新版本,支持趋势查询) =====
CREATE TABLE IF NOT EXISTS edu_analytics.mastery_snapshot (
student_id String,
knowledge_point_id String,
subject_id String,
mastery_level Float32,
calculated_at DateTime64(3, 'UTC'),
calculation_method LowCardinality(String) -- 'weighted_moving_avg' / 'simple_avg' / 'forgetting_curve'
) ENGINE = MergeTree
PARTITION BY toYYYYMM(calculated_at)
ORDER BY (student_id, knowledge_point_id, calculated_at);
-- ===== 4. AI 用量计费记录ai 服务通过 Kafka 事件投递data-ana 消费落库) =====
CREATE TABLE IF NOT EXISTS edu_analytics.ai_usage_log (
request_id String,
user_id String,
provider LowCardinality(String), -- 'openai' / 'anthropic' / 'baichuan' / 'local'
model LowCardinality(String),
prompt_tokens UInt32,
completion_tokens UInt32,
total_tokens UInt32,
latency_ms UInt32,
success Boolean,
cost_cents UInt32, -- 计费(分),便于聚合
occurred_at DateTime64(3, 'UTC')
) ENGINE = ReplacingMergeTree(occurred_at)
PARTITION BY toYYYYMM(occurred_at)
ORDER BY (request_id); -- 按 request_id 幂等去重
-- ===== 5. 学生考勤记录core-edu attendance 表 CDC 同步) =====
CREATE TABLE IF NOT EXISTS edu_analytics.attendance_logs (
student_id String,
class_id String,
attendance_date Date,
status LowCardinality(String), -- 'present' / 'absent' / 'late' / 'leave'
recorded_by String, -- 教师用户 ID
remark String DEFAULT '',
occurred_at DateTime64(3, 'UTC')
) ENGINE = ReplacingMergeTree(occurred_at)
PARTITION BY toYYYYMM(attendance_date)
ORDER BY (student_id, class_id, attendance_date);