| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- -- 迁移文件: 048_add_performance_indexes.sql
- -- 描述: 为高频查询字段添加性能优化索引
- -- 需求: 7.1 - 数据库查询优化
- -- ============================================
- -- 用户表 (users) 索引
- -- ============================================
- -- phone 索引已存在于 005_create_users_table.sql
- -- created_at 索引已存在于 038_create_stats_indexes.sql
- -- ============================================
- -- API 日志表 (api_log) 索引
- -- ============================================
- -- 用户日志查询:按用户ID和时间范围查询
- -- 注:idx_api_log_user_created 已存在于 041_add_api_log_indexes.sql
- -- 接口统计:按接口路径和时间范围统计
- CREATE INDEX IF NOT EXISTS idx_api_log_path_created
- ON aigcspace.api_log(api_path, created_at DESC);
- -- 错误日志筛选:按响应状态码筛选
- CREATE INDEX IF NOT EXISTS idx_api_log_response_status
- ON aigcspace.api_log(response_status);
- -- ============================================
- -- 消费记录表 (user_consumption) 索引
- -- ============================================
- -- 用户消费查询:idx_user_consumption_user_created 已存在于 030_create_user_consumption.sql
- -- 服务类型统计:按模型名称和时间范围统计
- CREATE INDEX IF NOT EXISTS idx_user_consumption_model_created
- ON aigcspace.user_consumption(model_name, created_at DESC);
- -- ============================================
- -- 充值记录表 (user_recharge_record) 索引
- -- ============================================
- -- 用户充值查询:按用户ID和时间范围查询
- CREATE INDEX IF NOT EXISTS idx_user_recharge_user_created
- ON aigcspace.user_recharge_record(user_id, created_at DESC);
- -- status 索引已存在于 018_create_user_recharge_record.sql 和 038_create_stats_indexes.sql
- -- ============================================
- -- 对话表 (ai_conversation) 索引
- -- ============================================
- -- 用户对话列表:按用户ID和时间范围查询
- CREATE INDEX IF NOT EXISTS idx_ai_conversation_user_created
- ON aigcspace.ai_conversation(user_id, created_at DESC);
- -- ============================================
- -- 消息表 (ai_message) 索引
- -- ============================================
- -- 对话消息列表:按会话ID和时间范围查询
- CREATE INDEX IF NOT EXISTS idx_ai_message_conv_created
- ON aigcspace.ai_message(conversation_id, created_at DESC);
- -- ============================================
- -- 回滚迁移(注释掉,仅作参考)
- -- ============================================
- -- DROP INDEX IF EXISTS idx_api_log_path_created;
- -- DROP INDEX IF EXISTS idx_api_log_response_status;
- -- DROP INDEX IF EXISTS idx_user_consumption_model_created;
- -- DROP INDEX IF EXISTS idx_user_recharge_user_created;
- -- DROP INDEX IF EXISTS idx_ai_conversation_user_created;
- -- DROP INDEX IF EXISTS idx_ai_message_conv_created;
|