@@ -60,44 +60,51 @@ at the right time. When you segment your data to access specific columns, your q
6060For example, to access information about a single device with a specific ` device_id ` , you segment on the ` device_id ` column.
6161This enables you to run analytical queries on compressed data in the $COLUMNSTORE much faster.
6262
63- For example for the following $HYPERTABLE:
63+ To illustrate, let's create a $HYPERTABLE and then run the same query on it with and without optimizations :
6464
65- ``` sql
66- CREATE TABLE metrics (
67- time TIMESTAMPTZ ,
68- user_id INT ,
69- device_id INT ,
70- data JSONB
71- ) WITH (
72- tsdb .hypertable
73- );
74- ```
65+ <Procedure >
7566
76- < CreateHypertablePolicyNote />
67+ 1 . ** Create a $HYPERTABLE **
7768
78- <Procedure >
69+ Create a ` metrics ` $HYPERTABLE with the following command:
70+
71+ ``` sql
72+ CREATE TABLE metrics (
73+ time TIMESTAMPTZ ,
74+ user_id INT ,
75+ device_id INT ,
76+ data JSONB
77+ ) WITH (
78+ tsdb .hypertable
79+ );
80+ ```
81+
82+ < CreateHypertablePolicyNote / >
83+
84+ 1 . ** Execute a query on the $HYPERTABLE without optimizations**
7985
80- 1 . ** Execute a query on a regular $HYPERTABLE**
8186 1 . Query your data
8287 ` ` ` sql
83- SELECT device_id, AVG (cpu) AS avg_cpu, AVG (disk_io) AS avg_disk_io
88+ SELECT device_id, AVG(cpu) AS avg_cpu, AVG(disk_io) AS avg_disk_io
8489 FROM metrics
85- WHERE device_id = 5
90+ WHERE time >= '2024-03-01 00:00:00+01'
91+ AND time < '2024-03-02 00:00:00+01'
92+ AND device_id = 5
8693 GROUP BY device_id;
8794 ` ` `
8895 Gives the following result:
8996 ` ` ` sql
90- device_id | avg_cpu | avg_disk_io
97+ device_id | avg_cpu | avg_disk_io
9198 -----------+--------------------+---------------------
92- 5 | 0.4972598866221261 | 0.49820356730280524
99+ 5 | 0.4954351575883885 | 0.49725603413909114
93100 (1 row)
94- Time: 177,399 ms
101+ Time: 29.216 ms
95102 ` ` `
96103
971041 . ** Execute a query on the same data segmented and ordered in the $COLUMNSTORE**
98105
99- 1 . Control the way your data is ordered in the $COLUMNSTORE:
100-
106+ 1 . Control the way your data is ordered and segmented in the $COLUMNSTORE:
107+
101108 ` ` ` sql
102109 ALTER TABLE metrics SET (
103110 timescaledb.enable_columnstore = true,
@@ -108,24 +115,30 @@ CREATE TABLE metrics (
108115
109116 1 . Query your data
110117 ` ` ` sql
111- select avg(cpu) from metrics where time >= '2024-03-01 00:00:00+01' and time < '2024-03-02 00:00:00+01';
112- ` ` `
118+ SELECT device_id, AVG(cpu) AS avg_cpu, AVG(disk_io) AS avg_disk_io
119+ FROM metrics
120+ WHERE time >= '2024-03-01 00:00:00+01'
121+ AND time < '2024-03-02 00:00:00+01'
122+ AND device_id = 5
123+ GROUP BY device_id;
124+ ` ` `
113125 Gives the following result:
126+
114127 ` ` ` sql
115- device_id | avg_cpu | avg_disk_io
116- -----------+-------------------+---------------------
117- 5 | 0.497259886622126 | 0.49820356730280535
128+ device_id | avg_cpu | avg_disk_io
129+ -----------+-------------------- +---------------------
130+ 5 | 0.4954351575883885 | 0.49725603413909114
118131 (1 row)
119- Time: 42,139 ms
132+ Time: 1.828 ms
120133 ` ` `
121134
122- As you see, using ` orderby` and ` segmentby` not only reduces the amount of space taken by your data, but also
135+ As you see, using ` orderby` and ` segmentby` not only reduces the amount of space taken by your data, but also
123136 vastly improves query speed.
124137
125138< / Procedure>
126139
127140The number of rows that are compressed together in a single batch (like the ones we see above) is 1000 .
128- If your chunk does not contain enough data to create big enough batches, your compression ratio will be reduced.
141+ If your $CHUNK does not contain enough data to create big enough batches, your compression ratio will be reduced.
129142This needs to be taken into account when you define your $COLUMNSTORE settings.
130143
131144[hypercore]: / use- timescale/ :currentVersion:/ hypercore/
0 commit comments