海量日志
场景
两年半后,数据量激增。
数据规模:
- 日均调用量:200 万次
- 日志表记录:每天 200 万条
- 累计数据:18 个月 × 30 天 × 200 万 = 10 亿条
问题:
解决方案
日志表涨到 10 亿条后,单靠调索引已经救不回来。这一节的思路是横向拆分:分库分表让写入散开,冷热分离让查询只碰热数据,搜索引擎承担复杂的多条件检索。
1. 分库分表
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
endpoint | VARCHAR(255) | NOT NULL |
params | TEXT | 请求参数 |
response_status | INT | HTTP 状态码 |
response_time | INT | 响应时间(ms) |
created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
| id | endpoint | params | response_status | response_time | created_at |
|---|
1 | /api/weather | city=北京 | 200 | 118 | 2026-03-31 14:00:00 |
2 | /api/weather | city=上海 | 200 | 132 | 2026-03-31 14:01:00 |
3 | /api/weather | city=广州 | 500 | 240 | 2026-03-31 14:02:00 |
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | INT | PRIMARY KEY, AUTO_INCREMENT |
email | VARCHAR(255) | UNIQUE, NOT NULL |
password_hash | VARCHAR(255) | NOT NULL |
api_key | VARCHAR(64) | UNIQUE, NOT NULL |
plan | VARCHAR(50) | DEFAULT 'free' |
daily_limit | INT | DEFAULT 1000 |
created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
| id | email | plan | daily_limit | created_at |
|---|
1 | alice@demo.com | free | 1000 | 2026-04-08 10:32:11 |
2 | bob@demo.com | pro | 100000 | 2026-04-11 09:15:02 |
3 | ops@demo.com | basic | 10000 | 2026-04-13 21:44:36 |
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
user_id | INT | NOT NULL |
endpoint | VARCHAR(255) | NOT NULL |
hour | TIMESTAMP | 精确到小时 |
total_calls | INT | DEFAULT 0 |
success_calls | INT | DEFAULT 0 |
error_calls | INT | DEFAULT 0 |
avg_response_time | FLOAT | 平均响应时间(ms) |
created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
| user_id | endpoint | hour | total_calls | success_calls | error_calls |
|---|
1001 | /api/weather | 2026-04-20 10:00 | 132 | 128 | 4 |
1002 | /api/forecast | 2026-04-20 10:00 | 91 | 90 | 1 |
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
user_id | INT | NOT NULL |
endpoint | VARCHAR(255) | NOT NULL |
date | DATE | 统计日期 |
total_calls | INT | DEFAULT 0 |
success_calls | INT | DEFAULT 0 |
error_calls | INT | DEFAULT 0 |
avg_response_time | FLOAT | 平均响应时间(ms) |
p95_response_time | FLOAT | 95分位响应时间(ms) |
p99_response_time | FLOAT | 99分位响应时间(ms) |
created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
| user_id | endpoint | date | total_calls | success_calls | error_calls |
|---|
1001 | /api/weather | 2026-04-20 | 2410 | 2368 | 42 |
1002 | /api/forecast | 2026-04-20 | 1832 | 1810 | 22 |
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | BIGINT | PRIMARY KEY, AUTO_INCREMENT |
user_id | INT | NOT NULL |
month | DATE | 每月1号 |
total_calls | INT | DEFAULT 0 |
total_success | INT | DEFAULT 0 |
total_error | INT | DEFAULT 0 |
avg_daily_calls | FLOAT | 日均调用次数 |
peak_hour_calls | INT | 峰值小时调用量 |
most_used_endpoint | VARCHAR(255) | 最常调用接口 |
created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
| user_id | month | total_calls | total_success | total_error | avg_daily_calls |
|---|
1001 | 2026-04 | 58420 | 57110 | 1310 | 1947 |
1002 | 2026-04 | 40680 | 39920 | 760 | 1356 |
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | INT | PRIMARY KEY, AUTO_INCREMENT |
name | VARCHAR(100) | UNIQUE, NOT NULL |
display_name | VARCHAR(100) | NOT NULL |
endpoint | VARCHAR(255) | NOT NULL |
method | VARCHAR(10) | DEFAULT 'GET' |
category | VARCHAR(50) | weather/news/finance/tools |
external_api_url | VARCHAR(255) | 上游服务地址 |
requires_auth | BOOLEAN | DEFAULT TRUE |
cache_ttl | INT | 缓存时间(秒) |
rate_limit_factor | INT | DEFAULT 1 |
status | VARCHAR(20) | active/deprecated/maintenance |
is_public | BOOLEAN | DEFAULT TRUE |
created_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
updated_at | TIMESTAMP | ON UPDATE CURRENT_TIMESTAMP |
| id | name | display_name | category | method | cache_ttl | status |
|---|
1 | weather | 天气查询 | weather | GET | 3600 | active |
2 | news | 新闻快讯 | news | GET | 300 | active |
3 | stock | 股票行情 | finance | GET | 60 | maintenance |
user_api_permissionsFields| 字段名 | 类型 | 约束 / 说明 |
|---|
id | INT | PRIMARY KEY, AUTO_INCREMENT |
user_id | INT | NOT NULL |
api_id | INT | NOT NULL, FK -> api_definitions.id |
granted_at | TIMESTAMP | DEFAULT CURRENT_TIMESTAMP |
expires_at | TIMESTAMP | NULL 表示长期有效 |
status | VARCHAR(20) | active/revoked/expired |
| id | user_id | api_id | granted_at | expires_at | status |
|---|
8001 | 101 | 1 | 2026-04-10 10:00:00 | NULL | active |
8002 | 101 | 2 | 2026-04-10 10:00:00 | 2026-05-10 10:00:00 | active |
8003 | 204 | 3 | 2026-04-01 09:15:00 | 2026-04-30 23:59:59 | expired |
| 字段名 | 类型 | 约束 / 说明 |
|---|
id | BIGINT | NOT NULL, AUTO_INCREMENT |
user_id | INT | NOT NULL |
endpoint | VARCHAR(255) | NOT NULL |
params | TEXT | 请求参数 |
response_time | INT | 毫秒 |
status | VARCHAR(50) | 调用状态 |
created_at | TIMESTAMP | 分区键 |
pk | PRIMARY KEY(id, created_at) | 支持范围分区 |
| partition | range | records | storage | hotness |
|---|
p202401 | < 2024-02-01 | 58,200,000 | 370 GB | cold |
p202402 | < 2024-03-01 | 61,900,000 | 395 GB | warm |
pmax | MAXVALUE | ongoing | rolling | hot |
2. 冷热分离
冷热分离要回答的是「哪些数据还会被查」。按时间切分是最简单可靠的办法:越近的数据留在越贵的存储上,越久远的数据越往便宜的地方挪,查询路径随之变短。
设计流程2. 冷热分离
- 步骤 1:按时间把日志划成热(近 30 天)、温(30 到 180 天)、冷(180 天以上)三层
- 步骤 2:热数据留在 MySQL 主库并建好时间索引,温数据下沉到从库分担查询
- 步骤 3:冷数据迁到低成本对象存储,只保留归档索引供后续检索
- 步骤 4:核对各层的数据条数与查询命中情况,确认分层后主库体积和查询耗时都下降
3. 使用 Elasticsearch
当业务需要按多个条件组合检索、还要做统计时,SQL 索引已经不够用了。这时候引入 Elasticsearch,把「查日志」这件事从数据库里彻底搬出去。
设计流程3. 使用 Elasticsearch
- 步骤 1:设计日志在 ES 中的索引与字段映射,把常用查询条件做成可检索字段
- 步骤 2:用采集任务把增量日志同步进 ES,并按保留期滚动删除过期索引
- 步骤 3:用关键字、分页和聚合表达业务查询,让调用量统计不再压数据库
- 步骤 4:监控写入延迟和索引体积,必要时调大刷新间隔或提前合并小索引
关注点:索引结构设计、写入延迟与索引生命周期管理。
当前架构
GET https://dashboard.kuaiyizhi.cn/api/v3/weather?city=北京