create-dwy 0.9.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/src/create.js +5 -2
- package/src/prompts/grouped-checkbox.js +223 -0
- package/src/sync.js +45 -19
- package/templates/claude-global/hooks/pre-git-commit-sensitive-check.sh +0 -0
- package/templates/claude-global/rules/dwy-android-core.md +1 -0
- package/templates/claude-global/rules/dwy-db-migration.md +1 -0
- package/templates/claude-global/rules/dwy-flutter-core.md +1 -0
- package/templates/claude-global/rules/dwy-flutter-network.md +1 -0
- package/templates/claude-global/rules/dwy-flutter-router.md +1 -0
- package/templates/claude-global/rules/dwy-flutter-testing.md +1 -0
- package/templates/claude-global/rules/dwy-postgres.md +1 -0
- package/templates/claude-global/rules/dwy-python-backend.md +44 -3
- package/templates/claude-global/rules/dwy-python-core.md +36 -2
- package/templates/claude-global/rules/dwy-python-testing.md +1 -0
- package/templates/claude-global/rules/dwy-redis.md +1 -0
- package/templates/claude-global/rules/dwy-tdd-development.md +1 -0
- package/templates/claude-global/rules/dwy-vue-core.md +3 -43
- package/templates/claude-global/rules/dwy-vue-pinia.md +1 -0
- package/templates/claude-global/rules/dwy-vue-testing.md +1 -0
- package/templates/claude-global/skills/dwy-cibuildwheel/SKILL.md +355 -0
- package/templates/claude-global/skills/dwy-cibuildwheel/references/release-workflow-template.yml +99 -0
- package/templates/claude-global/skills/dwy-deploy-audit/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_base.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_capacity.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_db.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_docker.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_env.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_https.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_logs.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_nginx.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_resilience.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_secrets.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_services.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/check_ssh.sh +0 -0
- package/templates/claude-global/skills/dwy-deploy-audit/scripts/run_all.sh +0 -0
- package/templates/claude-global/skills/dwy-dev-qa/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-docker/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-docker/scripts/query_dockerhub.py +0 -0
- package/templates/claude-global/skills/dwy-docker/scripts/scan_images.sh +0 -0
- package/templates/claude-global/skills/dwy-dolphindb/SKILL.md +123 -76
- package/templates/claude-global/skills/dwy-dolphindb/references/create-database-table.md +219 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/crud-operations.md +281 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/data-import.md +271 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/docker-deployment.md +63 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/performance-tuning.md +223 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/pitfalls-and-best-practices.md +352 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/python-api.md +306 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/setup-and-connection.md +143 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/sources.md +46 -0
- package/templates/claude-global/skills/dwy-dolphindb/references/sql-batch-compute.md +206 -0
- package/templates/claude-global/skills/dwy-eapi/SKILL.md +134 -402
- package/templates/claude-global/skills/dwy-ekit/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-eui/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-fullstack-scaffold/SKILL.md +14 -7
- package/templates/claude-global/skills/dwy-git-commit/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-mirror-source/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-mirror-source/scripts/apply_mirrors.py +0 -0
- package/templates/claude-global/skills/dwy-mirror-source/scripts/check_mirrors.py +0 -0
- package/templates/claude-global/skills/dwy-pentest/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-publish/SKILL.md +1 -0
- package/templates/claude-global/skills/dwy-tech-stack/SKILL.md +1 -0
- package/templates/project/fullstack-monorepo/CLAUDE.md.hbs +21 -9
- package/templates/project/fullstack-monorepo/backend/Dockerfile.prod +40 -0
- package/templates/project/fullstack-monorepo/backend/_env.example.hbs +26 -0
- package/templates/project/fullstack-monorepo/docker-compose.dev.yml.hbs +4 -4
- package/templates/project/fullstack-monorepo/docker-compose.prod.yml.hbs +116 -0
- package/templates/project/fullstack-monorepo/docs/.gitkeep +0 -0
- package/templates/project/fullstack-monorepo/frontend/_env.example.hbs +19 -0
- package/templates/project/fullstack-monorepo/{dev.sh.hbs → scripts/dev.sh.hbs} +2 -2
- package/templates/claude-global/skills/dwy-dolphindb/evals/evals.json +0 -67
- package/templates/claude-global/skills/dwy-dolphindb/references/dolphindb-rules.md +0 -441
- package/templates/claude-global/skills/dwy-eapi/references/health.md +0 -47
- package/templates/claude-global/skills/dwy-eapi/references/logger.md +0 -60
- package/templates/project/fullstack-monorepo/_env.example.hbs +0 -24
|
@@ -1,98 +1,145 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: dwy-dolphindb
|
|
3
3
|
description: >
|
|
4
|
-
DolphinDB
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
dolphindb
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
SYMBOL ≤255 字节、单分区大小落在引擎推荐区间等硬约束。
|
|
4
|
+
DolphinDB 3.0 社区版(2核8G / 1控2节点)开发与审查 skill:写 DolphinDB Script (.dos)、
|
|
5
|
+
用 Python SDK (dolphindb 3.0.4)、设计 DFS 分区表(TSDB/OLAP 引擎选型)、导入 tick/snapshot/
|
|
6
|
+
kline 金融数据、优化慢查询、排查 OOM / 卡死作业、Docker 部署。也用于代码审查:检查分区
|
|
7
|
+
裁剪(链式比较 `a <= col <= b`、函数包裹分区列)、批量写入 ≤500K 行、SYMBOL ≤255 字节、
|
|
8
|
+
STRING/BLOB 静默截断、输入校验防脚本注入、连接池配置、`pool.run` async/sync 用法。
|
|
9
|
+
关键字触发:dolphindb, DDB, loadTable, loadTextEx, createPartitionedTable, append!,
|
|
10
|
+
dropPartition, DBConnectionPool, tableAppender, PartitionedTableAppender,
|
|
11
|
+
MultithreadedTableWriter, addRangePartitions, newValuePartitionPolicy, TSDB, OLAP,
|
|
12
|
+
getClusterPerf, getConsoleJobs, level2 行情, tick 数据, 时序数据库, 8848 端口, .dos 脚本.
|
|
13
|
+
category: 数据库
|
|
15
14
|
---
|
|
16
15
|
|
|
17
|
-
# DolphinDB
|
|
16
|
+
# DolphinDB 3.0 社区版使用指南
|
|
18
17
|
|
|
19
|
-
##
|
|
18
|
+
## 当前环境
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
- 代码审查(CR)时发现文件包含 DolphinDB 调用
|
|
31
|
-
|
|
32
|
-
## 使用步骤
|
|
20
|
+
| 项 | 值 |
|
|
21
|
+
|---|---|
|
|
22
|
+
| 版本 | DolphinDB 3.0 社区版 |
|
|
23
|
+
| 拓扑 | 1 控制节点 + 2 计算/数据节点 |
|
|
24
|
+
| 资源 | 2 核 / 8GB 内存(已分配 3GB),磁盘 33 TB |
|
|
25
|
+
| 端口 | Web/控制 `http://localhost:8848`,集群另含 `192.168.0.107:8848` / `172.17.0.1:8848` 等 |
|
|
26
|
+
| Worker | 4 线程,连接上限 512(当前 15) |
|
|
27
|
+
| Python SDK | `dolphindb==3.0.4` |
|
|
28
|
+
| 场景 | 个人/小团队 + 教学,**单查询内存上限按 2GB 控制**(详见 [[performance-tuning]])|
|
|
33
29
|
|
|
34
|
-
|
|
30
|
+
## 何时进入哪份 reference
|
|
35
31
|
|
|
36
|
-
|
|
32
|
+
| 你正在做 | 看这里 |
|
|
33
|
+
|---|---|
|
|
34
|
+
| 第一次连 DDB / `import dolphindb` 报错 / 8848 进不去 | [[setup-and-connection]] |
|
|
35
|
+
| 设计 DFS 分区表 / 选 TSDB 还是 OLAP / 给 level2 建表 | [[create-database-table]] |
|
|
36
|
+
| `loadText` 慢 / 想多线程导入 / Python 写 DataFrame 进 DDB | [[data-import]] |
|
|
37
|
+
| 写 SELECT/INSERT/UPDATE/DELETE / `delete` 删一整个分区 | [[crud-operations]] |
|
|
38
|
+
| 合成 K 线 / 算 SOIR 订单失衡 / 截面排名 / 用 `mr` 分布式跑批 | [[sql-batch-compute]] |
|
|
39
|
+
| Python SDK 用法 / `session.run` 高级参数 / 流订阅 / 异步写 / 类型映射 | [[python-api]] |
|
|
40
|
+
| 查询慢 / OOM / 节点卡住 / 内存涨不下来 / 不知道哪个作业在跑 | [[performance-tuning]] |
|
|
41
|
+
| 时间类型踩坑 / DECIMAL 精度 / SYMBOL 长度 / backtick 单行 / 反模式 | [[pitfalls-and-best-practices]] |
|
|
42
|
+
| Docker 部署 / Alpine 镜像坑 / healthcheck / 资源限制 | [[docker-deployment]] |
|
|
43
|
+
| 官方文档链接 / 版本基线 / GitHub 仓库 | [[sources]] |
|
|
44
|
+
|
|
45
|
+
## 30 秒速查
|
|
46
|
+
|
|
47
|
+
```dolphindb
|
|
48
|
+
// 连接(Python)
|
|
49
|
+
import dolphindb as ddb
|
|
50
|
+
s = ddb.session()
|
|
51
|
+
s.connect("localhost", 8848, "admin", "123456")
|
|
52
|
+
|
|
53
|
+
// 看集群健康
|
|
54
|
+
s.run("getClusterPerf()") // CPU/内存/磁盘
|
|
55
|
+
s.run("getRecentJobs(20)") // 最近 20 个批作业
|
|
56
|
+
s.run("pnodeRun(getConsoleJobs)") // 所有节点同步作业
|
|
57
|
+
s.run("getSessionMemoryStat()") // 当前 session 内存
|
|
58
|
+
|
|
59
|
+
// 看库表
|
|
60
|
+
s.run("getClusterDFSDatabases()") // 所有 DFS 库
|
|
61
|
+
s.run("getClusterDFSTables('dfs://xxx')") // 库下所有表
|
|
62
|
+
s.run("schema(loadTable('dfs://xxx','t'))") // 表结构
|
|
63
|
+
|
|
64
|
+
// 杀作业(按 rootJobId)
|
|
65
|
+
s.run("cancelJob(`xxxx)") // 异步作业
|
|
66
|
+
s.run("cancelConsoleJob(`xxxx)") // 同步作业
|
|
67
|
+
```
|
|
37
68
|
|
|
38
|
-
|
|
69
|
+
## 核心原则(社区版 2核8G 必须遵守)
|
|
39
70
|
|
|
40
|
-
|
|
71
|
+
1. **单查询不超 2GB**:8GB 内存 → 系统/缓存 2GB + worker 缓冲 2GB + 单查询预算 ≤ 2GB。先 `count()` 估行数 × 列宽,超了就走分区分批或 `mr`。
|
|
72
|
+
2. **任何 WHERE 必带分区列**:否则全表扫,2 节点直接 OOM。用 `select [HINT_EXPLAIN] ...` 验证是否触发分区剪枝。
|
|
73
|
+
3. **TSDB 排序列别选高基数**:`sortColumns` 不要用主键/唯一列(索引膨胀);金融场景用 `[SecurityID, TradeTime]`。
|
|
74
|
+
4. **写入用 PartitionedTableAppender + DBConnectionPool**:单连接 + `tableInsert` 慢,4 worker → pool size 3。
|
|
75
|
+
5. **删数据按分区删**:`dropPartition()` 秒级,`delete from` 全表扫慢且占内存。
|
|
41
76
|
|
|
42
|
-
|
|
43
|
-
|---|---|
|
|
44
|
-
| 查询脚本编写 | 三(分区裁剪)、八(查询优化) |
|
|
45
|
-
| 建库建表/分区方案设计 | 十二(分区设计原则) |
|
|
46
|
-
| 批量数据写入 | 二(连接管理)、四(批量写入)、十(数据类型映射)、十三(字符串长度限制) |
|
|
47
|
-
| 脚本拼接/动态生成 | 五(脚本语法)、六(输入校验) |
|
|
48
|
-
| 服务层 Python 代码 | 二(连接管理)、六(输入校验)、十(数据类型映射) |
|
|
49
|
-
| Docker/部署配置 | 九(Docker 环境) |
|
|
50
|
-
| 测试代码 | 十一(测试 Mock) |
|
|
51
|
-
| 代码审查/CR | 全部,尤其是十四(检查清单) |
|
|
77
|
+
## 反模式(看到立即停手)
|
|
52
78
|
|
|
53
|
-
|
|
79
|
+
| 反模式 | 后果 | 改成 |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| `select * from t where 2024.01.01 <= TradeDate <= 2024.01.05` | 链式条件不触发剪枝 → 全表扫 | `where TradeDate between 2024.01.01 and 2024.01.05` |
|
|
82
|
+
| `delete from t where TradeDate=2024.01.01` | 重写整个分区,慢+占内存 | `dropPartition(database(dbName), 2024.01.01, tableName='t')` |
|
|
83
|
+
| 在客户端 `s.run("select * from huge_table")` | 一次拉全部,OOM | `s.run("...", fetchSize=8192)` 分段读 |
|
|
84
|
+
| DECIMAL 列用 FLOAT/DOUBLE | 精度丢失(金融场景禁用) | `DECIMAL32(3)` / `DECIMAL64(6)` |
|
|
85
|
+
| `DATETIME` 存 2038 后的时间 | 32 位 int 溢出 → 显示 1970 | 全用 `TIMESTAMP`(毫秒精度 64 位) |
|
|
86
|
+
| 建表时 `sortColumns` 用 UUID/序列号 | 索引膨胀,TSDB 查询变慢 | 用 `[业务键, 时间列]` 组合 |
|
|
54
87
|
|
|
55
|
-
|
|
88
|
+
**完整反模式清单**(类型陷阱 / SQL 陷阱 / 库表设计 / 写入 / 运维)见 [[pitfalls-and-best-practices]]。
|
|
56
89
|
|
|
57
|
-
|
|
90
|
+
## 审查模式(代码 review / CR 用)
|
|
58
91
|
|
|
59
|
-
|
|
92
|
+
任务是**审查**他人代码时,按以下格式输出:
|
|
60
93
|
|
|
61
94
|
```
|
|
62
|
-
⚠️ [
|
|
95
|
+
⚠️ [reference名] 具体问题
|
|
63
96
|
违规代码:<摘录>
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
对符合规则的代码,可以简要确认:
|
|
97
|
+
修复建议:<可直接替换的代码>
|
|
98
|
+
原因:<对应 reference 里的依据,一句话>
|
|
69
99
|
|
|
100
|
+
✅ [reference名] 符合要求 — <简要说明>
|
|
70
101
|
```
|
|
71
|
-
✅ [规则章节] 符合要求 — <简要说明>
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### 4. 特别关注以下致命违规
|
|
75
|
-
|
|
76
|
-
以下问题会直接导致生产故障或 OOM,必须零容忍:
|
|
77
|
-
|
|
78
|
-
1. **分区列被函数包裹**(如 `date(trade_date)`)→ 全表扫描 → 8GB OOM
|
|
79
|
-
2. **链式比较过滤**(如 `2022.12.01 <= TradeDate <= 2022.12.03`)→ 不触发分区剪枝 → 全表扫描
|
|
80
|
-
3. **单次 append 超过 500K 行** → TSDB 报错 `exceeds max limit`
|
|
81
|
-
4. **未校验的外部输入拼接到脚本** → 脚本注入风险
|
|
82
|
-
5. **连接池做 upload + run** → 变量跨连接不可见
|
|
83
|
-
6. **select *** → 传输全部 30+ 列,浪费带宽和内存
|
|
84
|
-
7. **分页使用 `LIMIT x OFFSET y`** → DolphinDB 不支持 OFFSET 关键字
|
|
85
|
-
8. **`asyncio.to_thread(pool.run, script)`** → SDK v3 的 `pool.run()` 已是原生 async,无需包装
|
|
86
|
-
9. **SYMBOL 列写入未校验长度** → 超 255 字节抛异常,整批写入失败
|
|
87
|
-
10. **建表分区方案过细**(如每天 5000+ 个 0.6MB 小分区)→ 命中 `maxPartitionNumPerQuery=65536` 限制
|
|
88
|
-
|
|
89
|
-
### 5. 检查清单应用
|
|
90
|
-
|
|
91
|
-
如果任务是**代码审查**或**最终确认**,逐条过一遍 `references/dolphindb-rules.md` 第十四章的检查清单(13 项),并给出通过/不通过的明确结论。
|
|
92
|
-
|
|
93
|
-
## 输出要求
|
|
94
102
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
### 10 条致命违规(零容忍)
|
|
104
|
+
|
|
105
|
+
| # | 违规 | 后果 |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| 1 | 分区列被函数包裹(`date(col)` / `year(col)` / `temporalAdd(col,...)`) | 全表扫 → OOM |
|
|
108
|
+
| 2 | 链式比较 `a <= col <= b` | 不触发剪枝 → 全表扫 |
|
|
109
|
+
| 3 | 单次 `append` > 500K 行 | TSDB 报 `exceeds max limit` |
|
|
110
|
+
| 4 | 未校验外部输入拼到脚本 | 脚本注入 |
|
|
111
|
+
| 5 | 连接池里 upload + run(变量跨连接不可见) | 变量找不到 |
|
|
112
|
+
| 6 | `select *`(30+ 列全传) | 浪费带宽和内存 |
|
|
113
|
+
| 7 | 分页用 `LIMIT x OFFSET y` | DDB 不支持 OFFSET 关键字 |
|
|
114
|
+
| 8 | `asyncio.to_thread(pool.run, script)` | SDK v3 `pool.run` 已是原生 async |
|
|
115
|
+
| 9 | SYMBOL 列写入未校验 ≤255 字节 | 整批写入失败 |
|
|
116
|
+
| 10 | 单分区 < 100MB 且总分区数 > 65536 | 触达 `maxPartitionNumPerQuery` 上限 |
|
|
117
|
+
|
|
118
|
+
完整审查清单 ↓
|
|
119
|
+
|
|
120
|
+
## 变更检查清单(写 / 改 DDB 代码前过一遍)
|
|
121
|
+
|
|
122
|
+
- [ ] WHERE 是否命中分区列?分区列是否被函数包裹(`year()` / `date()` / `temporalAdd()`)?
|
|
123
|
+
- [ ] 范围条件用了 `between ... and ...` 或两个独立条件?没有链式比较 `a <= col <= b`?
|
|
124
|
+
- [ ] 单次 `append` ≤ 500K 行?循环里有 `del + gc.collect()`?
|
|
125
|
+
- [ ] 大文件用 pyarrow `read_table + slice`,不是 `pd.read_feather` 全量加载?
|
|
126
|
+
- [ ] 拼接 DDB 脚本时,外部输入有正则白名单校验?(防注入)
|
|
127
|
+
- [ ] `pool.run()` 用 `await`(原生 async),`Session.run()` 用 `asyncio.to_thread()`?
|
|
128
|
+
- [ ] 连接池大小 ≤ CPU 核数 × 2(社区版 4 worker → pool 3)?
|
|
129
|
+
- [ ] `select` 只取需要的列?没有 `select *`?
|
|
130
|
+
- [ ] 多次聚合查询能否合并为一次?
|
|
131
|
+
- [ ] 多因子横向查询用了 `pivot by`,不是 Python 端 merge?
|
|
132
|
+
- [ ] backtick 列名定义在单行内?
|
|
133
|
+
- [ ] 分页用 `limit offset, count`(**不是** `LIMIT x OFFSET y`)?
|
|
134
|
+
- [ ] 新建库表:单分区估算大小落在引擎区间?总分区数远低于 65536?二级高基数列走 `HASH` 不是 `VALUE`?
|
|
135
|
+
- [ ] 写入 SYMBOL 列前校验 ≤ 255 字节?STRING/BLOB 有长度断言防静默截断?
|
|
136
|
+
- [ ] 删数据用 `dropPartition`,不是 `delete from`?
|
|
137
|
+
- [ ] pandas → DDB:`uint` 是否 `.astype('int64')`?NaN→int 是否 `.fillna(0)`?
|
|
138
|
+
- [ ] Session / Pool 有 `try/finally close()`?
|
|
139
|
+
|
|
140
|
+
## 必读官方资料
|
|
141
|
+
|
|
142
|
+
完整源 URL 见 [[sources]]。常用 3 个:
|
|
143
|
+
- 金融入门:https://docs.dolphindb.cn/zh/tutorials/new_users_finance.html
|
|
144
|
+
- 使用须知:https://docs.dolphindb.cn/zh/tutorials/usage_guidelines.html
|
|
145
|
+
- Python SDK:https://docs.dolphindb.cn/zh/pydoc/ (3.0.4)
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# 建库建表
|
|
2
|
+
|
|
3
|
+
## 30 秒选型
|
|
4
|
+
|
|
5
|
+
| 你的数据 | 引擎 | 分区策略 | 排序列 |
|
|
6
|
+
|---|---|---|---|
|
|
7
|
+
| 行情快照 / tick / 逐笔 / level2 | **TSDB** | `VALUE(日期), HASH(SymbolField, N)` | `[SecurityID, TradeTime]` |
|
|
8
|
+
| 分钟/日 K 线 / 因子结果 | **OLAP** | `VALUE(日期)` 或 `RANGE(月份)` | — |
|
|
9
|
+
| 维度信息(股票基础信息表等) | — | 维度表(不分区) | — |
|
|
10
|
+
| 流式行情写入 + 实时查询 | TSDB | 同 tick | 同 tick |
|
|
11
|
+
|
|
12
|
+
**OLAP vs TSDB**:
|
|
13
|
+
- OLAP = 列存,写入快,**不支持点查/更新**。适合 K 线、因子、批分析。
|
|
14
|
+
- TSDB = 列存 + 索引,**支持 sortKey 排序+索引**,秒级点查。适合 tick/snapshot/逐笔,分区 400MB-1GB。
|
|
15
|
+
- 选错了改不了,整个库 drop 重建。
|
|
16
|
+
|
|
17
|
+
## 分区策略
|
|
18
|
+
|
|
19
|
+
| 类型 | 关键字 | 自动扩展 | 适用 |
|
|
20
|
+
|---|---|---|---|
|
|
21
|
+
| VALUE | `VALUE(2024.01.01..2024.12.31)` | ✅ | 日期、状态 |
|
|
22
|
+
| HASH | `HASH([SYMBOL, 50])` | ✅ | 高基数列(股票代码) |
|
|
23
|
+
| RANGE | `RANGE(0 100 200 300)` | ❌ 需 `addRangePartitions` | 数值范围 |
|
|
24
|
+
| LIST | `LIST([['A','B'], ['C']])` | ❌ | 明确分组 |
|
|
25
|
+
| COMPO | `partitioned by VALUE(...), HASH(...)` | 看子分区 | 复合(最多 3 级)|
|
|
26
|
+
|
|
27
|
+
**分区层级硬限制**:1-3 级。常用 2 级 = `日期 + 股票代码 HASH`。
|
|
28
|
+
|
|
29
|
+
**分区粒度目标**:单分区压缩后 **TSDB 400MB-1GB / OLAP 100MB-300MB**。粒度太细 → 元数据爆炸;太粗 → 查询不能并行。
|
|
30
|
+
|
|
31
|
+
**`maxPartitionNumPerQuery` 硬限**:默认 65536。单次查询触达的分区数超过这个值直接报 `The number of partitions relevant to the query is too large`。
|
|
32
|
+
|
|
33
|
+
### 分区设计反例(**新手最常踩**)
|
|
34
|
+
|
|
35
|
+
> 股票行情数据(每天 5000 万条),先按 `TradeDate` 值分区,再按 `SecurityID` 二级 VALUE 分区。
|
|
36
|
+
> A 股 5000+ 标的 → **一天 5000 个分区,每个仅 0.6MB**。
|
|
37
|
+
|
|
38
|
+
后果:
|
|
39
|
+
- 查 14 天 → 70000 分区 → 超 65536 上限报错
|
|
40
|
+
- 单分区 0.6MB 远低于 TSDB 400MB 下限,元数据开销 > 数据本身
|
|
41
|
+
- 列存收益丧失,查询比单级分区还慢
|
|
42
|
+
|
|
43
|
+
**修复**:二级改 `HASH([SYMBOL, 50])`(固定 50 桶),每天 50 个分区,单分区 60MB(仍偏小但元数据 OK)。
|
|
44
|
+
|
|
45
|
+
### VALUE 分区自动扩展
|
|
46
|
+
|
|
47
|
+
```dolphindb
|
|
48
|
+
// 库参数:写入新值时自动建分区
|
|
49
|
+
create database "dfs://xxx"
|
|
50
|
+
partitioned by VALUE(2024.01.01..2024.01.07)
|
|
51
|
+
engine='TSDB',
|
|
52
|
+
newValuePartitionPolicy=add // 关键:新日期自动建分区,不需预创建
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
不设 `newValuePartitionPolicy=add` → 写入未预建的日期会报 `partition does not exist`。
|
|
56
|
+
|
|
57
|
+
### RANGE 分区静默丢弃陷阱
|
|
58
|
+
|
|
59
|
+
```dolphindb
|
|
60
|
+
// 库参数 allowMissingPartitions 默认 true
|
|
61
|
+
// → 范围外数据【静默丢弃,不报错】!
|
|
62
|
+
create database "dfs://xxx"
|
|
63
|
+
partitioned by RANGE(2024.01M..2024.12M)
|
|
64
|
+
// 写 2025 年数据 → 默默丢
|
|
65
|
+
|
|
66
|
+
// 需要严格性:
|
|
67
|
+
create database "dfs://xxx"
|
|
68
|
+
partitioned by RANGE(2024.01M..2024.12M),
|
|
69
|
+
allowMissingPartitions=false // 越界写入直接报错
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
RANGE 分区**不会**自动扩展,必须手动 `addRangePartitions(database, futureRanges)`。
|
|
73
|
+
|
|
74
|
+
### 估算 HASH 桶数
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
桶数 ≈ (每日数据量 / 期望单分区大小)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A 股 5000+ 股票 + level2 一天 ~80GB → 按日期分区后 80GB / 1GB ≈ 80 桶 → 取 `HASH([SYMBOL, 50])`(保守留 buffer)。
|
|
81
|
+
|
|
82
|
+
## 建库 DDL
|
|
83
|
+
|
|
84
|
+
### 行情快照(TSDB + COMPO 分区)
|
|
85
|
+
|
|
86
|
+
```dolphindb
|
|
87
|
+
// 删库重来(开发期)
|
|
88
|
+
if(existsDatabase("dfs://stock_lv2_snapshot")) {
|
|
89
|
+
dropDatabase("dfs://stock_lv2_snapshot")
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
create database "dfs://stock_lv2_snapshot"
|
|
93
|
+
partitioned by VALUE(2024.01.03..2024.01.04), HASH([SYMBOL, 50]),
|
|
94
|
+
engine='TSDB'
|
|
95
|
+
// VALUE 写头 2 天即可,写入新日期会自动扩展;写多了只是元数据占位
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**注意**:
|
|
99
|
+
- `VALUE` 用一小段日期初始化即可,会自动扩展,**不要塞几年的空分区**(元数据膨胀)。
|
|
100
|
+
- 删库用 `dropDatabase`,要在 controller 上跑。
|
|
101
|
+
- `engine='TSDB'` 不写默认是 OLAP。
|
|
102
|
+
|
|
103
|
+
### 分钟 K 线(OLAP + 单级 VALUE)
|
|
104
|
+
|
|
105
|
+
```dolphindb
|
|
106
|
+
if(existsDatabase("dfs://k_minute_level")) {
|
|
107
|
+
dropDatabase("dfs://k_minute_level")
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
create database "dfs://k_minute_level"
|
|
111
|
+
partitioned by VALUE(2024.01.01..2024.01.31)
|
|
112
|
+
engine='OLAP'
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 维度表(不分区,跟在某个 DFS 库下)
|
|
116
|
+
|
|
117
|
+
```dolphindb
|
|
118
|
+
db = database("dfs://stock_lv2_snapshot")
|
|
119
|
+
createDimensionTable(db, "security_info",
|
|
120
|
+
table(1:0, ["SecurityID","Name","Industry"], [SYMBOL,STRING,SYMBOL])
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## 建表 DDL
|
|
125
|
+
|
|
126
|
+
### TSDB 表(必须 sortColumns)
|
|
127
|
+
|
|
128
|
+
```dolphindb
|
|
129
|
+
create table "dfs://stock_lv2_snapshot"."snapshot"(
|
|
130
|
+
TradeDate DATE[comment="交易日期", compress="delta"]
|
|
131
|
+
TradeTime TIME[comment="交易时间", compress="delta"]
|
|
132
|
+
SecurityID SYMBOL
|
|
133
|
+
LastPrice DOUBLE
|
|
134
|
+
OpenPrice DOUBLE
|
|
135
|
+
HighPrice DOUBLE
|
|
136
|
+
LowPrice DOUBLE
|
|
137
|
+
TotalVolumeTrade LONG
|
|
138
|
+
OfferPrice DOUBLE[] // 数组向量:10 档卖价
|
|
139
|
+
BidPrice DOUBLE[]
|
|
140
|
+
OfferOrderQty LONG[]
|
|
141
|
+
BidOrderQty LONG[]
|
|
142
|
+
)
|
|
143
|
+
partitioned by TradeDate, SecurityID,
|
|
144
|
+
sortColumns=["SecurityID","TradeTime"],
|
|
145
|
+
keepDuplicates=ALL
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
**关键参数**:
|
|
149
|
+
- `partitioned by`:必须和库的 partition 顺序一致。
|
|
150
|
+
- `sortColumns`:**最后一列必须是时间列**,前面是高频查询的过滤列(如 `SecurityID`)。
|
|
151
|
+
- `keepDuplicates`:`ALL`(保留)/`FIRST`/`LAST`,金融 tick 一般 `ALL`。
|
|
152
|
+
- `compress="delta"`:单调递增列(日期、时间、序号)必加,能压缩 80%+。
|
|
153
|
+
- `DOUBLE[]`:数组向量类型,10 档行情用一列搞定(vs 拆 20 列)。
|
|
154
|
+
|
|
155
|
+
### OLAP 表(轻量,无 sortColumns)
|
|
156
|
+
|
|
157
|
+
```dolphindb
|
|
158
|
+
create table "dfs://k_minute_level"."k_minute"(
|
|
159
|
+
securityid SYMBOL
|
|
160
|
+
tradetime TIMESTAMP
|
|
161
|
+
open DOUBLE
|
|
162
|
+
close DOUBLE
|
|
163
|
+
high DOUBLE
|
|
164
|
+
low DOUBLE
|
|
165
|
+
vol INT
|
|
166
|
+
val DOUBLE
|
|
167
|
+
vwap DOUBLE
|
|
168
|
+
)
|
|
169
|
+
partitioned by tradetime
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## sortColumns 选择规则(TSDB)
|
|
173
|
+
|
|
174
|
+
✅ 推荐:`[业务键, 时间列]` —— `[SecurityID, TradeTime]`、`[InstrumentID, Timestamp]`
|
|
175
|
+
|
|
176
|
+
❌ 禁止:
|
|
177
|
+
- 把**唯一键/主键**当 sortColumn(如订单号、UUID)→ 索引膨胀,TSDB 索引文件比数据还大
|
|
178
|
+
- **时间列放第一位** → 同一时刻的不同标的散开,按 SecurityID 查变慢
|
|
179
|
+
|
|
180
|
+
## 数据类型选择
|
|
181
|
+
|
|
182
|
+
| 场景 | 类型 | 不要用 |
|
|
183
|
+
|---|---|---|
|
|
184
|
+
| 价格、金额(金融) | `DECIMAL32(3)` / `DECIMAL64(6)` | `FLOAT/DOUBLE`(精度丢失) |
|
|
185
|
+
| 成交量、笔数 | `LONG`(大数)/ `INT`(小数) | — |
|
|
186
|
+
| 股票代码、合约代码 | `SYMBOL` | `STRING`(symbol 列式压缩 + 字典编码更省) |
|
|
187
|
+
| 名称、备注 | `STRING` | — |
|
|
188
|
+
| 日期 | `DATE` | — |
|
|
189
|
+
| 时间戳 | `TIMESTAMP`(毫秒)/`NANOTIMESTAMP`(纳秒) | `DATETIME`(32 位 int,2038 溢出!)|
|
|
190
|
+
| 10 档行情 | `DOUBLE[]` 数组向量 | 拆 20 列(写慢、查慢) |
|
|
191
|
+
|
|
192
|
+
## ALTER 限制(建表前想清楚)
|
|
193
|
+
|
|
194
|
+
| 操作 | TSDB | OLAP |
|
|
195
|
+
|---|---|---|
|
|
196
|
+
| 新增列(只能末尾) | ✅ | ✅ |
|
|
197
|
+
| 删列 | ❌ | ✅(非分区列) |
|
|
198
|
+
| 改列名 | ❌ | ✅(非分区列) |
|
|
199
|
+
| 改列类型 | ❌ | ✅(非分区列) |
|
|
200
|
+
| 改列顺序 | ❌ | ❌ |
|
|
201
|
+
| 改分区列 | ❌ | ❌ |
|
|
202
|
+
| 主键/唯一索引 | ❌ | ❌ |
|
|
203
|
+
| 自增列 | ❌ | ❌ |
|
|
204
|
+
|
|
205
|
+
**结论**:TSDB 表 schema 一旦定下基本不能动。建表前用一周数据 dry-run。
|
|
206
|
+
|
|
207
|
+
## 元数据查询
|
|
208
|
+
|
|
209
|
+
```dolphindb
|
|
210
|
+
getClusterDFSDatabases() // 所有 DFS 库
|
|
211
|
+
getClusterDFSTables("dfs://stock_lv2_snapshot")// 库下所有表
|
|
212
|
+
getTables(database("dfs://xxx")) // 同上
|
|
213
|
+
schema(loadTable("dfs://xxx","t")) // 完整 schema
|
|
214
|
+
getDatabaseDDL("dfs://xxx") // 反查建库 SQL
|
|
215
|
+
getDBTableDDL("dfs://xxx","t") // 反查建表 SQL
|
|
216
|
+
getTableDiskUsage("dfs://xxx","t") // 表占用磁盘
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
跨引用:导入数据 → [[data-import]],查询 → [[crud-operations]],反模式 → [[pitfalls-and-best-practices]]。
|