带有时间戳的SQL存储,更改数据类型

时间:2015-07-15 19:50:21

标签: mysql sql database-design normalization

我需要在SQL数据库中存储各种带时间戳的数据参数。请参阅下面的典型非标准化数据,其中我显示了五个模拟值(室温,室内湿度,游泳池pH值,AHU供应温度和室内压力)和一个二进制值(灯光状态)。

不同的"客户"我将使用"地址"拥有我在下面指出的数据。领域。通常会添加或删除要存储的给定参数。此外,通常会添加一个以前没有存档的新变量(即冷冻水供应温度),并且不可能事先知道将来可能需要存储哪些参数。

典型的查询是在给定的时间跨度内返回123 Main Street Room 102的温度。另一个典型的问题是返回123 Main Street的所有房间的温度,湿度和光照水平。

为表中的每个所需参数添加列显然没有意义。但我也很难在同一列中存储不同类型的数据。我也很难在许多参数中复制房间号码并认为它​​应该被标准化,但是,其他参数没有相关的房间号,所以我不知道这将如何工作。我还质疑是否应该创建一个表,其中PK是构建和时间戳的组合,并加入另一个表,其中包含参数和值的列(可能是类型)。

如何对这些数据进行建模?

+------------------+-----------------+------------------------+-------+
|     Address      |    Timestamp    |       Parameter        | Value |
+------------------+-----------------+------------------------+-------+
| 123 Main Street  | 7/13/2015 16:00 | Room 101 Temperature   | 70.99 |
| 123 Main Street  | 7/13/2015 16:00 | Room 101 Humidity      | 50%   |
| 123 Main Street  | 7/13/2015 16:00 | Room 101 Light Status  | Off   |
| 123 Main Street  | 7/13/2015 16:00 | Room 102 Temperature   | 70.90 |
| 123 Main Street  | 7/13/2015 16:00 | Room 102 Humidity      | 50%   |
| 123 Main Street  | 7/13/2015 16:00 | Room 102 Light Status  | Off   |
| 123 Main Street  | 7/13/2015 16:00 | Room 103 Temperature   | 69.95 |
| 123 Main Street  | 7/13/2015 16:00 | Room 103 Humidity      | 49%   |
| 123 Main Street  | 7/13/2015 16:00 | Room 103 Light Status  | Off   |
| 123 Main Street  | 7/13/2015 16:15 | Room 101 Temperature   | 69.65 |
| 123 Main Street  | 7/13/2015 16:15 | Room 101 Humidity      | 47%   |
| 123 Main Street  | 7/13/2015 16:15 | Room 101 Light Status  | On    |
| 123 Main Street  | 7/13/2015 16:15 | Room 102 Temperature   | 69.18 |
| 123 Main Street  | 7/13/2015 16:15 | Room 102 Humidity      | 46%   |
| 123 Main Street  | 7/13/2015 16:15 | Room 102 Light Status  | On    |
| 123 Main Street  | 7/13/2015 16:15 | Room 103 Temperature   | 68.49 |
| 123 Main Street  | 7/13/2015 16:15 | Room 103 Humidity      | 48%   |
| 123 Main Street  | 7/13/2015 16:15 | Room 103 Light Status  | On    |
| 123 Main Street  | 7/13/2015 16:30 | Room 101 Temperature   | 68.93 |
| 123 Main Street  | 7/13/2015 16:30 | Room 101 Humidity      | 49%   |
| 123 Main Street  | 7/13/2015 16:30 | Room 101 Light Status  | On    |
| 123 Main Street  | 7/13/2015 16:30 | Room 102 Temperature   | 69.44 |
| 123 Main Street  | 7/13/2015 16:30 | Room 102 Humidity      | 49%   |
| 123 Main Street  | 7/13/2015 16:30 | Room 102 Light Status  | Off   |
| 123 Main Street  | 7/13/2015 16:30 | Room 103 Temperature   | 69.63 |
| 123 Main Street  | 7/13/2015 16:30 | Room 103 Humidity      | 48%   |
| 123 Main Street  | 7/13/2015 16:30 | Room 103 Light Status  | Off   |
| 321 Front Street | 7/14/2015 14:00 | AHU Supply Temperature | 69.96 |
| 321 Front Street | 7/14/2015 14:00 | Swimming Pool PH       | 7.19  |
| 321 Front Street | 7/14/2015 14:00 | Room 101 Pressure      | 0.11  |
| 321 Front Street | 7/14/2015 14:15 | AHU Supply Temperature | 69.92 |
| 321 Front Street | 7/14/2015 14:15 | Swimming Pool PH       | 6.97  |
| 321 Front Street | 7/14/2015 14:15 | Room 101 Pressure      | 0.11  |
| 321 Front Street | 7/14/2015 14:30 | AHU Supply Temperature | 70.37 |
| 321 Front Street | 7/14/2015 14:30 | Swimming Pool PH       | 6.84  |
| 321 Front Street | 7/14/2015 14:30 | Room 101 Pressure      | 0.12  |
| 321 Front Street | 7/14/2015 14:45 | AHU Supply Temperature | 70.80 |
| 321 Front Street | 7/14/2015 14:45 | Swimming Pool PH       | 6.70  |
| 321 Front Street | 7/14/2015 14:45 | Room 101 Pressure      | 0.12  |
| 321 Front Street | 7/14/2015 15:00 | AHU Supply Temperature | 71.29 |
| 321 Front Street | 7/14/2015 15:00 | Swimming Pool PH       | 6.90  |
| 321 Front Street | 7/14/2015 15:00 | Room 101 Pressure      | 0.12  |
| 321 Front Street | 7/14/2015 15:15 | AHU Supply Temperature | 72.13 |
| 321 Front Street | 7/14/2015 15:15 | Swimming Pool PH       | 7.13  |
| 321 Front Street | 7/14/2015 15:15 | Room 101 Pressure      | 0.11  |
| 321 Front Street | 7/14/2015 15:30 | AHU Supply Temperature | 72.84 |
| 321 Front Street | 7/14/2015 15:30 | Swimming Pool PH       | 7.01  |
| 321 Front Street | 7/14/2015 15:30 | Room 101 Pressure      | 0.11  |
| 321 Front Street | 7/14/2015 15:45 | AHU Supply Temperature | 72.82 |
| 321 Front Street | 7/14/2015 15:45 | Swimming Pool PH       | 7.22  |
| 321 Front Street | 7/14/2015 15:45 | Room 101 Pressure      | 0.11  |
| 321 Front Street | 7/14/2015 16:00 | AHU Supply Temperature | 72.23 |
| 321 Front Street | 7/14/2015 16:00 | Swimming Pool PH       | 7.40  |
| 321 Front Street | 7/14/2015 16:00 | Room 101 Pressure      | 0.11  |
+------------------+-----------------+------------------------+-------+

1 个答案:

答案 0 :(得分:2)

我会选择3张桌子:

Address
-------
address_id
address_name

Location
--------
location_id
location_name
address_id

Measurement
-----------
meauserment_id
location_id
type
timestamp
value

这里的所有房间,游泳池和用品都被建模为不同的位置,所以类型只是"温度","湿度"等

对于您的查询,您需要在(location_id, type, timestamp)上有一个复合二级索引。

如果您可以为同一地址拥有大量位置,并且您真正关心第二类查询的读取性能,那么(假设您可以允许自己忽略每个时间戳相同location_id类型的多个数据点)组织测量的最佳方法是:

Measurement
-----------
address_id
location_id
type
timestamp
value

您的PK为(address_id, location_id, type, timestamp) +辅助索引(location_id, type, timestamp)