-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add index creation for chunks without segmentby
Previously, for chunks without a segmentby column specified in the compression settings, no indexes were created. This change allows indexes to be created on orderby columns only in this scenario. Since there is no segmentby in the compression settings for these chunks we also disable segmentwise recompression for them as it is more optimal to do a full recompression in this case.
- Loading branch information
Showing
18 changed files
with
532 additions
and
532 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Implements: #7436 Add index creation on orderby columns |
This file contains 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
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
-- This file and its contents are licensed under the Timescale License. | ||
-- Please see the included NOTICE for copyright information and | ||
-- LICENSE-TIMESCALE for a copy of the license. | ||
-- force index scan to be used when possible | ||
set enable_seqscan to false; | ||
\set PREFIX 'EXPLAIN (analyze, costs off, summary off, timing off) ' | ||
create table segind(time timestamptz, a int, b int); | ||
select create_hypertable('segind', by_range('time')); | ||
NOTICE: adding not-null constraint to column "time" | ||
create_hypertable | ||
------------------- | ||
(1,t) | ||
(1 row) | ||
|
||
-- enable compression on hypertable with no segment by column | ||
alter table segind set (timescaledb.compress, timescaledb.compress_segmentby='', timescaledb.compress_orderby='time, b'); | ||
insert into segind values('2024-11-08 10:31:28.436014-07', 1, 1), ('2024-11-08 10:32:28.436014-07', 2, 1), ('2024-11-08 10:33:28.436014-07', 3, 1), ('2024-11-08 10:34:28.436014-07', 2, 1), ('2024-11-08 10:35:28.436014-07', 1, 2), ('2024-11-08 10:36:28.436014-07', 4, 1); | ||
-- compress chunk | ||
-- this should create an index using orderby columns | ||
select compress_chunk(show_chunks('segind')); | ||
compress_chunk | ||
---------------------------------------- | ||
_timescaledb_internal._hyper_1_1_chunk | ||
(1 row) | ||
|
||
-- query using orderby columns should use the index | ||
:PREFIX select * from segind where b = 1; | ||
QUERY PLAN | ||
-------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=5 loops=1) | ||
Vectorized Filter: (b = 1) | ||
Rows Removed by Filter: 1 | ||
-> Index Scan using compress_hyper_2_2_chunk__ts_meta_min_1__ts_meta_max_1__ts__idx on compress_hyper_2_2_chunk (actual rows=1 loops=1) | ||
Index Cond: ((_ts_meta_min_2 <= 1) AND (_ts_meta_max_2 >= 1)) | ||
(5 rows) | ||
|
||
:PREFIX select * from segind where time = '2024-11-08 10:32:28.436014-07'; | ||
QUERY PLAN | ||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=1 loops=1) | ||
Vectorized Filter: ("time" = 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) | ||
Rows Removed by Filter: 5 | ||
-> Index Scan using compress_hyper_2_2_chunk__ts_meta_min_1__ts_meta_max_1__ts__idx on compress_hyper_2_2_chunk (actual rows=1 loops=1) | ||
Index Cond: ((_ts_meta_min_1 <= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) AND (_ts_meta_max_1 >= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone)) | ||
(5 rows) | ||
|
||
:PREFIX select * from segind where b = 1 and time = '2024-11-08 10:32:28.436014-07'; | ||
QUERY PLAN | ||
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=1 loops=1) | ||
Vectorized Filter: ((b = 1) AND ("time" = 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone)) | ||
Rows Removed by Filter: 5 | ||
-> Index Scan using compress_hyper_2_2_chunk__ts_meta_min_1__ts_meta_max_1__ts__idx on compress_hyper_2_2_chunk (actual rows=1 loops=1) | ||
Index Cond: ((_ts_meta_min_1 <= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) AND (_ts_meta_max_1 >= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) AND (_ts_meta_min_2 <= 1) AND (_ts_meta_max_2 >= 1)) | ||
(5 rows) | ||
|
||
-- a query on another column should perform a seq scan since there is no index on it | ||
:PREFIX select * from segind where a = 1; | ||
QUERY PLAN | ||
--------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=2 loops=1) | ||
Vectorized Filter: (a = 1) | ||
Rows Removed by Filter: 4 | ||
-> Seq Scan on compress_hyper_2_2_chunk (actual rows=1 loops=1) | ||
(4 rows) | ||
|
||
-- decompress the chunk to drop the index | ||
select decompress_chunk(show_chunks('segind')); | ||
decompress_chunk | ||
---------------------------------------- | ||
_timescaledb_internal._hyper_1_1_chunk | ||
(1 row) | ||
|
||
-- change compression settings to use segmentby column | ||
alter table segind set (timescaledb.compress, timescaledb.compress_segmentby='a', timescaledb.compress_orderby='time, b'); | ||
-- compress chunk | ||
-- this should create an index using segmentby and orderby columns | ||
select compress_chunk(show_chunks('segind')); | ||
compress_chunk | ||
---------------------------------------- | ||
_timescaledb_internal._hyper_1_1_chunk | ||
(1 row) | ||
|
||
-- queries using segmentby or orderby columns should use the index | ||
:PREFIX select * from segind where b = 1; | ||
QUERY PLAN | ||
-------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=5 loops=1) | ||
Vectorized Filter: (b = 1) | ||
Rows Removed by Filter: 1 | ||
-> Index Scan using compress_hyper_2_3_chunk_a__ts_meta_min_1__ts_meta_max_1__t_idx on compress_hyper_2_3_chunk (actual rows=4 loops=1) | ||
Index Cond: ((_ts_meta_min_2 <= 1) AND (_ts_meta_max_2 >= 1)) | ||
(5 rows) | ||
|
||
:PREFIX select * from segind where time = '2024-11-08 10:32:28.436014-07'; | ||
QUERY PLAN | ||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=1 loops=1) | ||
Vectorized Filter: ("time" = 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) | ||
Rows Removed by Filter: 3 | ||
-> Index Scan using compress_hyper_2_3_chunk_a__ts_meta_min_1__ts_meta_max_1__t_idx on compress_hyper_2_3_chunk (actual rows=2 loops=1) | ||
Index Cond: ((_ts_meta_min_1 <= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) AND (_ts_meta_max_1 >= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone)) | ||
(5 rows) | ||
|
||
:PREFIX select * from segind where b = 1 and time = '2024-11-08 10:32:28.436014-07'; | ||
QUERY PLAN | ||
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=1 loops=1) | ||
Vectorized Filter: ((b = 1) AND ("time" = 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone)) | ||
Rows Removed by Filter: 3 | ||
-> Index Scan using compress_hyper_2_3_chunk_a__ts_meta_min_1__ts_meta_max_1__t_idx on compress_hyper_2_3_chunk (actual rows=2 loops=1) | ||
Index Cond: ((_ts_meta_min_1 <= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) AND (_ts_meta_max_1 >= 'Fri Nov 08 09:32:28.436014 2024 PST'::timestamp with time zone) AND (_ts_meta_min_2 <= 1) AND (_ts_meta_max_2 >= 1)) | ||
(5 rows) | ||
|
||
:PREFIX select * from segind where a = 1; | ||
QUERY PLAN | ||
-------------------------------------------------------------------------------------------------------------------------------------------- | ||
Custom Scan (DecompressChunk) on _hyper_1_1_chunk (actual rows=2 loops=1) | ||
-> Index Scan using compress_hyper_2_3_chunk_a__ts_meta_min_1__ts_meta_max_1__t_idx on compress_hyper_2_3_chunk (actual rows=1 loops=1) | ||
Index Cond: (a = 1) | ||
(3 rows) | ||
|
||
-- cleanup | ||
RESET enable_seqscan; |
This file contains 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
This file contains 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
Oops, something went wrong.