gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:56:54 26 4
gpt4 key购买 nike

如果我对股票的频率可能不同,存储各种股票的 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

结果是:

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 的明智用法)。

关于sql - 如何设计一个数据库来存储不同分辨率的OHLC时间序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44639415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com