What is the most efficient way to store measurements with a variable number of fields in a database?

依然范特西╮ 提交于 2019-12-03 16:34:41

Databases can handle large amounts of data in a table if you use efficient indexes. So you can use this table structure:

create table measurements (
     id,
     seq integer, -- between 1 and 256
     ts timestamp, -- Timestamp of the measurement
     value decimal(...)
)

Create an index on id, id, seq and ts. That will allow you to search efficiently through the data. If you distrust your database, just insert a few million rows and run a couple of selects to see how well it fares.

For comparison: I have an Oracle database here with 112 million rows and I can select a record by timestamp or ID within 120ms (0.12s)

You could save serialized data in a text field, for example JSON-encoding the measurements as:

[<velocity-value-1>, <velocity-value-2>, ...]

Then, in your code, deserialize the values after querying.

This should work well if you only filter your queries by the other fields, and not by the saved values. If you do filter by the values, using them in WHERE clauses will be a nightmare.

I'd go with a second table:

table measurements (Id, DateTime, Temperature, Pressure)
table velocity (Id, MeasurementId, Sequence, Value)

Velocity.MeasurementId references Measurements.Id.
Velocity.Sequence is the index of the velocity value for that measurement (1-256).

Populate these tables with data as close to real-world as possible and test the sql statements to find the best indexes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!