DolphinDB SQL 案例教程

Posted DolphinDB

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DolphinDB SQL 案例教程相关的知识,希望对你有一定的参考价值。

本教程重点介绍了一些常见场景下的SQL编写案例,通过优化前后性能对比或正确编写方法介绍,说明DolphinDB SQL脚本的使用技巧,案例共分四类:​条件过滤相关案例​、​分布式表相关案例​、​分组计算相关案例​及​元编程相关案例​,具体案例可通过目录快速浏览。


1 测试环境说明

处理器:Intel(R) Xeon(R) Silver 4216 CPU @ 2.10GHz

核数:64

内存:512 GB

操作系统:CentOS Linux release 7.9

License:免费版License,CPU 2核,内存 8GB

DolphinDB Server 版本:DolphinDB_Linux64_V2.00.4,单节点模式部署

DolphinDB GUI 版本:DolphinDB_GUI_V1.30.15

以下章节案例中所用到的2020年06月测试数据为上交所 Level-1 快照数据,基于真实数据结构模拟2000只股票快照数据,基于 OLAP 与 TSDB 存储引擎的建库建表、数据模拟、数据插入脚本如下:

model = table(1:0, `SecurityID`DateTime`PreClosePx`OpenPx`HighPx`LowPx`LastPx`Volume`Amount`BidPrice1`BidPrice2`BidPrice3`BidPrice4`BidPrice5`BidOrderQty1`BidOrderQty2`BidOrderQty3`BidOrderQty4`BidOrderQty5`OfferPrice1`OfferPrice2`OfferPrice3`OfferPrice4`OfferPrice5`OfferQty1`OfferQty2`OfferQty3`OfferQty4`OfferQty5, [SYMBOL, DATETIME, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, LONG, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, LONG, LONG, LONG, LONG, LONG, DOUBLE, DOUBLE, DOUBLE, DOUBLE, DOUBLE, LONG, LONG, LONG, LONG, LONG])

// OLAP 存储引擎建库建表
dbDate = database("", VALUE, 2020.06.01..2020.06.07)
dbSecurityID = database("", HASH, [SYMBOL, 10])
db = database("dfs://Level1", COMPO, [dbDate, dbSecurityID])
createPartitionedTable(db, model, `Snapshot, `DateTime`SecurityID)

// TSDB 存储引擎建库建表
dbDate = database("", VALUE, 2020.06.01..2020.06.07)
dbSymbol = database("", HASH, [SYMBOL, 10])
db = database("dfs://Level1_TSDB", COMPO, [dbDate, dbSymbol], engine="TSDB")
createPartitionedTable(db, model, `Snapshot, `DateTime`SecurityID, sortColumns=`SecurityID`DateTime)

def mockHalfDayData(Date, StartTime)
t_SecurityID = table(format(600001..602000, "000000") + ".SH" as SecurityID)
t_DateTime = table(concatDateTime(Date, StartTime + 1..2400 * 3) as DateTime)
t = cj(t_SecurityID, t_DateTime)
size = t.size()
return table(t.SecurityID as SecurityID, t.DateTime as DateTime, rand(100.0, size) as PreClosePx, rand(100.0, size) as OpenPx, rand(100.0, size) as HighPx, rand(100.0, size) as LowPx, rand(100.0, size) as LastPx, rand(10000, size) as Volume, rand(100000.0, size) as Amount, rand(100.0, size) as BidPrice1, rand(100.0, size) as BidPrice2, rand(100.0, size) as BidPrice3, rand(100.0, size) as BidPrice4, rand(100.0, size) as BidPrice5, rand(100000, size) as BidOrderQty1, rand(100000, size) as BidOrderQty2, rand(100000, size) as BidOrderQty3, rand(100000, size) as BidOrderQty4, rand(100000, size) as BidOrderQty5, rand(100.0, size) as OfferPrice1, rand(100.0, size) as OfferPrice2, rand(100.0, size) as OfferPrice3, rand(100.0, size) as OfferPrice4, rand(100.0, size) as OfferPrice5, rand(100000, size) as OfferQty1, rand(100000, size) as OfferQty2, rand(100000, size) as OfferQty3, rand(100000, size) as OfferQty4, rand(100000, size) as OfferQty5)


def mockDa
ta(DateVector, StartTimeVector)
for(Date in DateVector)
for(StartTime in StartTimeVector)
data = mockHalfDayData(Date, StartTime)

// OLAP 存储引擎分布式表插入模拟数据
loadTable("dfs://Level1", "Snapshot").append!(data)

// TSDB 存储引擎分布式表插入模拟数据
loadTable("dfs://Level1_TSDB", "Snapshot").append!(data)




mockData(2020.06.01..2020.06.02, 09:30:00 13:00:00)

2 条件过滤相关案例

where 条件子句包含

以上是关于DolphinDB SQL 案例教程的主要内容,如果未能解决你的问题,请参考以下文章

DolphinDB 函数化编程案例教程

时序数据库DolphinDB历史数据回放教程

对接恒生极速行情丨DolphinDB NSQ 插件使用教程

如何实时计算中证1000指数的主买/主卖交易量

干货丨DolphinDB元编程教程

用 DolphinDB 和 Python Celery 搭建一个高性能因子计算平台