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

时间:2017-06-19 20:28:48

标签: sql time-series google-bigquery database-schema

如果我的股票频率不同,那么为各种股票存储OHLC数据的最佳方法是什么?例如,我可能有:

img src="/photos/bird.jpg"

img src="photos/bird.jpg"

img src="bird.jpg"

img src="gallery/photos/bird.jpg"

我正在考虑将所有内容存储在同一个表中,只是添加一个指定分辨率的列,因此它可能如下所示:

* OHLC for 5-minute bars for APPL
* OHLC for 1-minute bars for APPL
* OHLC for 5-minute bars for IBM

这看起来不错吗?

1 个答案:

答案 0 :(得分:3)

看起来确实很好。正如您的另一种可能性,您还可以将每个新分辨率作为单独的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

结果是:

enter image description here

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

您还可以在日期级别聚合ARRAYS,如下所示:

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

结果是:

enter image description here

这种类型的架构可能会为您提供一些优势,具体取决于您处理的数据量,例如更便宜,更有效的存储以及更快的查询(您有时可能会发现返回Resources Exceeded的查询之间存在差异错误,它的工作是STRUCTS and ARRAYS)的明智用法。

相关问题