今天遇到一個(gè)問題 之前用timescaledb創(chuàng)建的超表 是已7天為單位的 由于7天的數(shù)據(jù)量過大 影響查詢效率 需要把7天的分區(qū)改為一天一分區(qū)
創(chuàng)建超表 create_hypertable()
1.創(chuàng)建普通版表
1
2
3
4
5
6
7
|
create table "超表名" ( "collect_time" timestamp (6) not null , "code" varchar (36) collate "pg_catalog" . "default" not null , "value" numeric (14,4) not null , "create_time" timestamp (6) not null ) ; |
2.改為超表 create_hypertable()
1
|
select create_hypertable( '超表名' , 'collect_time' , chunk_time_interval => interval '7 day' ); |
3.插入數(shù)據(jù)
1
2
3
4
5
|
insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2020-10-15 16:35:00' , '22255220522' , '23.4672' , '2020-10-14 16:35:26.659' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2020-10-16 16:35:00' , '26622569666' , '0.1085' , '2020-10-14 16:35:27.546' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2020-10-13 16:35:00' , '525941155555' , '25.0549' , '2020-10-14 16:35:28.473' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2020-10-14 16:35:00' , '744445411114' , '0.0000' , '2020-10-14 16:35:24.01' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2020-10-12 16:35:00' , '774484457444' , '0.0000' , '2020-10-14 16:35:23.032' ); |
查看分區(qū),你會(huì)發(fā)現(xiàn)這些數(shù)據(jù)在2個(gè)分區(qū)內(nèi)
修改分區(qū) set_chunk_time_interval()
一.查看分區(qū)情況
1.查看_timescaledb_catalog.dimension 表
1
|
select * from "_timescaledb_catalog" . "dimension" |
interval_length上顯示 604800000000 (timestamp類型)意思是一周
2.查看分區(qū)塊狀態(tài)
查看 dimension_slice 表
轉(zhuǎn)換時(shí)間戳
1602720000000000 2020-10-15 08:00:00
1603324800000000 2020-10-22 08:00:00
這里可以看到分區(qū)是7天的
二.修改分區(qū)時(shí)間 set_chunk_time_interval()
1.修改分區(qū)時(shí)間
1
|
select set_chunk_time_interval( '超表名' , interval '24 hours' ); |
2.插入數(shù)據(jù)驗(yàn)證
1
2
3
4
5
6
|
insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2021-1-14 16:35:00' , '375222d001' , '27.7932' , '2020-10-14 16:35:15.011' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2021-1-15 16:35:00' , '3715044111' , '0.0000' , '2020-10-14 16:35:20.389' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2021-1-16 16:35:00' , '202q0019qt001' , '0.3663' , '2020-10-14 16:35:19.087' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2021-1-17 16:35:00' , '3702000284441' , '22.2946' , '2020-10-14 16:35:15.035' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2021-1-18 16:35:00' , '37075225555501' , '0.3022' , '2020-10-14 16:35:24.041' ); insert into 超表名( "collect_time" , "code" , "value" , "create_time" ) values ( '2021-1-19 16:35:00' , '25555222206001' , '0.0000' , '2020-10-14 16:35:23.956' ); |
三.查看 修改結(jié)果
查看_timescaledb_catalog.dimension 表
變成 86400000000 了
2.查看分區(qū)
分區(qū)也多了
還有第2種(未測試)
我想能不能直接"_timescaledb_catalog".“dimension” 表的 interval_length 字段直接 改為86400000000
到此這篇關(guān)于postgresql 數(shù)據(jù)庫 timescaledb 修改分區(qū)時(shí)間(范圍)的文章就介紹到這了,更多相關(guān)postgresql 數(shù)據(jù)庫 timescaledb 分區(qū)時(shí)間內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/yang_z_1/article/details/112766944