2017-06-19 55 views
0

如果我可能有不同的股票頻率,存儲各種股票的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 

確實,似乎精細?

回答

1

它看起來不錯。正如另一種可能適合你,你也可以在每個新的決議,作爲一個單獨的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添加另一行。

您也可以聚集在陣列上的日期水平,像這樣:

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)。

相關問題