forked from freddenis/oracle-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredef.sh
192 lines (163 loc) · 6.11 KB
/
redef.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash
# Fred DENIS -- January 2013
# Redef online of a table (the objective to compress the table online)
#
#
# some default values
#
TMP=/tmp/redef$$.sql
cat /dev/null > $TMP
PARALLEL=16 # Parallelism for the redefinition
COMPRESSION=" compress for archive high "
DEBUG="NO"
DEBUG="" # comment to not debug
#
# Usage function
#
usage() {
cat << !
$0 -o OWNER -t TABLE -d DESTINATION_TABLESPACE
!
exit 1
}
#
# Manage the options
#
while getopts "o:t:d:" OPTION
do
case $OPTION in
o ) OWNER=`echo ${OPTARG} | tr '[a-z]' '[A-Z]'` ;;
t ) TABLE=`echo ${OPTARG} | tr '[a-z]' '[A-Z]'` ;;
d ) TBS=`echo ${OPTARG} | tr '[a-z]' '[A-Z]'` ;;
:) echo " l'option -$OPTARG need a value " ; usage ;;
\?) echo "Option -$OPTARG inconnue" ; usage ;;
esac
done
if [[ -z $DEBUG ]]
then
echo "owner : " $OWNER
echo "table : " $TABLE
echo "dest tablespace : " $TBS
fi
if [[ -z $OWNER ]] || [[ -z $TABLE ]] || [[ -z $TBS ]]
then
usage
fi
#
# Check if the table exists
#
RESULT=`sqlplus -S '/ as sysdba' << END_SQL
set pages 999 ;
set head off ;
set feed off ;
select count(*) from dba_tables where table_name = '${TABLE}' and owner = '${OWNER}' ;
END_SQL`
if [[ -z $DEBUG ]]
then
echo "result SQL table : " $RESULT
fi
if [ $RESULT -eq "0" ]
then
cat << !
The table ${OWNER}.${TABLE} does not exists.
!
exit 3
fi
#
# Check if the destination tablespace exists
#
RESULT=`sqlplus -S '/ as sysdba' << END_SQL
set pages 999 ;
set head off ;
set feed off ;
select count(*) from dba_tablespaces where tablespace_name = '${TBS}' ;
END_SQL`
if [[ -z $DEBUG ]]
then
echo "Result SQL tablespaces : " $RESULT
fi
if [ $RESULT -eq "0" ]
then
cat << !
The tablespace ${TBS} does not exists.
!
exit 4
fi
INTERIM=I${TABLE} # Name of the interim table
cat << END_SQL >> $TMP
set pages 999 ;
set lines 200 ;
set timing on ;
whenever sqlerror exit failure ;
set serveroutput on ;
alter session force parallel dml parallel $PARALLEL ;
alter session force parallel query parallel $PARALLEL ;
prompt -- Size of the table and index before redef
select tablespace_name, segment_name, round(bytes/1024/1024) "MB" from dba_segments where owner = '${OWNER}' and segment_name = '${TABLE}' ;
select tablespace_name, segment_name, round(bytes/1024/1024) "MB" from dba_segments where owner = '${OWNER}' and segment_name in (select index_name from dba_indexes where owner = '${OWNER}' and table_name = '${TABLE}') ;
select count(*) from ${OWNER}.${TABLE} ;
prompt -- Can redef table ?
exec DBMS_REDEFINITION.CAN_REDEF_TABLE('${OWNER}', '${TABLE}', DBMS_REDEFINITION.CONS_USE_ROWID);
prompt -- Creation of the interim table ${INTERIM}
-- for inittranscreate table ${OWNER}.${INTERIM} ${COMPRESSION} tablespace ${TBS} as select * from ${OWNER}.${TABLE} where 1 = 2 ;
create table ${OWNER}.${INTERIM} tablespace ${TBS} as select * from ${OWNER}.${TABLE} where 1 = 2 ;
alter table ${OWNER}.${INTERIM} initrans 100 maxtrans 255 ;
select ini_trans, max_trans from dba_tables where table_name = '${TABLE}' and owner = '${OWNER}' ;
prompt -- check constraint before redef
select table_name, constraint_type, constraint_name, status from dba_constraints where owner = '${OWNER}' and table_name = '${TABLE}' ;
select table_name, constraint_type, constraint_name, status from dba_constraints where owner = '${OWNER}' and table_name = '${INTERIM}' ;
prompt -- Drop constraint of the interim table (to avoid ORA-01442 error)
begin
for i in (select owner, table_name, constraint_name from dba_constraints where owner = '${OWNER}' and table_name = '${INTERIM}')
loop
execute immediate 'alter table ' || i.owner || '.' || i.table_name || ' drop constraint ' || i.constraint_name ;
end loop ;
end ;
/
prompt -- redef table
BEGIN
DBMS_REDEFINITION.START_REDEF_TABLE(
uname => '${OWNER}',
orig_table => '${TABLE}',
int_table => '${INTERIM}',
options_flag => DBMS_REDEFINITION.CONS_USE_ROWID);
END ;
/
prompt -- copy table dependents
DECLARE
error_count pls_integer := 0;
BEGIN
DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS('${OWNER}', '${TABLE}', '${INTERIM}', dbms_redefinition.cons_orig_params, TRUE,TRUE,TRUE,FALSE,error_count);
DBMS_OUTPUT.PUT_LINE('errors := ' || TO_CHAR(error_count));
end ;
/
prompt -- check for errors
select object_name, base_table_name, ddl_txt from DBA_REDEFINITION_ERRORS;
prompt -- sync interim table
exec DBMS_REDEFINITION.SYNC_INTERIM_TABLE('${OWNER}', '${TABLE}', '${INTERIM}') ;
prompt -- finish redef
exec DBMS_REDEFINITION.FINISH_REDEF_TABLE('${OWNER}', '${TABLE}', '${INTERIM}') ;
prompt -- move the index of the table to the new tablespace
begin
for i in (select owner, index_name from dba_indexes where owner = '${OWNER}' and table_name = '${TABLE}')
loop
execute immediate 'alter index ' || i.owner || '.' || i.index_name || ' rebuild tablespace ${TBS} online ' ;
end loop ;
end ;
/
prompt -- Size of the table and index before redef
select tablespace_name, segment_name, round(bytes/1024/1024) "MB" from dba_segments where owner = '${OWNER}' and segment_name = '${TABLE}' ;
select tablespace_name, segment_name, round(bytes/1024/1024) "MB" from dba_segments where owner = '${OWNER}' and segment_name in (select index_name from dba_indexes where owner = '${OWNER}' and table_name = '${TABLE}') ;
select count(*) from ${OWNER}.${TABLE} ;
prompt -- check constraint after redef
select table_name, constraint_type, constraint_name, status from dba_constraints where owner = '${OWNER}' and table_name = '${TABLE}' ;
exit ;
prompt -- drop interim table
drop table ${OWNER}.${INTERIM} ;
END_SQL
#sqlplus / as sysdba << !
# @${TMP}
#!
#cat $TMP
echo $TMP
#rm $TMP