This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_indexes.sql
58 lines (53 loc) · 1.9 KB
/
rebuild_indexes.sql
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
/**
* vim:filetype=plsql
* Script trying to rebuild all indexes on each table from tables_list.
* To use just specify table_list variable and run this script in SQL*Plus
* or any other Oralce client or IDE
*/
set serveroutput on size 1000000
declare
type t_table_list is table of varchar2(128);
table_list t_table_list := t_table_list('SOME_TABLE_NAME',
'ANOTHER_TABLE_NAME');
-- service variables
cmd varchar2(1024) := null;
begin
dbms_output.enable(1000000);
if table_list.count = 0 then
raise_application_error(-20001, 'Table list not specified.');
end if;
for i in table_list.first..table_list.last
loop
dbms_application_info.set_module(module_name=>'Rebuilding '
||table_list(i)||'indexes', action_name=>null);
dbms_output.put_line('Rebuilding indexes for '||table_list(i));
for rec in (select index_name
from user_indexes
where table_name = upper(table_list(i)))
loop
dbms_application_info.set_action(action_name => 'Trying to rebuild '
||rec.index_name);
dbms_output.put_line(' Trying to rebuild '||rec.index_name);
cmd := 'ALTER INDEX '||rec.index_name||' REBUILD';
dbms_output.put_line(' '||cmd);
begin
execute immediate cmd;
dbms_output.put_line(' Success!!!');
exception
when others then
begin
dbms_application_info.set_action(
action_name=>'Trying to online rebuild '||rec.index_name);
dbms_output.put_line(' Trying to rebuild online');
dbms_output.put_line(' '||cmd||' ONLINE');
execute immediate cmd||' ONLINE';
dbms_output.put_line(' Success!!!');
exception
when others then
dbms_output.put_line(' Error: '||sqlerrm(sqlcode));
end;
end;
end loop;
end loop;
end;
/