如何设计一个数据库来存储不同分辨率的OHLC时间序列?

如果我可能有不同的股票频率,存储各种股票的OHLC数据的最佳方式是什么?例如,我可能有:如何设计一个数据库来存储不同分辨率的OHLC时间序列?

* OHLC for 5-minute bars for APPL 

* OHLC for 1-minute bars for APPL

* OHLC for 5-minute bars for IBM

我想在同一个表中存储的一切,只是增加一个指定的分辨率,所以可能看起来像这样的列:

symbol, date,  time, resolution, open, high, low, close 

AAPL, 2017-06-19, 9:30, 5 min, 99.12, 102.52, 94.22, 98.34

AAPL, 2017-06-19, 9:30, 1 min, 99.12, 100.11, 99.01, 100.34

IBM, 2017-06-19, 9:30, 5 min, 40.15, 45.78, 39.18, 44.22

确实,似乎精细?

回答:

它看起来不错。正如另一种可能适合你,你也可以在每个新的决议,作为一个单独的STRUCT(记录)存储内的ARRAY(重复场),像这样:

WITH data AS(

select 'APPL' as symbol, ARRAY<STRUCT<date string, time string, resolution INT64, open FLOAT64, high FLOAT64, low FLOAT64, close FLOAT64>> [STRUCT('2017-06-19' as date, '9:30' as time, 5 as resolution, 99.12 as open, 102.52 as high, 94.22 as low, 98.32 as close), STRUCT('2017-06-19' as date, '9:30' as time, 1 as resolution, 99.12 as open, 100.11 as high, 99.01 as low, 100.34 as close)] stock union all

select 'IBM' as symbol, ARRAY<STRUCT<date string, time string, resolution INT64, open FLOAT64, high FLOAT64, low FLOAT64, close FLOAT64>> [STRUCT('2017-06-19' as date, '9:30' as time, 5 as resolution, 40.15 as open, 45.78 as high, 39.18 as low, 44.22 as close)]

)

SELECT * FROM data

导致:

请注意,当您存储新的分辨率值时,它会为每个股票定义的ARRAY添加另一行。

您也可以聚集在阵列上的日期水平,像这样:

WITH data AS(

select 'APPL' as symbol, STRUCT<date string, time string, hit ARRAY<STRUCT<resolution INT64, open FLOAT64, high FLOAT64, low FLOAT64, close FLOAT64>>> ('2017-06-19', '9:30', [STRUCT(1 as resolution, 99.12 as open, 102.52 as high, 94.22 as low, 98.32 as close), STRUCT(5 as resolution, 99.12 as open, 100.11 as high, 99.01 as low, 100.34 as close)]) stock union all

select 'IBM' as symbol, STRUCT<date string, time string, hit ARRAY<STRUCT<resolution INT64, open FLOAT64, high FLOAT64, low FLOAT64, close FLOAT64>>> ('2017-06-19', '9:30', [STRUCT(1 as resolution, 40.15 as open, 45.78 as high, 39.18 as low, 44.22 as close)])

)

SELECT * FROM data

导致:

这种类型的模式可能会给你一定的优势取决于有多少您正在处理的数据,例如更便宜,更有效的存储以及更快的查询(您可能会发现返回Resources Exceeded错误的查询与其工作之间的差异是明智的使用STRUCTS and ARRAYS)。

以上是 如何设计一个数据库来存储不同分辨率的OHLC时间序列? 的全部内容, 来源链接: utcz.com/qa/259002.html

回到顶部