简体   繁体   English

SQL 时序数据类型的数据库结构

[英]SQL database structure for time series data type

I wonder if someone could take a minute out of their day to give some suggestion on my database structure design.我想知道是否有人可以抽出一分钟来对我的数据库结构设计提出一些建议。

I have sensor data (eg temperature, humidity...) with time series format (10Hz) which are installed in different floors of different houses of different cities.我有时间序列格式(10Hz)的传感器数据(例如温度、湿度......),它们安装在不同城市不同房屋的不同楼层。 So let say something like this:所以让我们这样说:

*City Paris-->House A -->Floor 1 --> Sensor Humidity & temp --> csv file with time series for hours, days, years *City Paris-->House A -->Floor 1 --> Sensor Humidity & temp --> csv 文件,包含小时、天、年的时间序列

City Paris-->House B -->Floor 3 --> Sensor Humidity --> csv file with time series for hours, days, years* City Paris-->House B -->Floor 3 --> Sensor Humidity --> csv 文件,包含小时、天、年的时间序列*

So now I would like to answer these questions: 1- What would be the most efficient method to store the data A sql database?所以现在我想回答这些问题: 1- 存储数据 A sql 数据库的最有效方法是什么? 2- Would it make sense to have timestamp data stored in sql database but the sensor data in CSV file and then link them them to sql database? 2-将时间戳数据存储在 sql 数据库中但将传感器数据存储在 CSV 文件中然后将它们链接到 sql 数据库中是否有意义? 3- What about the scalability and possibility to add new sensors? 3- 添加新传感器的可扩展性和可能性如何?

Many thanks for your help and suggestion in advance,非常感谢您提前提供的帮助和建议,

At least you should not save the csv in the database as a varchar or text at once.至少您不应该一次将数据库中的 csv 保存为 varchar 或文本。 You should break down eveything in as small parts as possible.你应该把一切都分解成尽可能小的部分。 My suggestion is you first create a table like this我的建议是你首先创建一个这样的表

CREATE TABLE measurements (measurement_id INT PRIMARY KEY, floor_id INT, type VARCHAR(50), value FLOAT)

Then you create a table for floors然后为楼层创建一个表

CREATE TABLE floors (floor_id INT PRIMARY KEY, building_id INT, floor_name INT)

And at least the connection to the building至少与建筑物的连接

CREATE TABLE buildings (building_id INT PRIMARY KEY, building_name VARCHAR(200), building_city VARCHAR(200))

You should create foreign keys from the floors.floor_id to measurements.floor_id and the buildings.building_id to floor.building _id .您应该创建从floors.floor_id到 measure.floor_id 和从buildings.building_idmeasurements.floor_id floor.building _id的外键。

You can even break down into more tables to have cities and/or addresses in own once if you like.如果你愿意,你甚至可以分解成更多的表格来拥有自己的城市和/或地址。

If your objective is to run time-series analytics, I would recommend to break down your data so that each reading is in one row and to use a time-series database.如果您的目标是运行时间序列分析,我建议您分解数据,以便每个读数都在一行中并使用时间序列数据库。

The schema proposed earlier is good.前面提出的模式很好。 But I personally find storing the data in 3 tables too complex as you need to write / check constraints across 3 different tables, and most of your queries will require JOIN clauses.但我个人认为将数据存储在 3 个表中过于复杂,因为您需要跨 3 个不同的表编写/检查约束,并且您的大多数查询都需要 JOIN 子句。

There are ways to make this schema simpler, for example by leveraging the symbol type in QuestDB.有一些方法可以使这个模式更简单,例如利用 QuestDB 中的symbol类型。 Symbol stores repetitive strings as a map of integers.符号将重复字符串存储为整数的 map。 On the surface, you are manipulating strings, but the storage cost and operation complexity is that of an int.表面上你是在操作字符串,但存储成本和操作复杂度是 int 的。

This means you can store all your data in a single, more simple table, with no performance or storage penalty.这意味着您可以将所有数据存储在一个更简单的表中,而不会降低性能或存储空间。 And this would simplify both ingestion as you write into only one table, and queries by removing the need to perform multiple joins.这将简化您只写入一个表时的摄取,并通过消除执行多个连接的需要来简化查询。

Here is what the DDL would look like.这是 DDL 的样子。

CREATE TABLE measurements (
id INT,
ts TIMESTAMP,
sensor_name SYMBOL,
floor_name SYMBOL,
building_name SYMBOL,
building_city SYMBOL,
type SYMBOL,
value DOUBLE
) timestamp (ts)

If you want to add more sensors or buildings, all you need to do is write to the same table.如果您想添加更多传感器或建筑物,您需要做的就是写入同一张表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM