|
|
|
postgres=# create table keys ( |
|
postgres(# id integer primary key, |
|
postgres(# name varchar(255) not null |
|
postgres(# ); |
|
CREATE TABLE |
|
postgres=# |
|
postgres=# create table data ( |
|
postgres(# point timestamptz not null, |
|
postgres(# key_id integer not null, |
|
postgres(# value real not null |
|
postgres(# ); |
|
CREATE TABLE |
|
postgres=# |
|
postgres=# create index on data (key_id); |
|
CREATE INDEX |
|
postgres=# |
|
postgres=# select create_hypertable( |
|
postgres(# 'data', 'point', |
|
postgres(# partitioning_column => 'key_id', |
|
postgres(# number_partitions => 3 |
|
postgres(# ); |
|
create_hypertable |
|
------------------- |
|
(1,public,data,t) |
|
(1 row) |
|
|
|
postgres=# |
|
postgres=# INSERT INTO keys (id, name) |
|
postgres-# SELECT i, 'metric-' || i |
|
postgres-# FROM generate_series(0, 100) AS g (i); |
|
INSERT 0 101 |
|
postgres=# |
|
postgres=# INSERT INTO data (point, key_id, value) |
|
postgres-# SELECT now(), floor(random() * 100), random() |
|
postgres-# FROM generate_series(0, 1000000) AS g (i); |
|
INSERT 0 1000001 |
|
postgres=# |
|
postgres=# explain analyze |
|
postgres-# select * from data |
|
postgres-# join keys on keys.id = data.key_id |
|
postgres-# where keys.name = 'metric-1'; |
|
QUERY PLAN |
|
------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
Nested Loop (cost=16.78..3701.07 rows=5001 width=536) (actual time=0.622..7.077 rows=9865 loops=1) |
|
-> Seq Scan on keys (cost=0.00..11.75 rows=1 width=520) (actual time=0.009..0.031 rows=1 loops=1) |
|
Filter: ((name)::text = 'metric-1'::text) |
|
Rows Removed by Filter: 100 |
|
-> Append (cost=16.78..3639.31 rows=5001 width=16) (actual time=0.610..5.961 rows=9865 loops=1) |
|
-> Bitmap Heap Scan on _hyper_1_1_chunk (cost=16.78..1012.89 rows=1401 width=16) (actual time=0.007..0.007 rows=0 loops=1) |
|
Recheck Cond: (key_id = keys.id) |
|
-> Bitmap Index Scan on _hyper_1_1_chunk_data_key_id_idx (cost=0.00..16.43 rows=1401 width=0) (actual time=0.005..0.005 rows=0 loops=1) |
|
Index Cond: (key_id = keys.id) |
|
-> Bitmap Heap Scan on _hyper_1_2_chunk (cost=19.81..1192.52 rows=1650 width=16) (actual time=0.601..5.337 rows=9865 loops=1) |
|
Recheck Cond: (key_id = keys.id) |
|
Heap Blocks: exact=1781 |
|
-> Bitmap Index Scan on _hyper_1_2_chunk_data_key_id_idx (cost=0.00..19.40 rows=1650 width=0) (actual time=0.430..0.430 rows=9865 loops=1) |
|
Index Cond: (key_id = keys.id) |
|
-> Bitmap Heap Scan on _hyper_1_3_chunk (cost=23.24..1408.89 rows=1950 width=16) (actual time=0.013..0.013 rows=0 loops=1) |
|
Recheck Cond: (key_id = keys.id) |
|
-> Bitmap Index Scan on _hyper_1_3_chunk_data_key_id_idx (cost=0.00..22.75 rows=1950 width=0) (actual time=0.010..0.010 rows=0 loops=1) |
|
Index Cond: (key_id = keys.id) |
|
Planning Time: 0.532 ms |
|
Execution Time: 7.482 ms |
|
(20 rows) |
|
|
|
postgres=# |
|
postgres=# explain analyze |
|
postgres-# select * from data |
|
postgres-# where key_id = 1; |
|
QUERY PLAN |
|
------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
Append (cost=19.81..1200.77 rows=1650 width=16) (actual time=0.776..4.952 rows=9865 loops=1) |
|
-> Bitmap Heap Scan on _hyper_1_2_chunk (cost=19.81..1192.52 rows=1650 width=16) (actual time=0.775..4.411 rows=9865 loops=1) |
|
Recheck Cond: (key_id = 1) |
|
Heap Blocks: exact=1781 |
|
-> Bitmap Index Scan on _hyper_1_2_chunk_data_key_id_idx (cost=0.00..19.40 rows=1650 width=0) (actual time=0.476..0.476 rows=9865 loops=1) |
|
Index Cond: (key_id = 1) |
|
Planning Time: 0.212 ms |
|
Execution Time: 5.253 ms |
|
(8 rows) |
|
|
|
postgres=# \d data |
|
Table "public.data" |
|
Column | Type | Collation | Nullable | Default |
|
--------+--------------------------+-----------+----------+--------- |
|
point | timestamp with time zone | | not null | |
|
key_id | integer | | not null | |
|
value | real | | not null | |
|
Indexes: |
|
"data_key_id_idx" btree (key_id) |
|
"data_key_id_point_idx" btree (key_id, point DESC) |
|
"data_point_idx" btree (point DESC) |
|
Triggers: |
|
ts_insert_blocker BEFORE INSERT ON data FOR EACH ROW EXECUTE PROCEDURE _timescaledb_internal.insert_blocker() |
|
Number of child tables: 3 (Use \d+ to list them.) |
|
|
|
postgres=# \d+ data |
|
Table "public.data" |
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description |
|
--------+--------------------------+-----------+----------+---------+---------+--------------+------------- |
|
point | timestamp with time zone | | not null | | plain | | |
|
key_id | integer | | not null | | plain | | |
|
value | real | | not null | | plain | | |
|
Indexes: |
|
"data_key_id_idx" btree (key_id) |
|
"data_key_id_point_idx" btree (key_id, point DESC) |
|
"data_point_idx" btree (point DESC) |
|
Triggers: |
|
ts_insert_blocker BEFORE INSERT ON data FOR EACH ROW EXECUTE PROCEDURE _timescaledb_internal.insert_blocker() |
|
Child tables: _timescaledb_internal._hyper_1_1_chunk, |
|
_timescaledb_internal._hyper_1_2_chunk, |
|
_timescaledb_internal._hyper_1_3_chunk |
|
|
|
postgres=# \d+ _timescaledb_internal._hyper_1_1_chunk |
|
Table "_timescaledb_internal._hyper_1_1_chunk" |
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description |
|
--------+--------------------------+-----------+----------+---------+---------+--------------+------------- |
|
point | timestamp with time zone | | not null | | plain | | |
|
key_id | integer | | not null | | plain | | |
|
value | real | | not null | | plain | | |
|
Indexes: |
|
"_hyper_1_1_chunk_data_key_id_idx" btree (key_id) |
|
"_hyper_1_1_chunk_data_key_id_point_idx" btree (key_id, point DESC) |
|
"_hyper_1_1_chunk_data_point_idx" btree (point DESC) |
|
Check constraints: |
|
"constraint_1" CHECK (point >= '2020-02-13 00:00:00+00'::timestamp with time zone AND point < '2020-02-20 00:00:00+00'::timestamp with time zone) |
|
"constraint_2" CHECK (_timescaledb_internal.get_partition_hash(key_id) >= 715827882 AND _timescaledb_internal.get_partition_hash(key_id) < 1431655764) |
|
Inherits: data |