2026-03-12 21:01:38
This commit is contained in:
2
partitioning/articles_01.txt
Normal file
2
partitioning/articles_01.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Implementing dynamic partitions AND subpartitions
|
||||
https://connor-mcdonald.com/2022/04/22/implementing-dynamic-partitions-and-subpartitions/
|
||||
126
partitioning/range_to_interval_01.txt
Normal file
126
partitioning/range_to_interval_01.txt
Normal file
@@ -0,0 +1,126 @@
|
||||
-- http://www.oraclefindings.com/2017/07/23/switching-range-interval-partitioning/
|
||||
|
||||
drop table DEMO purge;
|
||||
|
||||
create table DEMO(
|
||||
id INTEGER generated always as identity
|
||||
,day DATE not null
|
||||
,code VARCHAR2(2) not null
|
||||
,val NUMBER not null
|
||||
,PRIMARY KEY(id)
|
||||
)
|
||||
partition by range(day)(
|
||||
partition P_2024_01 values less than (date'2024-02-01')
|
||||
,partition P_2024_02 values less than (date'2024-03-01')
|
||||
,partition INFINITY values less than (MAXVALUE)
|
||||
)
|
||||
;
|
||||
|
||||
create index IDX_VAL on DEMO(val) local;
|
||||
|
||||
|
||||
insert into DEMO (day,code,val) values (date'2024-01-09','UK',1005);
|
||||
insert into DEMO (day,code,val) values (date'2024-01-10','IT',900);
|
||||
insert into DEMO (day,code,val) values (date'2024-01-11','IT',400);
|
||||
insert into DEMO (day,code,val) values (date'2024-01-11','FR',400);
|
||||
insert into DEMO (day,code,val) values (date'2024-01-12','UK',400);
|
||||
insert into DEMO (day,code,val) values (date'2024-01-12','IT',500);
|
||||
|
||||
insert into DEMO (day,code,val) values (date'2024-02-07','UK',765);
|
||||
insert into DEMO (day,code,val) values (date'2024-02-09','IT',551);
|
||||
insert into DEMO (day,code,val) values (date'2024-02-09','IT',90);
|
||||
insert into DEMO (day,code,val) values (date'2024-02-09','FR',407);
|
||||
insert into DEMO (day,code,val) values (date'2024-02-09','UK',101);
|
||||
insert into DEMO (day,code,val) values (date'2024-02-10','IT',505);
|
||||
insert into DEMO (day,code,val) values (date'2024-02-10','FR',2000);
|
||||
|
||||
commit;
|
||||
|
||||
|
||||
exec dbms_stats.gather_table_stats(user,'DEMO');
|
||||
exec dbms_stats.delete_table_stats(user,'DEMO');
|
||||
|
||||
-- IMPORTANT: the table should NOT have a MAXVALUE partition
|
||||
-- ALTER TABLE… SET INTERVAL fails with: ORA-14759: SET INTERVAL is not legal on this table. (Doc ID 2926948.1)
|
||||
|
||||
select count(*) from DEMO partition (INFINITY);
|
||||
-- Drop the MAXVALUE partition.
|
||||
alter table POC.DEMO drop partition INFINITY;
|
||||
|
||||
alter table DEMO set interval(NUMTOYMINTERVAL(1, 'MONTH'));
|
||||
|
||||
insert into DEMO (day,code,val) values (date'2024-04-01','IT',50);
|
||||
insert into DEMO (day,code,val) values (date'2024-05-12','FR',60);
|
||||
insert into DEMO (day,code,val) values (date'2024-05-14','UK',70);
|
||||
commit;
|
||||
|
||||
|
||||
-------------------------------------------------------------
|
||||
|
||||
drop table DEMO purge;
|
||||
|
||||
create table DEMO(
|
||||
id INTEGER generated always as identity
|
||||
,day DATE not null
|
||||
,code VARCHAR2(2) not null
|
||||
,val NUMBER not null
|
||||
,PRIMARY KEY(id)
|
||||
)
|
||||
partition by range(day) subpartition by list (code)(
|
||||
partition P_2024_01 values less than (date'2024-02-01')
|
||||
(
|
||||
subpartition P_2024_01_UK values ('UK')
|
||||
,subpartition P_2024_01_IT values ('IT')
|
||||
,subpartition P_2024_01_FR values ('FR')
|
||||
)
|
||||
,partition P_2024_02 values less than (date'2024-03-01')
|
||||
(
|
||||
subpartition P_2024_02_UK values ('UK')
|
||||
,subpartition P_2024_02_IT values ('IT')
|
||||
,subpartition P_2024_02_FR values ('FR')
|
||||
)
|
||||
,partition INFINITY values less than (MAXVALUE)
|
||||
(
|
||||
subpartition INFINITY_UK values ('UK')
|
||||
,subpartition INFINITY_IT values ('IT')
|
||||
,subpartition INFINITY_FR values ('FR')
|
||||
)
|
||||
)
|
||||
;
|
||||
|
||||
create index IDX_VAL on DEMO(val) local;
|
||||
|
||||
alter table POC.DEMO drop partition INFINITY;
|
||||
alter table DEMO set interval(NUMTOYMINTERVAL(1, 'MONTH'));
|
||||
|
||||
alter index POC.SYS_C007367 rebuild;
|
||||
|
||||
|
||||
ALTER TABLE DEMO SPLIT SUBPARTITION SYS_SUBP3241
|
||||
VALUES ('UK') INTO (
|
||||
SUBPARTITION SYS_SUBP3241_UK,
|
||||
SUBPARTITION SYS_SUBP3241_DIFF
|
||||
)
|
||||
ONLINE;
|
||||
|
||||
ALTER TABLE DEMO SPLIT SUBPARTITION SYS_SUBP3241_DIFF
|
||||
VALUES ('IT') INTO (
|
||||
SUBPARTITION SYS_SUBP3241_IT,
|
||||
SUBPARTITION SYS_SUBP3241_FR
|
||||
)
|
||||
ONLINE;
|
||||
|
||||
-- because wrong previous subpart name
|
||||
alter table POC.DEMO rename subpartition SYS_SUBP3241_FR to SYS_SUBP3241_DIFF;
|
||||
|
||||
ALTER TABLE DEMO SPLIT SUBPARTITION SYS_SUBP3241_DIFF
|
||||
VALUES ('FR') INTO (
|
||||
SUBPARTITION SYS_SUBP3241_FR,
|
||||
SUBPARTITION SYS_SUBP3241_OTHER
|
||||
)
|
||||
ONLINE;
|
||||
|
||||
select count(*) from DEMO subpartition(SYS_SUBP3241_OTHER);
|
||||
alter table DEMO drop subpartition SYS_SUBP3241_OTHER;
|
||||
|
||||
|
||||
93
partitioning/range_to_interval_02
Normal file
93
partitioning/range_to_interval_02
Normal file
@@ -0,0 +1,93 @@
|
||||
drop table DEMO purge;
|
||||
|
||||
create table DEMO(
|
||||
id INTEGER generated always as identity
|
||||
,day DATE not null
|
||||
,code VARCHAR2(2) not null
|
||||
,val NUMBER not null
|
||||
,PRIMARY KEY(id)
|
||||
)
|
||||
partition by range(day)(
|
||||
partition P_2024_01 values less than (date'2024-02-01')
|
||||
,partition P_2024_02 values less than (date'2024-03-01')
|
||||
,partition P_2024_03 values less than (date'2024-04-01')
|
||||
,partition P_2024_04 values less than (date'2024-05-01')
|
||||
,partition P_2024_05 values less than (date'2024-06-01')
|
||||
,partition P_2024_06 values less than (date'2024-07-01')
|
||||
,partition INFINITY values less than (MAXVALUE)
|
||||
)
|
||||
;
|
||||
|
||||
create index IDX_VAL on DEMO(val) local;
|
||||
|
||||
insert /*+ APPEND */ into DEMO (day,code,val)
|
||||
select
|
||||
DATE'2024-01-01' + trunc(DBMS_RANDOM.VALUE(1,30*4))
|
||||
,DECODE(trunc(DBMS_RANDOM.VALUE(1,10)),
|
||||
1, 'UK'
|
||||
,2, 'UK'
|
||||
,3, 'UK'
|
||||
,4, 'UK'
|
||||
,5, 'UK'
|
||||
,6, 'IT'
|
||||
,7, 'IT'
|
||||
,8, 'FR'
|
||||
,9, 'FR'
|
||||
,10, 'FR'
|
||||
)
|
||||
,trunc(DBMS_RANDOM.VALUE(1,10000))
|
||||
from
|
||||
xmltable('1 to 4000000')
|
||||
;
|
||||
|
||||
commit;
|
||||
|
||||
|
||||
exec dbms_stats.gather_table_stats(user,'DEMO');
|
||||
exec dbms_stats.delete_table_stats(user,'DEMO');
|
||||
|
||||
exec DBMS_STATS.SET_TABLE_PREFS(ownname=>'POC',tabname=>'DEMO', pname=>'INCREMENTAL', pvalue=>'TRUE');
|
||||
exec DBMS_STATS.SET_TABLE_PREFS(ownname=>'POC',tabname=>'DEMO', pname=>'GRANULARITY', pvalue=>'PARTITION');
|
||||
|
||||
exec DBMS_STATS.SET_TABLE_PREFS(ownname=>'POC',tabname=>'DEMO', pname=>'INCREMENTAL', pvalue=>'FALSE');
|
||||
exec DBMS_STATS.SET_TABLE_PREFS(ownname=>'POC',tabname=>'DEMO', pname=>'GRANULARITY', pvalue=>'AUTO');
|
||||
|
||||
|
||||
execute dbms_stats.gather_table_stats(ownname=>'POC',tabname=>'DEMO', partname=>'P_2024_01');
|
||||
|
||||
execute dbms_stats.delete_table_stats(ownname=>'POC',tabname=>'DEMO', partname=>'P_2024_06');
|
||||
execute dbms_stats.gather_table_stats(ownname=>'POC',tabname=>'DEMO', partname=>'P_2024_06');
|
||||
execute dbms_stats.gather_table_stats(ownname=>'POC',tabname=>'DEMO', partname=>'P_2024_06',granularity=>'PARTITION');
|
||||
|
||||
|
||||
alter table POC.DEMO drop partition P_2024_05 update global indexes;
|
||||
alter table POC.DEMO drop partition P_2024_06 update global indexes;
|
||||
alter table POC.DEMO drop partition INFINITY update global indexes;
|
||||
|
||||
alter table DEMO set interval(NUMTOYMINTERVAL(1, 'MONTH'));
|
||||
|
||||
insert /*+ APPEND */ into DEMO (day,code,val)
|
||||
select
|
||||
DATE'2024-07-01' + trunc(DBMS_RANDOM.VALUE(1,30*1))
|
||||
,DECODE(trunc(DBMS_RANDOM.VALUE(1,10)),
|
||||
1, 'UK'
|
||||
,2, 'UK'
|
||||
,3, 'UK'
|
||||
,4, 'UK'
|
||||
,5, 'UK'
|
||||
,6, 'IT'
|
||||
,7, 'IT'
|
||||
,8, 'FR'
|
||||
,9, 'FR'
|
||||
,10, 'FR'
|
||||
)
|
||||
,trunc(DBMS_RANDOM.VALUE(1,10000))
|
||||
from
|
||||
xmltable('1 to 1000000')
|
||||
;
|
||||
|
||||
commit;
|
||||
|
||||
|
||||
|
||||
|
||||
131
partitioning/range_to_interval_03
Normal file
131
partitioning/range_to_interval_03
Normal file
@@ -0,0 +1,131 @@
|
||||
drop table DEMO1 purge;
|
||||
|
||||
create table DEMO1(
|
||||
id INTEGER generated always as identity
|
||||
,day DATE not null
|
||||
,code VARCHAR2(2) not null
|
||||
,val NUMBER not null
|
||||
,PRIMARY KEY(id)
|
||||
)
|
||||
partition by range(day)(
|
||||
partition P_2024_01 values less than (date'2024-02-01')
|
||||
,partition P_2024_02 values less than (date'2024-03-01')
|
||||
,partition P_2024_03 values less than (date'2024-04-01')
|
||||
,partition P_2024_04 values less than (date'2024-05-01')
|
||||
)
|
||||
;
|
||||
|
||||
create index IDX_VAL on DEMO1(val) local;
|
||||
|
||||
insert /*+ APPEND */ into DEMO1 (day,code,val)
|
||||
select
|
||||
DATE'2024-01-01' + trunc(DBMS_RANDOM.VALUE(1,30*4))
|
||||
,DECODE(trunc(DBMS_RANDOM.VALUE(1,10)),
|
||||
1, 'UK'
|
||||
,2, 'UK'
|
||||
,3, 'UK'
|
||||
,4, 'UK'
|
||||
,5, 'UK'
|
||||
,6, 'IT'
|
||||
,7, 'IT'
|
||||
,8, 'FR'
|
||||
,9, 'FR'
|
||||
,10, 'FR'
|
||||
)
|
||||
,trunc(DBMS_RANDOM.VALUE(1,10000))
|
||||
from
|
||||
xmltable('1 to 4000000')
|
||||
;
|
||||
commit;
|
||||
|
||||
-- create a copy of the table
|
||||
|
||||
drop table DEMO2 purge;
|
||||
|
||||
create table DEMO2(
|
||||
id INTEGER generated always as identity
|
||||
,day DATE not null
|
||||
,code VARCHAR2(2) not null
|
||||
,val NUMBER not null
|
||||
,PRIMARY KEY(id)
|
||||
)
|
||||
partition by range(day)(
|
||||
partition P_2024_01 values less than (date'2024-02-01')
|
||||
,partition P_2024_02 values less than (date'2024-03-01')
|
||||
,partition P_2024_03 values less than (date'2024-04-01')
|
||||
,partition P_2024_04 values less than (date'2024-05-01')
|
||||
)
|
||||
;
|
||||
|
||||
create index IDX_VAL2 on DEMO2(val) local;
|
||||
|
||||
insert /*+ APPEND */ into DEMO2 (day,code,val) select day,code,val from DEMO1;
|
||||
commit;
|
||||
|
||||
|
||||
exec dbms_stats.gather_table_stats(user,'DEMO1');
|
||||
|
||||
exec DBMS_STATS.SET_TABLE_PREFS(ownname=>'POC',tabname=>'DEMO2', pname=>'INCREMENTAL', pvalue=>'TRUE');
|
||||
exec DBMS_STATS.SET_TABLE_PREFS(ownname=>'POC',tabname=>'DEMO2', pname=>'GRANULARITY', pvalue=>'PARTITION');
|
||||
exec dbms_stats.gather_table_stats(user,'DEMO2');
|
||||
|
||||
-- initial stats on DEM02 are faster because of incremental aggregation of GLOBAL stats instead basic calculation
|
||||
-- repeating gather table stats is much faster on DEMO2 (last_analyzed is nut increased)
|
||||
(test if inserting a couple of lines in every partition change someting)
|
||||
|
||||
|
||||
-- convert tables to INTERVAL
|
||||
alter table DEMO1 set interval(NUMTOYMINTERVAL(1, 'MONTH'));
|
||||
alter table DEMO2 set interval(NUMTOYMINTERVAL(1, 'MONTH'));
|
||||
|
||||
-- insert a lot of lines in 1-st partition
|
||||
drop table DEMOAUX purge;
|
||||
create table DEMOAUX as select * from DEMO1 where 1=0;
|
||||
alter table DEMOAUX drop column ID;
|
||||
|
||||
insert /*+ APPEND */ into DEMOAUX (day,code,val)
|
||||
select
|
||||
DATE'2024-01-01' + trunc(DBMS_RANDOM.VALUE(1,30*1))
|
||||
,DECODE(trunc(DBMS_RANDOM.VALUE(1,10)),
|
||||
1, 'UK'
|
||||
,2, 'UK'
|
||||
,3, 'UK'
|
||||
,4, 'UK'
|
||||
,5, 'UK'
|
||||
,6, 'IT'
|
||||
,7, 'IT'
|
||||
,8, 'FR'
|
||||
,9, 'FR'
|
||||
,10, 'FR'
|
||||
)
|
||||
,trunc(DBMS_RANDOM.VALUE(1,10000))
|
||||
from
|
||||
xmltable('1 to 1000000')
|
||||
;
|
||||
commit;
|
||||
|
||||
|
||||
insert /*+ APPEND */ into DEMO1 (day,code,val) select day,code,val from DEMOAUX;
|
||||
commit;
|
||||
|
||||
insert /*+ APPEND */ into DEMO2 (day,code,val) select day,code,val from DEMOAUX;
|
||||
commit;
|
||||
|
||||
|
||||
-- stats on DEMO2 will be faster
|
||||
exec dbms_stats.gather_table_stats(user,'DEMO1');
|
||||
exec dbms_stats.gather_table_stats(user,'DEMO2');
|
||||
|
||||
|
||||
=> check why GLOBAL stats on DEMO2 are staled
|
||||
|
||||
|
||||
|
||||
-- insert 1 line in a new partition
|
||||
insert into DEMO1 (day,code,val) values (date'2024-05-09','UK',1005);
|
||||
insert into DEMO2 (day,code,val) values (date'2024-05-09','UK',1005);
|
||||
commit;
|
||||
|
||||
|
||||
|
||||
|
||||
10
partitioning/range_to_interval_topics_01
Normal file
10
partitioning/range_to_interval_topics_01
Normal file
@@ -0,0 +1,10 @@
|
||||
- classic partitionned table DEMO transformation to INTERVAL partitionning
|
||||
- insert data for 4 months
|
||||
- try to modify / delete MAXVALUE partition UPDATING GLOBAL indexes
|
||||
- transform table to INTERVAL
|
||||
- insert 2 months of data (if old named partition exists, it will be used)
|
||||
- rename automatic partitions
|
||||
|
||||
|
||||
STATS
|
||||
- with default stats parameters
|
||||
Reference in New Issue
Block a user