Files
asd-backend/migrations/006_add_performance_indexes.sql
Milky0217 e6048ea010 feat(auth): 实现登录限流和性能优化
性能优化:
- 添加数据库索引优化查询性能 (006)
  - weather_data: user_id, date, is_favorite 索引
  - users: openid 索引
  - payment_orders: status 索引
- 新增 refresh_tokens 表支持双 Token 机制 (004)
- 新增 web_login_codes 表支持网页端扫码登录 (005)

安全增强:
- 实现基于 IP 的登录限流 (rate_limiter.rs)
  - 滑动窗口算法: 5次/分钟/IP
  - 自动清理过期记录
  - 429 TooManyRequests 响应

新模块:
- src/rate_limiter.rs: 限流模块
- src/alipay.rs: 支付宝签名模块 (RSA2)
- src/error.rs: 统一错误类型 (含 TooManyRequests)
- src/handlers/meta.rs: 元数据处理器

代码清理:
- 修复 .gitignore 规则,正确跟踪 src/ 和 migrations/
2026-04-24 16:13:49 +08:00

51 lines
2.2 KiB
SQL
Raw Permalink 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.
-- 迁移: 006_add_performance_indexes.sql
-- 目的: 为常用查询字段添加索引,提升查询性能
-- 日期: 2026-04-24
-- ============================================
-- weather_data 表索引
-- ============================================
-- 索引:加速按用户查询天气数据列表
-- 用途GET /weather 接口按 user_id + date 排序查询
CREATE INDEX IF NOT EXISTS idx_weather_data_user_id ON weather_data(user_id);
-- 索引:加速按日期排序查询
-- 用途:列表查询默认按 date DESC, hour DESC, min DESC 排序
CREATE INDEX IF NOT EXISTS idx_weather_data_date ON weather_data(date DESC);
-- 索引:加速用户收藏列表查询
-- 用途GET /api/favorites 查询 user_id + is_favorite = true
CREATE INDEX IF NOT EXISTS idx_weather_data_user_favorite ON weather_data(user_id, is_favorite);
-- 索引:复合索引加速用户数据列表(无需额外排序)
-- 用途:用户历史记录查询
CREATE INDEX IF NOT EXISTS idx_weather_data_user_date ON weather_data(user_id, date DESC);
-- ============================================
-- users 表索引
-- ============================================
-- 索引:加速 openid 查询(登录时高频使用)
-- 注意openid 可能已有 UNIQUE 约束自动创建索引,此处确保存在
CREATE INDEX IF NOT EXISTS idx_users_openid ON users(openid);
-- ============================================
-- payment_orders 表索引(补充)
-- ============================================
-- 索引:加速按状态查询待处理订单
-- 用途:管理员查询 pending 状态订单
CREATE INDEX IF NOT EXISTS idx_payment_orders_status ON payment_orders(status);
-- ============================================
-- web_login_codes 表索引(来自 005
-- ============================================
-- 确保 web_login_codes 表索引存在05已创建此处确保兼容性
-- 登录码查询和过期清理
CREATE INDEX IF NOT EXISTS idx_web_login_codes_code ON web_login_codes(code);
CREATE INDEX IF NOT EXISTS idx_web_login_codes_expires_at ON web_login_codes(expires_at);
COMMENT ON TABLE weather_data IS '添加性能索引user_id、date、is_favorite';