diff --git a/Environment/Service/Data/Queries/CreateReading.sql b/Environment/Service/Data/Queries/CreateReading.sql index 8591f36..a2e14b8 100644 --- a/Environment/Service/Data/Queries/CreateReading.sql +++ b/Environment/Service/Data/Queries/CreateReading.sql @@ -1,30 +1,24 @@ -INSERT INTO - reading - ( - time, - name, - model, - temperature, - pressure, - humidity, - luminance, - gas_resistance, - color_temperature, - air_quality_index - ) -VALUES - ( - @Timestamp, - @Name, - @Model, - @Temperature, - @Pressure, - @Humidity, - @Luminance, - @GasResistance, - @ColorTemperature, - @AirQualityIndex - ) -ON CONFLICT - ON CONSTRAINT reading_pk - DO NOTHING \ No newline at end of file +INSERT INTO reading( + time, + name, + model, + temperature, + pressure, + humidity, + luminance, + gas_resistance, + color_temperature, + air_quality_index) +VALUES ( + @Timestamp, + @Name, + @Model, + @Temperature, + @Pressure, + @Humidity, + @Luminance, + @GasResistance, + @ColorTemperature, + @AirQualityIndex) +ON CONFLICT + DO NOTHING diff --git a/Environment/Service/Data/Schema/1-Initial Schema.sql b/Environment/Service/Data/Schema/1-Initial Schema.sql index ea39f30..de5d702 100644 --- a/Environment/Service/Data/Schema/1-Initial Schema.sql +++ b/Environment/Service/Data/Schema/1-Initial Schema.sql @@ -1,17 +1,26 @@ -CREATE TABLE - reading ( - time timestamptz NOT NULL, - name text NOT NULL, - model text NOT NULL, - temperature DECIMAL NOT NULL, - pressure DECIMAL NOT NULL, - humidity DECIMAL NOT NULL, - luminance INT NOT NULL, - gas_resistance INT NOT NULL, - color_temperature INT NOT NULL, - air_quality_index DECIMAL NOT NULL, - CONSTRAINT reading_pk PRIMARY KEY (time, name, model) - ); +CREATE EXTENSION IF NOT EXISTS timescaledb; + +CREATE TABLE reading( + time timestamptz NOT NULL, + name text NOT NULL, + model text NOT NULL, + temperature DECIMAL NOT NULL, + pressure DECIMAL NOT NULL, + humidity DECIMAL NOT NULL, + luminance int NOT NULL, + gas_resistance int NOT NULL, + color_temperature int NOT NULL, + air_quality_index DECIMAL NOT NULL, + CONSTRAINT reading_pk PRIMARY KEY (time, name, model) +); SELECT - create_hypertable('reading', by_range('time')); \ No newline at end of file + 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');