C/C++教程

【Oracle】分区表(笔记)

本文主要是介绍【Oracle】分区表(笔记),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
-- 分表:订单表order采用年月分表后,出order本身外还生成order_202101、order_202102等的子表
-- 分表在逻辑上是多张不同的表,而分区表在逻辑上是一张表
--分区的条件(建议):
    -- 表数据量大于2GB
    -- 新增的数据都是插入最新分区中(一个典型的例子:新分区用于数据插入与修改,历史分区只用于读取操作)
    
-- 分区的好处
    --提高数据可用性
        --消除分区:优化器在查询时有需要的去除未用到的分区
        --减少停机时间:仅需要回复某个分区的表远小于回复整个数据库的表(100GB的表与50个2GB的分区)
    --方便管理:操作小对象比操作大对象容器,占用的资源更少
    --改善语句性能
    
-- 分区表机制
select * from user_tab_partitions t where lower(t.table_name) = 'dave_range_exp';

--1)范围分区 Range
create table dave_range_exp(
    id  varchar(16),
    create_date date,
    data varchar(32)
)
partition by range(create_date)
(
    partition Jan values less than ( to_date('20210201', 'yyyymmdd')),
    partition Feb values less than ( to_date('20210301', 'yyyymmdd')),
    partition Mar values less than ( to_date('20210401', 'yyyymmdd')),
    partition Apr values less than ( to_date('20210501', 'yyyymmdd')),
    partition May values less than ( to_date('20210601', 'yyyymmdd')),
    partition Jun values less than ( to_date('20210701', 'yyyymmdd')),
    partition Jul values less than ( to_date('20210801', 'yyyymmdd')),
    partition Aug values less than ( to_date('20210901', 'yyyymmdd')),
    partition Sep values less than ( to_date('20211001', 'yyyymmdd')),
    partition Oct values less than ( to_date('20211101', 'yyyymmdd')),
    partition Nov values less than ( to_date('20211201', 'yyyymmdd')),
    partition Dec values less than (maxvalue)
);

insert into dave_range_exp values('20081202020001', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '测试1');
insert into dave_range_exp values('20081202020002', to_date('2021-01-26','yyyy-mm-dd hh24:mi:ss'), '测试2');
insert into dave_range_exp values('20081202020003', to_date('2021-02-28','yyyy-mm-dd hh24:mi:ss'), '测试3');
insert into dave_range_exp values('20081202020004', to_date('2021-03-25','yyyy-mm-dd hh24:mi:ss'), '测试4');
insert into dave_range_exp values('20081202020005', to_date('2021-04-21','yyyy-mm-dd hh24:mi:ss'), '测试5');
insert into dave_range_exp values('20081202020006', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '测试6');
insert into dave_range_exp values('20081202020007', to_date('2021-01-26','yyyy-mm-dd hh24:mi:ss'), '测试7');
insert into dave_range_exp values('20081202020008', to_date('2021-02-28','yyyy-mm-dd hh24:mi:ss'), '测试8');
insert into dave_range_exp values('20081202020009', to_date('2021-03-25','yyyy-mm-dd hh24:mi:ss'), '测试9');
insert into dave_range_exp values('20081202020010', to_date('2021-04-21','yyyy-mm-dd hh24:mi:ss'), '测试10');

select * from dave_range_exp partition(Jan) order by data;

-- 2) 散列分区
-- oracle建议分区是2的一个幂(如2,4,8,16等)
create table dave_hash_exp(
    id  varchar(16),
    create_date date,
    data varchar(32)
)
partition by hash(create_date)
(
    partition part_1,
    partition part_2
);

insert into dave_hash_exp values('20081202020001', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '测试1');
insert into dave_hash_exp values('20081202020002', to_date('2021-01-26','yyyy-mm-dd hh24:mi:ss'), '测试2');
insert into dave_hash_exp values('20081202020003', to_date('2021-02-28','yyyy-mm-dd hh24:mi:ss'), '测试3');
insert into dave_hash_exp values('20081202020004', to_date('2021-03-25','yyyy-mm-dd hh24:mi:ss'), '测试4');
insert into dave_hash_exp values('20081202020005', to_date('2021-04-21','yyyy-mm-dd hh24:mi:ss'), '测试5');
insert into dave_hash_exp values('20081202020006', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '测试6');
insert into dave_hash_exp values('20081202020007', to_date('2021-01-26','yyyy-mm-dd hh24:mi:ss'), '测试7');
insert into dave_hash_exp values('20081202020008', to_date('2021-02-28','yyyy-mm-dd hh24:mi:ss'), '测试8');
insert into dave_hash_exp values('20081202020009', to_date('2021-03-25','yyyy-mm-dd hh24:mi:ss'), '测试9');
insert into dave_hash_exp values('20081202020010', to_date('2021-04-21','yyyy-mm-dd hh24:mi:ss'), '测试10');

select * from dave_hash_exp partition(part_1) order by data;

--3) 列表分区
create table dave_list_exp(
    id  varchar(16),
    create_date date,
    data varchar(32)
)
partition by list(id)
(
    partition part_1 values ('20081202020001', '20081202020002', '20081202020003', '20081202020004', '20081202020005'),
    partition part_2 values ('20081202020006', '20081202020007', '20081202020008'),
    partition part_3 values ('20081202020009', '20081202020010')
);

