如何在不杀死 KDB 的情况下从 Q 中的分钟数据计算每日数据?
How do I calculate daily data from minute data in Q without killing KDB?
我编写了以下查询:
select first(o), max(h), min(l), last(c), sum(v) by sym, t.date from disk
其中 'disk' 是一个分开的八字形磁盘 table 包含 160m 行。
当我 运行 这个查询时,所有 RAM 都被消耗,然后 KDB 死掉。
我做错了什么?
编辑
我的架构是:
c | t f a
---| -----
t | p
o | f
h | f
l | f
c | f
v | i
sym| s p
date
是一个虚拟列,因此每次将 table 读入内存时都需要创建它,因此首先对 date
列进行分组可以加快询问。我还认为您可以从 t.date
中删除 t
,即
q) select first(o), max(h), min(l), last(c), sum(v) by date, sym from disk
.Q
命名空间中有一个有用的函数,.Q.dpft
这里 https://code.kx.com/q/ref/dotq/#qdpft-save-table 可以将 tables 保存到分区数据库中,您可以将其包装在lambda 并传递每个日期,例如
q) / table might be called alltrades
q)table:([]date:"d"0?7000+til 10;sym:100?`AAA`BBB`CCC;price:100?100.0)
q)table
date sym price
-----------------------
2019.03.10 BBB 49.81119
2019.03.08 CCC 8.997612
2019.03.08 CCC 22.74166
2019.03.06 BBB 86.544
..
q) / below, t might be called trade
q){t::select from table where date=x; .Q.dpft[`:hdb; x; `sym;`t]}'[exec distinct date from table]
`t`t`t`t`t`t`t`t`t`t
q) \
user@host:~$ cd hdb
user@host:~/hdb$ ls
2019.03.02 2019.03.03 2019.03.04 2019.03.05 2019.03.06 2019.03.07 2019.03.08 2019.03.09 2019.03.10 2019.03.11 sym
user@host:~/hdb$ cd 2019.03.02
user@host:~/hdb/2019.03.02$ ls
t
user@host:~/hdb/2019.03.02$ cd t
user@host:~/hdb/2019.03.02/t$ ls
date price sym
其中hdb
是你要保存到的目录路径,sym
是会自动枚举的符号列,table
是内存中的table您要分区的。
我编写了以下查询:
select first(o), max(h), min(l), last(c), sum(v) by sym, t.date from disk
其中 'disk' 是一个分开的八字形磁盘 table 包含 160m 行。
当我 运行 这个查询时,所有 RAM 都被消耗,然后 KDB 死掉。
我做错了什么?
编辑 我的架构是:
c | t f a
---| -----
t | p
o | f
h | f
l | f
c | f
v | i
sym| s p
date
是一个虚拟列,因此每次将 table 读入内存时都需要创建它,因此首先对 date
列进行分组可以加快询问。我还认为您可以从 t.date
中删除 t
,即
q) select first(o), max(h), min(l), last(c), sum(v) by date, sym from disk
.Q
命名空间中有一个有用的函数,.Q.dpft
这里 https://code.kx.com/q/ref/dotq/#qdpft-save-table 可以将 tables 保存到分区数据库中,您可以将其包装在lambda 并传递每个日期,例如
q) / table might be called alltrades
q)table:([]date:"d"0?7000+til 10;sym:100?`AAA`BBB`CCC;price:100?100.0)
q)table
date sym price
-----------------------
2019.03.10 BBB 49.81119
2019.03.08 CCC 8.997612
2019.03.08 CCC 22.74166
2019.03.06 BBB 86.544
..
q) / below, t might be called trade
q){t::select from table where date=x; .Q.dpft[`:hdb; x; `sym;`t]}'[exec distinct date from table]
`t`t`t`t`t`t`t`t`t`t
q) \
user@host:~$ cd hdb
user@host:~/hdb$ ls
2019.03.02 2019.03.03 2019.03.04 2019.03.05 2019.03.06 2019.03.07 2019.03.08 2019.03.09 2019.03.10 2019.03.11 sym
user@host:~/hdb$ cd 2019.03.02
user@host:~/hdb/2019.03.02$ ls
t
user@host:~/hdb/2019.03.02$ cd t
user@host:~/hdb/2019.03.02/t$ ls
date price sym
其中hdb
是你要保存到的目录路径,sym
是会自动枚举的符号列,table
是内存中的table您要分区的。