1.创建新的表空间
CREATE TABLESPACE bayonet
DATAFILE '/oradata/db/bayonet_01.DBF'
SIZE 2G
AUTOEXTEND ON NEXT 1G MAXSIZE UNLIMITED LOGGING
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO ;
2.修改用户默认表空间
ALTER USER dbuser DEFAULT tablespace bayonet;
3.创建迁移表到新的表空间SQL执行语句
==普通表:
SELECT 'alter table ' ||table_name || ' move tablespace bayonet;'
FROM user_all_tables
WHERE tablespace_name='USERS';
--无子分区
SELECT 'alter table ' ||t1.TABLE_NAME || ' move partition '|| t2.PARTITION_NAME ||' tablespace bayonet;'
FROM user_all_tables t1,USER_TAB_PARTITIONS t2
WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t2.tablespace_name='USERS' and t2.subpartition_count>0;
--子分区
SELECT 'alter table ' ||t1.TABLE_NAME || ' move subpartition '|| t2.subpartition_name ||' tablespace bayonet;'
FROM user_all_tables t1,all_tab_subpartitions t2
WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t2.tablespace_name='USERS';
SELECT 'alter table ' ||t1.TABLE_NAME || ' modify default ATTRIBUTES FOR partition '|| t2.PARTITION_NAME ||' tablespace bayonet;'
FROM user_all_tables t1,USER_TAB_PARTITIONS t2
WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t2.tablespace_name='USERS';
4.创建索引重建的SQL执行语句
select 'alter table '||table_name||' disable primary key;' from user_constraints where constraint_type='P' and R_OWNER='DBUSER';
SELECT 'alter index ' ||index_name || ' rebuild tablespace bayonet;'
FROM user_indexes
WHERE index_type='NORMAL' AND DROPPED='NO' AND
TABLE_NAME IN (
SELECT TABLE_NAME
FROM user_all_tables
WHERE tablespace_name='users'
);
select 'alter table '||table_name||' enable primary key;' from user_constraints
where constraint_type='P' and R_OWNER='DBUSER';
5.创建表分区
-- Create table
create table GPS_POSITION
(
ID NVARCHAR2(32) not null,
CARDNO NVARCHAR2(32),
GPSTIME DATE,
LONGITUDE NUMBER,
LATITUDE NUMBER,
E_W NVARCHAR2(8),
N_S NVARCHAR2(8),
SPEED NVARCHAR2(16),
GPSSPEED NVARCHAR2(32),
DIRECTION NVARCHAR2(16),
MILEAGE NVARCHAR2(16),
MILEAGE1 NVARCHAR2(16),
HIGHT NVARCHAR2(16),
TEMPERATURE NVARCHAR2(16),
OIL NVARCHAR2(16),
PASSENGER NVARCHAR2(16),
S0 NVARCHAR2(2),
S1 NVARCHAR2(2),
S2 NVARCHAR2(2),
S3 NVARCHAR2(2),
S4 NVARCHAR2(2),
A0 NVARCHAR2(2),
A1 NVARCHAR2(2),
A2 NVARCHAR2(2),
A3 NVARCHAR2(2),
A4 NVARCHAR2(2),
RUNTIME NVARCHAR2(32),
A_V NVARCHAR2(8),
CREATEDATE DATE default sysdate,
GPSID NUMBER,
NUM NUMBER,
WAY NUMBER,
TYPE NUMBER,
WAYSENTDATE NUMBER,
RECEIVEDDATE NUMBER,
B0 NVARCHAR2(2),
B1 NVARCHAR2(2),
B2 NVARCHAR2(2),
B3 NVARCHAR2(2),
B4 NVARCHAR2(2),
B5 NVARCHAR2(2),
B6 NVARCHAR2(2),
CITYID NVARCHAR2(20),
DATASTATE NUMBER,
GPSLOCK NVARCHAR2(2),
GPSPOWER NVARCHAR2(2),
ADDRESS NVARCHAR2(200)
)
partition by range (GPSTIME)
subpartition by list (CITYID)
(
partition P201010 values less than (TO_DATE(' 2010-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace bayonet
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 2
minextents 1
maxextents unlimited
)
(
subpartition P201010_P600000 values ('600000') tablespace USERS,
subpartition P201010_P600001 values ('600001') tablespace USERS,
subpartition P201010_P600002 values ('600002') tablespace USERS,
subpartition P201010_P600003 values ('600003') tablespace USERS,
subpartition P201010_P600004 values ('600004') tablespace USERS,
subpartition P201010_P600005 values ('600005') tablespace USERS,
subpartition P201010_P600006 values ('600006') tablespace USERS,
subpartition P201010_P600007 values ('600007') tablespace USERS,
subpartition P201010_P600008 values ('600008') tablespace USERS,
subpartition P201010_P600009 values ('600009') tablespace USERS,
subpartition P201010_P600010 values ('600010') tablespace USERS,
subpartition P201010_P600011 values ('600011') tablespace USERS,
subpartition P201010_P600012 values ('600012') tablespace USERS,
subpartition P201010_P600013 values ('600013') tablespace USERS,
subpartition P201010_P600014 values ('600014') tablespace USERS,
subpartition P201010_P600015 values ('600015') tablespace USERS,
subpartition P201010_P600016 values ('600016') tablespace USERS,
subpartition P201010_P600017 values ('600017') tablespace USERS,
subpartition P201010_P600018 values ('600018') tablespace USERS,
subpartition P201010_P600019 values ('600019') tablespace USERS,
subpartition P201010_P600020 values ('600020') tablespace USERS
),
partition P201112 values less than (TO_DATE(' 2011-12-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace bayonet
pctfree 10
initrans 1
maxtrans 255
(
subpartition P201112_P600000 values ('600000') tablespace USERS,
subpartition P201112_P600001 values ('600001') tablespace USERS,
subpartition P201112_P600002 values ('600002') tablespace USERS,
subpartition P201112_P600003 values ('600003') tablespace USERS,
subpartition P201112_P600004 values ('600004') tablespace USERS,
subpartition P201112_P600005 values ('600005') tablespace USERS,
subpartition P201112_P600006 values ('600006') tablespace USERS,
subpartition P201112_P600007 values ('600007') tablespace USERS,
subpartition P201112_P600008 values ('600008') tablespace USERS,
subpartition P201112_P600009 values ('600009') tablespace USERS,
subpartition P201112_P600010 values ('600010') tablespace USERS,
subpartition P201112_P600011 values ('600011') tablespace USERS,
subpartition P201112_P600012 values ('600012') tablespace USERS,
subpartition P201112_P600013 values ('600013') tablespace USERS,
subpartition P201112_P600014 values ('600014') tablespace USERS,
subpartition P201112_P600015 values ('600015') tablespace USERS,
subpartition P201112_P600016 values ('600016') tablespace USERS,
subpartition P201112_P600017 values ('600017') tablespace USERS,
subpartition P201112_P600018 values ('600018') tablespace USERS,
subpartition P201112_P600019 values ('600019') tablespace USERS,
subpartition P201112_P600020 values ('600020') tablespace USERS,
subpartition P201112_P600021 values ('600021') tablespace USERS,
subpartition P201112_P600022 values ('600022') tablespace USERS,
subpartition P201112_P600023 values ('600023') tablespace USERS,
subpartition P201112_PSUB_DEF values (default) tablespace USERS
),
partition P201201 values less than (TO_DATE(' 2012-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
tablespace bayonet
pctfree 10
initrans 1
maxtrans 255
(
subpartition P201201_P600000 values ('600000') tablespace USERS,
subpartition P201201_P600001 values ('600001') tablespace USERS,
subpartition P201201_P600002 values ('600002') tablespace USERS,
subpartition P201201_P600003 values ('600003') tablespace USERS,
subpartition P201201_P600004 values ('600004') tablespace USERS,
subpartition P201201_P600005 values ('600005') tablespace USERS,
subpartition P201201_P600006 values ('600006') tablespace USERS,
subpartition P201201_P600007 values ('600007') tablespace USERS,
subpartition P201201_P600008 values ('600008') tablespace USERS,
subpartition P201201_P600009 values ('600009') tablespace USERS,
subpartition P201201_P600010 values ('600010') tablespace USERS,
subpartition P201201_P600011 values ('600011') tablespace USERS,
subpartition P201201_P600012 values ('600012') tablespace USERS,
subpartition P201201_P600013 values ('600013') tablespace USERS,
subpartition P201201_P600014 values ('600014') tablespace USERS,
subpartition P201201_P600015 values ('600015') tablespace USERS,
subpartition P201201_P600016 values ('600016') tablespace USERS,
subpartition P201201_P600017 values ('600017') tablespace USERS,
subpartition P201201_P600018 values ('600018') tablespace USERS,
subpartition P201201_P600019 values ('600019') tablespace USERS,
subpartition P201201_P600020 values ('600020') tablespace USERS,
subpartition P201201_P600021 values ('600021') tablespace USERS,
subpartition P201201_P600022 values ('600022') tablespace USERS,
subpartition P201201_P600023 values ('600023') tablespace USERS,
subpartition P201201_PSUB_DEF values (default) tablespace USERS
)
);