insert into dave_list_exp values('20081202020001', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '测试1');
insert into dave_list_exp values('20081202020002', to_date('2021-01-26','yyyy-mm-dd hh24:mi:ss'), '测试2');
insert into dave_list_exp values('20081202020003', to_date('2021-02-28','yyyy-mm-dd hh24:mi:ss'), '测试3');
insert into dave_list_exp values('20081202020004', to_date('2021-03-25','yyyy-mm-dd hh24:mi:ss'), '测试4');
insert into dave_list_exp values('20081202020005', to_date('2021-04-21','yyyy-mm-dd hh24:mi:ss'), '测试5');
insert into dave_list_exp values('20081202020006', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '测试6');
insert into dave_list_exp values('20081202020007', to_date('2021-01-26','yyyy-mm-dd hh24:mi:ss'), '测试7');
insert into dave_list_exp values('20081202020008', to_date('2021-02-28','yyyy-mm-dd hh24:mi:ss'), '测试8');
insert into dave_list_exp values('20081202020009', to_date('2021-03-25','yyyy-mm-dd hh24:mi:ss'), '测试9');
insert into dave_list_exp values('20081202020010', to_date('2021-04-21','yyyy-mm-dd hh24:mi:ss'), '测试10');

select * from dave_list_exp partition(part_1) order by data;

--4) 组合分区
-- 9i版本的oracle只支持2种复合分区,即range-hash和range-list
-- 11g版本的oracle增加了4种复合分区,即range-range、list-range、list-list、list-hash
create table dave_list_list_exp(
    id  varchar(16),
    create_date date,
    data varchar(32)
)
partition by list(id) subpartition by list(data)
(
    partition part_1 values ('340000000' )
    (
        subpartition part_1_sub_1 values('2020'),
        subpartition part_1_sub_2 values('2021'),
        subpartition part_1_sub_3 values('2022'),
        subpartition part_1_sub_4 values('2023')
    ),
    partition part_2 values ('340000001' )
    (
        subpartition part_2_sub_1 values('2020'),
        subpartition part_2_sub_2 values('2021'),
        subpartition part_2_sub_3 values('2022'),
        subpartition part_2_sub_4 values('2023')
    ),
    partition part_3 values ('340000002' )
    (
        subpartition part_3_sub_1 values('2020'),
        subpartition part_3_sub_2 values('2021'),
        subpartition part_3_sub_3 values('2022'),
        subpartition part_3_sub_4 values('2023')
    )
);

insert into dave_list_list_exp values('340000000', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '2020');
insert into dave_list_list_exp values('340000000', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '2020');
insert into dave_list_list_exp values('340000000', to_date('2021-01-29','yyyy-mm-dd hh24:mi:ss'), '2021');
insert into dave_list_list_exp values('340000000', to_date('2021-01-29','yyyy-mm-dd hh24:mi:ss'), '2021');
insert into dave_list_list_exp values('340000000', to_date('2021-01-30','yyyy-mm-dd hh24:mi:ss'), '2022');
insert into dave_list_list_exp values('340000000', to_date('2021-01-30','yyyy-mm-dd hh24:mi:ss'), '2022');
insert into dave_list_list_exp values('340000000', to_date('2021-01-31','yyyy-mm-dd hh24:mi:ss'), '2023');
insert into dave_list_list_exp values('340000000', to_date('2021-01-31','yyyy-mm-dd hh24:mi:ss'), '2023');
insert into dave_list_list_exp values('340000001', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '2020');
insert into dave_list_list_exp values('340000001', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '2020');
insert into dave_list_list_exp values('340000001', to_date('2021-01-29','yyyy-mm-dd hh24:mi:ss'), '2021');
insert into dave_list_list_exp values('340000001', to_date('2021-01-29','yyyy-mm-dd hh24:mi:ss'), '2021');
insert into dave_list_list_exp values('340000001', to_date('2021-01-30','yyyy-mm-dd hh24:mi:ss'), '2022');
insert into dave_list_list_exp values('340000001', to_date('2021-01-30','yyyy-mm-dd hh24:mi:ss'), '2022');
insert into dave_list_list_exp values('340000001', to_date('2021-01-31','yyyy-mm-dd hh24:mi:ss'), '2023');
insert into dave_list_list_exp values('340000001', to_date('2021-01-31','yyyy-mm-dd hh24:mi:ss'), '2023');
insert into dave_list_list_exp values('340000002', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '2020');
insert into dave_list_list_exp values('340000002', to_date('2021-01-28','yyyy-mm-dd hh24:mi:ss'), '2020');
insert into dave_list_list_exp values('340000002', to_date('2021-01-29','yyyy-mm-dd hh24:mi:ss'), '2021');
insert into dave_list_list_exp values('340000002', to_date('2021-01-29','yyyy-mm-dd hh24:mi:ss'), '2021');
insert into dave_list_list_exp values('340000002', to_date('2021-01-30','yyyy-mm-dd hh24:mi:ss'), '2022');
insert into dave_list_list_exp values('340000002', to_date('2021-01-30','yyyy-mm-dd hh24:mi:ss'), '2022');
insert into dave_list_list_exp values('340000002', to_date('2021-01-31','yyyy-mm-dd hh24:mi:ss'), '2023');
insert into dave_list_list_exp values('340000002', to_date('2021-01-31','yyyy-mm-dd hh24:mi:ss'), '2023');

-- 查询分区数据
select * from dave_list_list_exp  partition(part_1);
select * from dave_list_list_exp  subpartition(part_1_sub_2);


drop table dave_range_emp;
drop table dave_hash_exp;
drop table dave_list_exp;
drop table dave_list_list_exp;

 

这篇关于【Oracle】分区表(笔记)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!