Tweak database setup

This commit is contained in:
2024-02-01 16:39:45 -05:00
parent f2c585c9d7
commit 6460430a69
2 changed files with 48 additions and 45 deletions

View File

@@ -1,6 +1,4 @@
INSERT INTO INSERT INTO reading(
reading
(
time, time,
name, name,
model, model,
@@ -10,10 +8,8 @@ INSERT INTO
luminance, luminance,
gas_resistance, gas_resistance,
color_temperature, color_temperature,
air_quality_index air_quality_index)
) VALUES (
VALUES
(
@Timestamp, @Timestamp,
@Name, @Name,
@Model, @Model,
@@ -23,8 +19,6 @@ VALUES
@Luminance, @Luminance,
@GasResistance, @GasResistance,
@ColorTemperature, @ColorTemperature,
@AirQualityIndex @AirQualityIndex)
)
ON CONFLICT ON CONFLICT
ON CONSTRAINT reading_pk
DO NOTHING DO NOTHING

View File

@@ -1,17 +1,26 @@
CREATE TABLE CREATE EXTENSION IF NOT EXISTS timescaledb;
reading (
CREATE TABLE reading(
time timestamptz NOT NULL, time timestamptz NOT NULL,
name text NOT NULL, name text NOT NULL,
model text NOT NULL, model text NOT NULL,
temperature DECIMAL NOT NULL, temperature DECIMAL NOT NULL,
pressure DECIMAL NOT NULL, pressure DECIMAL NOT NULL,
humidity DECIMAL NOT NULL, humidity DECIMAL NOT NULL,
luminance INT NOT NULL, luminance int NOT NULL,
gas_resistance INT NOT NULL, gas_resistance int NOT NULL,
color_temperature INT NOT NULL, color_temperature int NOT NULL,
air_quality_index DECIMAL NOT NULL, air_quality_index DECIMAL NOT NULL,
CONSTRAINT reading_pk PRIMARY KEY (time, name, model) CONSTRAINT reading_pk PRIMARY KEY (time, name, model)
); );
SELECT SELECT
create_hypertable('reading', by_range('time')); create_hypertable('reading', by_range('time'));
ALTER TABLE reading SET (
timescaledb.compress,
timescaledb.compress_segmentby = 'name, model',
timescaledb.compress_orderby = 'time DESC');
SELECT
add_compression_policy('reading', INTERVAL '7 days');