-- statistic 服务 events 原始表 -- 创建时间: 2026-06-08 -- 说明: 事件原始表,按 received_at 按日分区 -- 关联: spec §3.2 -- 1. 创建 schema CREATE SCHEMA IF NOT EXISTS statistic; -- 2. 创建 events 主表(按 received_at 按日分区) CREATE TABLE IF NOT EXISTS statistic.events ( id BIGSERIAL, event_id UUID NOT NULL, user_id BIGINT NOT NULL, star_id BIGINT NOT NULL, event_type VARCHAR(64) NOT NULL, occurred_at TIMESTAMPTZ NOT NULL, received_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), properties JSONB NOT NULL DEFAULT '{}', PRIMARY KEY (id, received_at) ) PARTITION BY RANGE (received_at); -- 3. 唯一约束(去重):同一 event_id 不能重复 CREATE UNIQUE INDEX IF NOT EXISTS idx_events_event_id ON statistic.events (event_id, received_at); -- 4. 看板查询主索引(覆盖 90% 查询) CREATE INDEX IF NOT EXISTS idx_events_user_star_type_time ON statistic.events (user_id, star_id, event_type, received_at DESC); -- 5. 趋势分析索引 CREATE INDEX IF NOT EXISTS idx_events_star_type_time ON statistic.events (star_id, event_type, received_at DESC); -- 6. JSONB 属性 GIN 索引 CREATE INDEX IF NOT EXISTS idx_events_properties_gin ON statistic.events USING GIN (properties);