-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinit.sql
More file actions
33 lines (22 loc) · 1023 Bytes
/
init.sql
File metadata and controls
33 lines (22 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
CREATE EXTENSION IF NOT EXISTS timescaledb;
-- Create the cargo_data table within the schema
CREATE TABLE IF NOT EXISTS cargo_data (
time TIMESTAMPTZ NOT NULL,
ship_id TEXT NOT NULL,
cargo_id TEXT NOT NULL,
value DOUBLE PRECISION NOT NULL,
PRIMARY KEY (time, ship_id, cargo_id)
);
SELECT create_hypertable('cargo_data', by_range('time', INTERVAL '1 day'), if_not_exists => TRUE);
-- Create an index to optimize queries on the cargo_data table
CREATE INDEX IF NOT EXISTS idx_ship_cargo_time ON cargo_data (ship_id, cargo_id, time DESC);
-- Set a retention policy to automatically manage old data
SELECT add_retention_policy('cargo_data', INTERVAL '365 days');
-- Enable compression for older data to optimize storage
ALTER TABLE cargo_data SET (
timescaledb.compress,
timescaledb.compress_segmentby = 'ship_id',
timescaledb.compress_orderby = 'time DESC'
);
-- Add a compression policy to compress data after 1 month
SELECT add_compression_policy('cargo_data', INTERVAL '7 days');