Oracle表分区

分区表

创建分区表

create table test(
id number,
name char(30),
systemtime char(12))
partition by range (systemtime)(
partition test202201 values less than ('202202010000'),
partition test202202 values less than ('202203010000'),
partition test202203 values less than ('202204010000')
);

查看分区表信息

col table_owner for a5
col table_name for a5
col partition_name for a10
select table_owner,table_name,partition_name,tablespace_name from dba_tab_partitions where table_name = 'TEST';

插入数据

insert into test values (1,'name1','202201300000');
insert into test values (2,'name2','202202280000');
insert into test values (3,'name2','202203300000');
COMMIT;

查看数据分布

select count(*) from test;
select count(*) from test partition(test202201);
select count(*) from test partition(test202202);
select count(*) from test partition(test202203);

添加分区

alter table test add partition test202204 values less than ('202205010000');

删除分区

alter table test drop partition test202204;

截断分区

alter table test truncate partition test202203;

锁定分区

12C以后,数据库支持设置分区只读

alter table test modify partition test202203 read only;

查看锁定情况

set linesize 100
column table_name format a30
column partition_name format a30
column read_only format a9
select table_name, partition_name,read_only from user_tab_partitions where  table_name = 'TEST' order by 1, 2;

12C以前需要使用lock命令
当前会话不断开,其他会话不能向当前分区写数据

lock table test partition (test202203) in exclusive mode;

查看锁定情况

col owner for a10
col object_name for a10
select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;

索引

索引创建

线上创建索引,加online没错

create index idx_test_01 on test(id) local online;

索引分区信息

col index_owner for a10
col index_name for a20
col partition_name for a20
select index_owner,index_name,partition_name,tablespace_name from dba_ind_partitions where index_name='IDX_TEST_01';

索引重建

alter index idx_test_01 rebuild partition TEST202201 online;

统计信息

exec dbms_stats.gather_table_stats(ownname => 'TEST',tabname => 'TEST', partname => 'TEST202201',estimate_percent => 100,method_opt=> 'for all indexed columns',cascade=>TRUE,granularity=>'ALL');

导出导入

exp/imp

exp test/test file='/home/oracle/dmp/test_test202201.dmp' log='/home/oracle/dmp/test_test202201.log' tables=test:test202201 statistics=none

导入前需要确保有对应的分区

alter table test add partition test202203 values less than ('202203010000');
imp test/test file='/home/oracle/dmp/test_test202201.dmp' log='/home/oracle/dmp/test_test202201.log' tables=test:test202201 ignore=y

expdp/impdp

多个分区tables=“(test:test202201,test:test202202)”

expdp test/test directory=DATA_DIR dumpfile=test_test202201.dmp logfile=test_test202201.log tables=test:test202201

导入前需要确保有对应的分区

alter table test add partition test202203 values less than ('202203010000');
impdp test/test directory=DATA_DIR dumpfile=test_test202203.dmp logfile=test_test202203.log table_exists_action=append
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值