-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpctfree_advice.sql
169 lines (150 loc) · 4.8 KB
/
pctfree_advice.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
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
REM pctfree_advice.sql
REM (c)Go-Faster Consultancy 2008
CREATE TABLE chained_rows (
owner_name varchar2(30),
table_name varchar2(30),
cluster_name varchar2(30),
partition_name varchar2(30),
subpartition_name varchar2(30),
head_rowid urowid,
analyze_timestamp date
);
DROP INDEX chained_rows;
TRUNCATE TABLE chained_rows;
CREATE INDEX chained_rows ON chained_rows (owner_name,table_name) COMPRESS 2 PCTFREE 0;
BEGIN
FOR x IN (
SELECT owner, table_name, num_rows
FROM all_tables t
WHERE 1=1
/*-------------------------------------------------------
AND NOT table_name IN(SELECT DISTINCT table_name FROM chained_rows)
AND num_rows >= 1000000
AND num_rows BETWEEN 100000 AND 1000000
/*--------------------------------------------------------*/
AND table_name like 'PS_FT_YB%'
AND temporary = 'N'
) LOOP
DELETE FROM chained_rows WHERE owner_name = x.owner AND table_name = x.table_name;
EXECUTE IMMEDIATE 'ANALYZE TABLE '||x.owner||'.'||x.table_name||' LIST CHAINED ROWS INTO chained_rows';
COMMIT;
END LOOP;
END;
/
COLUMN owner_name FORMAT a8 HEADING 'Owner'
COLUMN pct_chained FORMAT 990.9
COLUMN chained_rows HEADING 'Chained|Rows'
COLUMN num_rows HEADING 'Number|of Rows' FORMAT 9999999
COLUMN pct_chained HEADING '%Chained'
COLUMN pct_Free HEADING '%|Free' FORMAT 999
COLUMN pct_used HEADING '%|Used' FORMAT 999
COLUMN new_pct_free HEADING 'New|%Free' FORMAT 990
COLUMN blocks FORMAT 999990 HEADING 'Blocks'
COLUMN avg_space FORMAT 99990 HEADING 'Space|per|Block'
COLUMN table_name FORMAT a30 HEADING 'Table Name'
COLUMN chain_cnt FORMAT 999999 HEADING 'Chain|Count'
COLUMN avg_row_len HEADING 'Average|Row|Length' FORMAT 99999
COLUMN wastage HEADING 'Wastage|(Mb)' FORMAT 9990
spool pctfree_advice
SELECT /*+LEADING(c)*/ c.*, t.num_rows
, c.chained_rows/t.num_rows*100 pct_chained
, t.pct_free, t.pct_used
, 100-FLOOR((100-t.pct_free)*(1-c.chained_rows/t.num_rows)) new_pct_free
from (
SELECT owner_name, table_name, COUNT(*) chained_rows
FROM chained_rows c
GROUP BY owner_name, table_name) c
, all_tables t
WHERE t.owner = c.owner_name
AND t.table_name = c.table_name
AND t.num_rows > 0
ORDER BY chained_rows desc, 1,2
/
SELECT /*+LEADING(c)*/ c.*, t.num_rows
, c.chained_rows/t.num_rows*100 pct_chained
, t.pct_free, t.pct_used
, 100-FLOOR((100-t.pct_free)*(1-c.chained_rows/t.num_rows)) new_pct_free
from (
SELECT owner_name, table_name, partition_name, COUNT(*) chained_rows
FROM chained_rows c
GROUP BY owner_name, table_name, partition_name) c
, all_tab_partitions t
WHERE t.table_owner = c.owner_name
AND t.table_name = c.table_name
AND t.partition_name = c.partition_name
AND t.num_rows > 0
ORDER BY 1,2,3
/
SELECT 'ALTER TABLE '||owner_name||'.'||table_name||
' MOVE TABLESPACE '||tablespace_name||' PCTFREE 1;'
FROM (
SELECT c.owner_name, c.table_name
, c.chained_rows/t.num_rows*100 pct_chained
, t.pct_free, t.pct_used
, 100-FLOOR((100-t.pct_free)*(1-c.chained_rows/t.num_rows)) new_pct_free
, t.tablespace_name
FROM (
SELECT owner_name, table_name, COUNT(*) chained_rows
FROM chained_rows c
GROUP BY owner_name, table_name) c
, all_tables t
WHERE t.owner = c.owner_name
AND t.table_name = c.table_name
AND t.num_rows > 0
AND c.chained_rows >= 500
ORDER BY c.chained_rows,1,2
)
/
SELECT 'ALTER TABLE '||owner_name||'.'||table_name||
' PCTFREE '||new_pct_free||' /*'||pct_free||'*/ PCTUSED '||LEAST(pct_used,90-new_pct_free)||';'
FROM (
SELECT c.owner_name, c.table_name
, c.chained_rows/t.num_rows*100 pct_chained
, t.pct_free, t.pct_used
, 100-FLOOR((100-t.pct_free)*(1-c.chained_rows/t.num_rows)) new_pct_free
FROM (
SELECT owner_name, table_name, COUNT(*) chained_rows
FROM chained_rows c
GROUP BY owner_name, table_name) c
, all_tables t
WHERE t.owner = c.owner_name
AND t.table_name = c.table_name
AND t.num_rows > 0
AND c.chained_rows >= 500
ORDER BY c.chained_rows, 1,2
)
/
SELECT x.cmd
FROM (
SELECT i.*
, 'ALTER INDEX '||index_name||' REBUILD TABLESPACE '||tablespace_name||';' cmd
FROM user_indexes i
WHERE i.tablespace_name IS NOT NULL
AND NOT i.index_type = 'LOB'
UNION ALL
SELECT j.*
, 'ALTER INDEX '||index_name||' REBUILD;'
FROM user_indexes j
WHERE j.tablespace_name IS NULL
) x
, (
SELECT owner_name, table_name, COUNT(*) chained_rows
FROM chained_rows c
GROUP BY owner_name, table_name
) c
WHERE c.table_name = x.table_name
AND c.owner_name = x.table_owner
AND c.chained_rows >= 500
ORDER BY c.chained_rows desc, x.index_name
/
SELECT 'ALTER INDEX '||index_name||' REBUILD TABLESPACE '||tablespace_name||';'
FROM user_indexes
WHERE status = 'UNUSABLE'
AND tablespace_name IS NOT NULL
UNION ALL
SELECT 'ALTER INDEX '||index_name||' REBUILD;'
FROM user_indexes
WHERE status = 'UNUSABLE'
and tablespace_name IS NULL
/
spool off