-
Notifications
You must be signed in to change notification settings - Fork 128
Update to secondary indexes doc #4588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atovpeko
wants to merge
1
commit into
latest
Choose a base branch
from
4187-feedback-page-use-timescalelatesthypercoresecondary-indexes
base: latest
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,44 +60,51 @@ at the right time. When you segment your data to access specific columns, your q | |
| For example, to access information about a single device with a specific `device_id`, you segment on the `device_id` column. | ||
| This enables you to run analytical queries on compressed data in the $COLUMNSTORE much faster. | ||
|
|
||
| For example for the following $HYPERTABLE: | ||
| To illustrate, let's create a $HYPERTABLE and then run the same query on it with and without optimizations: | ||
|
|
||
| ```sql | ||
| CREATE TABLE metrics ( | ||
| time TIMESTAMPTZ, | ||
| user_id INT, | ||
| device_id INT, | ||
| data JSONB | ||
| ) WITH ( | ||
| tsdb.hypertable | ||
| ); | ||
| ``` | ||
| <Procedure> | ||
|
|
||
| <CreateHypertablePolicyNote /> | ||
| 1. **Create a $HYPERTABLE** | ||
|
|
||
| <Procedure> | ||
| Create a `metrics` $HYPERTABLE with the following command: | ||
|
|
||
| ```sql | ||
| CREATE TABLE metrics ( | ||
| time TIMESTAMPTZ, | ||
| user_id INT, | ||
| device_id INT, | ||
| data JSONB | ||
| ) WITH ( | ||
| tsdb.hypertable | ||
| ); | ||
| ``` | ||
|
|
||
| <CreateHypertablePolicyNote /> | ||
|
|
||
| 1. **Execute a query on the $HYPERTABLE without optimizations** | ||
|
|
||
| 1. **Execute a query on a regular $HYPERTABLE** | ||
| 1. Query your data | ||
| ```sql | ||
| SELECT device_id, AVG(cpu) AS avg_cpu, AVG(disk_io) AS avg_disk_io | ||
| SELECT device_id, AVG(cpu) AS avg_cpu, AVG(disk_io) AS avg_disk_io | ||
| FROM metrics | ||
| WHERE device_id = 5 | ||
| WHERE time >= '2024-03-01 00:00:00+01' | ||
| AND time < '2024-03-02 00:00:00+01' | ||
| AND device_id = 5 | ||
| GROUP BY device_id; | ||
| ``` | ||
| Gives the following result: | ||
| ```sql | ||
| device_id | avg_cpu | avg_disk_io | ||
| device_id | avg_cpu | avg_disk_io | ||
| -----------+--------------------+--------------------- | ||
| 5 | 0.4972598866221261 | 0.49820356730280524 | ||
| 5 | 0.4954351575883885 | 0.49725603413909114 | ||
| (1 row) | ||
| Time: 177,399 ms | ||
| Time: 29.216 ms | ||
| ``` | ||
|
|
||
| 1. **Execute a query on the same data segmented and ordered in the $COLUMNSTORE** | ||
|
|
||
| 1. Control the way your data is ordered in the $COLUMNSTORE: | ||
| 1. Control the way your data is ordered and segmented in the $COLUMNSTORE: | ||
|
|
||
| ```sql | ||
| ALTER TABLE metrics SET ( | ||
| timescaledb.enable_columnstore = true, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Columnstore is already enabled, you don't need to enable it. |
||
|
|
@@ -108,24 +115,30 @@ CREATE TABLE metrics ( | |
|
|
||
| 1. Query your data | ||
| ```sql | ||
| select avg(cpu) from metrics where time >= '2024-03-01 00:00:00+01' and time < '2024-03-02 00:00:00+01'; | ||
| ``` | ||
| SELECT device_id, AVG(cpu) AS avg_cpu, AVG(disk_io) AS avg_disk_io | ||
| FROM metrics | ||
| WHERE time >= '2024-03-01 00:00:00+01' | ||
| AND time < '2024-03-02 00:00:00+01' | ||
| AND device_id = 5 | ||
| GROUP BY device_id; | ||
| ``` | ||
| Gives the following result: | ||
|
|
||
| ```sql | ||
| device_id | avg_cpu | avg_disk_io | ||
| -----------+-------------------+--------------------- | ||
| 5 | 0.497259886622126 | 0.49820356730280535 | ||
| device_id | avg_cpu | avg_disk_io | ||
| -----------+--------------------+--------------------- | ||
| 5 | 0.4954351575883885 | 0.49725603413909114 | ||
| (1 row) | ||
| Time: 42,139 ms | ||
| Time: 1.828 ms | ||
| ``` | ||
|
|
||
| As you see, using `orderby` and `segmentby` not only reduces the amount of space taken by your data, but also | ||
| As you see, using `orderby` and `segmentby` not only reduces the amount of space taken by your data, but also | ||
| vastly improves query speed. | ||
|
|
||
| </Procedure> | ||
|
|
||
| The number of rows that are compressed together in a single batch (like the ones we see above) is 1000. | ||
| If your chunk does not contain enough data to create big enough batches, your compression ratio will be reduced. | ||
| If your $CHUNK does not contain enough data to create big enough batches, your compression ratio will be reduced. | ||
| This needs to be taken into account when you define your $COLUMNSTORE settings. | ||
|
|
||
| [hypercore]: /use-timescale/:currentVersion:/hypercore/ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.