![](/img/trans.png)
[英]How to insert new row with id from other table in loop with Postgressql
[英]PostgresSQL - how to design an effective table
我需要從數十個傳感器收集數據,以 15 秒的間隔保存數據,在 postgres 數據庫中,並希望選擇上個月/一周/一天的值快速運行 - 繪制圖表。 我應該使用哪些索引、分區或其他東西?
CREATE TABLE temperatures(
sensor_id integer NOT NULL,
val real NOT NULL,
audit_date timestamp with time zone NOT NULL DEFAULT now()
)
對於每個傳感器,您每天收集 5,760 行。 這可以開始加起來。
首先,我建議對數據進行分區。 對數據進行分區的正確方法取決於您真正打算如何使用它。 您提到返回最新數據,因此這絕對建議使用時間。 您可能還想將sensor_id
作為分區鍵包含在內,但這不太可能。 您可以在 文檔中開始學習分區。
接下來你需要索引。 據推測,您想通過傳感器獲取值。 這表明(sensor_id, audit_date)
上有一個索引。 但是,可能需要調整索引以與分區方案兼容。
選擇
select
s.sensor_id,
audit_date::timestamp(0) as audit_date,
t.val,
s.comment
from
temperatures t,
sensors s
where
t.audit_date >= timestamp '$date1'
and t.audit_date <= timestamp '$date2'
and t.sensor_id in (select sensor_id from users_sensors us, users u where us.user_id = u.user_id and u.login = '$login')
and t.sensor_id = s.sensor_id
order by 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.