在 PostgreSQL 中存储价值历史

编程入门 行业动态 更新时间:2024-10-26 15:19:24
本文介绍了在 PostgreSQL 中存储价值历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

假设我们需要每天存储温度传感器的读数并保存历史记录.每个度量都是一个元组 day: number, value: string(可能会错过某些日子,这就是我们需要明确存储日期的原因).有数十万个传感器.

Let's say we need to store reading of temperature sensors and keep a history, for every day. Each measure is a tuple day: number, value: string (some days could be missed that's why we need to store the day explicitly). There are hundreds of thousands of sensors.

添加新的度量应该不需要重新读取和重写整个对象,应该是小增量添加.

Adding new measurement should not require re-reading and re-write the whole object, it should be small incremental addition.

而且,同一天可能会有多次阅读.在这种情况下,如果日期相同只应保留当天的最新测量值.

And, there could be multiple reading for the same day. In such case, if day is the same only the latest measurement for that day should be kept.

应该使用什么数据结构?我可以看到以下方式:

What data structure should be used? I can see the following ways:

CREATE TABLE sensor_history (
  sensor_id integer,
  time      integer[],
  value     text[]
);

CREATE TABLE sensor_history (
  sensor_id integer,
  history   json/jsonb/hstore
);

推荐答案

为什么不只为每个元组存储一行?例如

Why not just store one row per tuple? e.g.

CREATE TABLE sensor_history (
  sensor_id integer,
  time      integer,
  value     text
);

这篇关于在 PostgreSQL 中存储价值历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 10:28:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1393311.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:价值   历史   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!