I need to store data(float) from 20,000 sensors once a second. I originally wanted to create the following table:
time | sensor 1 | sensor 2 | … | sensor 20000 |
---|---|---|---|---|
2024-09-06 13:00:00 | 1.2 | 5.3 | …. | 2.0 |
But then I found a table cannot have more than 1600 columns in PostgreSQL. What’s the best practice to store this kind of data? Separate them into multiple tables or switch to another type of DB?
All 20000 sensor values are read and inserted together.
I need to query up to 100 of them per second to plot trend charts.
2
Answers
Store all 20000 sensor readings in a
json
column:Or if you always have all the readings use an array type:
Here’s how much space it takes to store 1 minute of the randomly generated per-second readings from 20k sensors with 10% sparsity (they share
setseed()
, so the random data they save is the exact same):numeric[]
SQL arrayjsonb
arrayjsonb
objecthstore
Column names link to documentation, cells link to db<>fiddle demos you can play around with.
In each case you can save space by reducing precision and scale of your readings, e.g. using a
numeric(4,2)
. That results in the array going down in size to2.5MB
and also shows how much of the EAV is just overhead and duplication of the time and sensor signatures, as it only shrinks to46MB
.Space consumption is only one of the factors, but you can use these as a starting point for further tests.
numeric[]
:jsonb
array:jsonb
object:entity-attribute-value: