SQL table with thousands of columns

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.

5

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):

32 tables
625 columns each
numeric[] SQL array jsonb array jsonb object json array json object hstore Entity-Attribute-Value
16MB 11MB 11MB 15MB 14MB 18MB 38MB 55MB

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 to 2.5MB and also shows how much of the EAV is just overhead and duplication of the time and sensor signatures, as it only shrinks to 46MB.

Space consumption is only one of the factors, but you can use these as a starting point for further tests.

numeric[]:

create table your_signals(measured_at timestamptz, reading_values numeric[]);
select setseed(.42);--makes this test repeatable
insert into your_signals values
  (now(),(select array_agg(case when .9>random() then random() end) 
          from generate_series(1,2e4)n2) );
select measured_at
  ,reading_values[1] as s1
  ,reading_values[5] as s5
  ,reading_values[9999] as s9999
  ,reading_values[20000] as s20000
from your_signals
measured_at s1 s5 s9999 s20000
2024-09-07 12:46:21.978572+01 0.362470663311556 0.5754996219675 0.800965200844344 0.906566857051784

jsonb array:

create table your_signals(measured_at timestamptz, reading_values jsonb);
select setseed(.42);--makes this test repeatable
insert into your_signals values
  (now(),(select jsonb_agg(case when .9>random() then random()::numeric(30,15) end)
          from generate_series(1,2e4)n2)
  );
select measured_at
  ,reading_values[0] as s1
  ,reading_values[4] as s5
  ,reading_values[9998] as s9999
  ,reading_values[19999] as s20000
from your_signals
measured_at s1 s5 s9999 s20000
2024-09-07 12:55:51.234168+01 0.362470663311556 0.575499621967500 0.800965200844344 0.906566857051784

jsonb object:

create table your_signals(measured_at timestamptz, reading_values jsonb);
select setseed(.42);--makes this test repeatable
insert into your_signals values
  (now(),(select jsonb_object_agg('s'||n2,random()::numeric(30,15))filter(where .9>random()) 
          from generate_series(1,2e4)n2) 
  );
select measured_at
  ,reading_values['s1'] as s1
  ,reading_values['s5'] as s5
  ,reading_values['s9999'] as s9999
  ,reading_values['s20000'] as s20000
from your_signals
measured_at s1 s5 s9999 s20000
2024-09-07 12:59:48.442463+01 0.362470663311556 0.575499621967500 0.800965200844344 0.906566857051784

entity-attribute-value:

create table your_signals(
       measured_at timestamptz
     , source_sensor smallint
     , reading_value numeric);

select setseed(.42);--makes this test repeatable
insert into your_signals 
select now(), n, random() 
from generate_series(1,2e4)n
where .9>random();

--requires a pivot to view sensors in columns
select measured_at
      ,min(reading_value)filter(where source_sensor=1)     as s1
      ,min(reading_value)filter(where source_sensor=5)     as s5
      ,min(reading_value)filter(where source_sensor=9999)  as s9999
      ,min(reading_value)filter(where source_sensor=20000) as s20000
from your_signals
where source_sensor in (1,5,9999,20000)
group by measured_at;
measured_at s1 s5 s9999 s20000
2024-09-07 12:58:24.030178+01 0.362470663311556 0.5754996219675 0.800965200844344 0.906566857051784

Store all 20000 sensor readings in a json column:

create table sensor (
  time timestamp,
  readings json
);

insert into sensor values (now(), '{"sensor1":1.2, "sensor2":5.3, ..., "sensor20000":2.0}');

Or if you always have all the readings use an array type:

create table sensor (
  time timestamp,
  readings decimal[]
);

insert into sensor values (now(), '{1.2, 5.3, ..., 2.0}');
-- or
insert into sensor values (now(), array[1.2, 5.3, ..., 2.0]);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật