0%

回收站过大导致表空间查询缓慢

目前数据库查询表空间使用率非常慢,甚至要半个小时才会出结果。查询语句的执行计划如下:

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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
SQL>explain plan for select t.*
    from (select d.tablespace_name,
                              space "sum_space(m)",
                              blocks sum_blocks,
                              space - nvl(free_space, 0) "used_space(m)",
                              round((1 - nvl(free_space, 0) / space) * 100, 2) "used_rate(%)",
                              free_space "free_space(m)"
                    from (select tablespace_name,
                                              round(sum(bytes) / (1048576), 2) space,
                                              sum(blocks) blocks
                                    from dba_data_files
                                  group by tablespace_name) d,
                              (select tablespace_name,
                                              round(sum(bytes) / (1048576), 2) free_space
                                    from dba_free_space
                                  group by tablespace_name) f
                  where d.tablespace_name = f.tablespace_name(+)
                union all --if have tempfile
                select d.tablespace_name,
                              space "sum_space(m)",
                              blocks sum_blocks,
                              used_space "used_space(m)",
                              round(nvl(used_space, 0) / space * 100, 2) "used_rate(%)",
                              space - used_space "free_space(m)"
                    from (select tablespace_name,
                                              round(sum(bytes) / (1048576), 2) space,
                                              sum(blocks) blocks
                                    from dba_temp_files
                                  group by tablespace_name) d,
                              (select tablespace,
                                              round(sum(blocks * 32768) / (1048576), 2) used_space
                                    from v$sort_usage
                                  group by tablespace) f
                  where d.tablespace_name = f.tablespace(+)) t
order by "used_rate(%)" desc;
SQL> SELECT plan_table_output FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE'));
Plan hash value: 1739573977
-----------------------------------------------------------------------------------------------------------
  
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  
-----------------------------------------------------------------------------------------------------------
  
| 0 | SELECT STATEMENT | | 3 | 246 | 4386 (5)| 00:00:53 |
  
| 1 | SORT ORDER BY | | 3 | 246 | 4386 (5)| 00:00:53 |
  
| 2 | VIEW | | 3 | 246 | 4385 (5)| 00:00:53 |
  
| 3 | UNION-ALL | | | | | |
  
|* 4 | HASH JOIN OUTER | | 2 | 146 | 4382 (5)| 00:00:53 |
  
| 5 | VIEW | | 2 | 86 | 5 (20)| 00:00:01 |
  
| 6 | HASH GROUP BY | | 2 | 70 | 5 (20)| 00:00:01 |
  
| 7 | VIEW | DBA_DATA_FILES | 2 | 70 | 4 (0)| 00:00:01 |
  
| 8 | UNION-ALL | | | | | |
  
| 9 | NESTED LOOPS | | 1 | 359 | 2 (0)| 00:00:01 |
  
| 10 | NESTED LOOPS | | 1 | 343 | 1 (0)| 00:00:01 |
  
| 11 | NESTED LOOPS | | 1 | 330 | 1 (0)| 00:00:01 |
  
|* 12 | FIXED TABLE FULL | X$KCCFN | 1 | 310 | 0 (0)| 00:00:01 |
  
|* 13 | TABLE ACCESS BY INDEX ROWID| FILE$ | 1 | 20 | 1 (0)| 00:00:01 |
  
|* 14 | INDEX UNIQUE SCAN | I_FILE1 | 1 | | 0 (0)| 00:00:01 |
  
|* 15 | FIXED TABLE FIXED INDEX | X$KCCFE (ind:1) | 1 | 13 | 0 (0)| 00:00:01 |
  
| 16 | TABLE ACCESS CLUSTER | TS$ | 1 | 16 | 1 (0)| 00:00:01 |
  
|* 17 | INDEX UNIQUE SCAN | I_TS# | 1 | | 0 (0)| 00:00:01 |
  
| 18 | NESTED LOOPS | | 1 | 402 | 2 (0)| 00:00:01 |
  
| 19 | NESTED LOOPS | | 1 | 386 | 1 (0)| 00:00:01 |
  
| 20 | NESTED LOOPS | | 1 | 373 | 1 (0)| 00:00:01 |
  
| 21 | NESTED LOOPS | | 1 | 362 | 0 (0)| 00:00:01 |
  
|* 22 | FIXED TABLE FULL | X$KCCFN | 1 | 310 | 0 (0)| 00:00:01 |
  
|* 23 | FIXED TABLE FIXED INDEX | X$KTFBHC (ind:1) | 1 | 52 | 0 (0)| 00:00:01 |
  
|* 24 | TABLE ACCESS BY INDEX ROWID| FILE$ | 1 | 11 | 1 (0)| 00:00:01 |
  
|* 25 | INDEX UNIQUE SCAN | I_FILE1 | 1 | | 0 (0)| 00:00:01 |
  
|* 26 | FIXED TABLE FIXED INDEX | X$KCCFE (ind:1) | 1 | 13 | 0 (0)| 00:00:01 |
  
| 27 | TABLE ACCESS CLUSTER | TS$ | 1 | 16 | 1 (0)| 00:00:01 |
  
|* 28 | INDEX UNIQUE SCAN | I_TS# | 1 | | 0 (0)| 00:00:01 |
  
| 29 | VIEW | | 51 | 1530 | 4377 (5)| 00:00:53 |
  
| 30 | HASH GROUP BY | | 51 | 1122 | 4377 (5)| 00:00:53 |
  
| 31 | VIEW | DBA_FREE_SPACE | 18911 | 406K| 4376 (5)| 00:00:53 |
  
| 32 | UNION-ALL | | | | | |
  
| 33 | NESTED LOOPS | | 1 | 66 | 4 (0)| 00:00:01 |
  
| 34 | NESTED LOOPS | | 1 | 59 | 4 (0)| 00:00:01 |
  
| 35 | TABLE ACCESS FULL | FET$ | 1 | 39 | 4 (0)| 00:00:01 |
  
|* 36 | TABLE ACCESS CLUSTER | TS$ | 1 | 20 | 0 (0)| 00:00:01 |
  
|* 37 | INDEX UNIQUE SCAN | I_TS# | 1 | | 0 (0)| 00:00:01 |
  
|* 38 | INDEX UNIQUE SCAN | I_FILE2 | 1 | 7 | 0 (0)| 00:00:01 |
  
| 39 | NESTED LOOPS | | 67 | 4824 | 22 (0)| 00:00:01 |
  
| 40 | NESTED LOOPS | | 67 | 4355 | 22 (0)| 00:00:01 |
  
|* 41 | TABLE ACCESS FULL | TS$ | 51 | 1326 | 22 (0)| 00:00:01 |
  
|* 42 | FIXED TABLE FIXED INDEX | X$KTFBFE (ind:1) | 1 | 39 | 0 (0)| 00:00:01 |
  
|* 43 | INDEX UNIQUE SCAN | I_FILE2 | 1 | 7 | 0 (0)| 00:00:01 |
  
|* 44 | HASH JOIN | | 18842 | 1214K| 3127 (6)| 00:00:38 |
  
|* 45 | TABLE ACCESS FULL | TS$ | 51 | 1326 | 22 (0)| 00:00:01 |
  
|* 46 | HASH JOIN | | 18985 | 741K| 3105 (6)| 00:00:38 |
  
| 47 | INDEX FULL SCAN | I_FILE2 | 146 | 1022 | 1 (0)| 00:00:01 |
  
| 48 | NESTED LOOPS | | 887K| 27M| 3100 (6)| 00:00:38 |
  
| 49 | TABLE ACCESS FULL | RECYCLEBIN$ | 887K| 10M| 2940 (1)| 00:00:36 |
  
|* 50 | FIXED TABLE FIXED INDEX | X$KTFBUE (ind:1) | 1 | 21 | 0 (0)| 00:00:01 |
  
| 51 | NESTED LOOPS | | 1 | 91 | 1223 (1)| 00:00:15 |
  
| 52 | NESTED LOOPS | | 49300 | 91 | 1223 (1)| 00:00:15 |
  
| 53 | NESTED LOOPS | | 1 | 79 | 23 (0)| 00:00:01 |
  
| 54 | NESTED LOOPS | | 1 | 72 | 23 (0)| 00:00:01 |
  
| 55 | TABLE ACCESS FULL | UET$ | 1 | 52 | 23 (0)| 00:00:01 |
  
|* 56 | TABLE ACCESS CLUSTER | TS$ | 1 | 20 | 0 (0)| 00:00:01 |
  
|* 57 | INDEX UNIQUE SCAN | I_TS# | 1 | | 0 (0)| 00:00:01 |
  
|* 58 | INDEX UNIQUE SCAN | I_FILE2 | 1 | 7 | 0 (0)| 00:00:01 |
  
|* 59 | INDEX RANGE SCAN | RECYCLEBIN$_TS | 49300 | | 336 (0)| 00:00:05 |
  
|* 60 | TABLE ACCESS BY INDEX ROWID | RECYCLEBIN$ | 2 | 24 | 1200 (1)| 00:00:15 |
  
|* 61 | HASH JOIN OUTER | | 1 | 73 | 3 (67)| 00:00:01 |
  
| 62 | VIEW | | 1 | 43 | 2 (50)| 00:00:01 |
  
| 63 | HASH GROUP BY | | 1 | 417 | 2 (50)| 00:00:01 |
  
| 64 | NESTED LOOPS | | 1 | 417 | 1 (0)| 00:00:01 |
  
| 65 | NESTED LOOPS | | 1 | 365 | 1 (0)| 00:00:01 |
  
| 66 | NESTED LOOPS | | 1 | 349 | 0 (0)| 00:00:01 |
  
|* 67 | FIXED TABLE FULL | X$KCCFN | 1 | 297 | 0 (0)| 00:00:01 |
  
|* 68 | FIXED TABLE FIXED INDEX | X$KTFTHC (ind:1) | 1 | 52 | 0 (0)| 00:00:01 |
  
| 69 | TABLE ACCESS CLUSTER | TS$ | 1 | 16 | 1 (0)| 00:00:01 |
  
|* 70 | INDEX UNIQUE SCAN | I_TS# | 1 | | 0 (0)| 00:00:01 |
  
|* 71 | FIXED TABLE FIXED INDEX | X$KCCTF (ind:1) | 1 | 52 | 0 (0)| 00:00:01 |
  
| 72 | VIEW | | 1 | 30 | 1 (100)| 00:00:01 |
  
| 73 | HASH GROUP BY | | 1 | 172 | 1 (100)| 00:00:01 |
  
| 74 | NESTED LOOPS | | 1 | 172 | 0 (0)| 00:00:01 |
  
| 75 | NESTED LOOPS | | 1 | 159 | 0 (0)| 00:00:01 |
  
| 76 | MERGE JOIN CARTESIAN | | 100 | 8800 | 0 (0)| 00:00:01 |
  
|* 77 | FIXED TABLE FULL | X$KTSSO | 1 | 62 | 0 (0)| 00:00:01 |
  
| 78 | BUFFER SORT | | 100 | 2600 | 0 (0)| 00:00:01 |
  
| 79 | FIXED TABLE FULL | X$KSLWT | 100 | 2600 | 0 (0)| 00:00:01 |
  
|* 80 | FIXED TABLE FIXED INDEX | X$KSUSE (ind:1) | 1 | 71 | 0 (0)| 00:00:01 |
  
|* 81 | FIXED TABLE FIXED INDEX | X$KSLED (ind:2) | 1 | 13 | 0 (0)| 00:00:01 |
  
-----------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
      4 - access("D"."TABLESPACE_NAME"="F"."TABLESPACE_NAME"(+))
  
    12 - filter("FNNAM" IS NOT NULL AND "FNTYP"=4 AND "INST_ID"=USERENV('INSTANCE') AND
  
                            BITAND("FNFLG",4)<>4)
  
    13 - filter("F"."SPARE1" IS NULL)
  
    14 - access("FNFNO"="F"."FILE#")
  
    15 - filter("FE"."FENUM"="F"."FILE#")
  
    17 - access("F"."TS#"="TS"."TS#")
  
    22 - filter("FNNAM" IS NOT NULL AND "FNTYP"=4 AND "INST_ID"=USERENV('INSTANCE') AND
  
                            BITAND("FNFLG",4)<>4)
  
    23 - filter("FNFNO"="HC"."KTFBHCAFNO")
  
    24 - filter("F"."SPARE1" IS NOT NULL)
  
    25 - access("FNFNO"="F"."FILE#")
  
    26 - filter("FE"."FENUM"="F"."FILE#")
  
    28 - access("HC"."KTFBHCTSN"="TS"."TS#")
  
    36 - filter("TS"."BITMAPPED"=0)
  
    37 - access("TS"."TS#"="F"."TS#")
  
    38 - access("F"."TS#"="FI"."TS#" AND "F"."FILE#"="FI"."RELFILE#")
  
    41 - filter("TS"."CONTENTS$"=0 AND "TS"."BITMAPPED"<>0 AND ("TS"."ONLINE$"=1 OR
  
                            "TS"."ONLINE$"=4))
  
    42 - filter("TS"."TS#"="F"."KTFBFETSN")
  
    43 - access("F"."KTFBFETSN"="FI"."TS#" AND "F"."KTFBFEFNO"="FI"."RELFILE#")
  
    44 - access("TS"."TS#"="RB"."TS#")
  
    45 - filter("TS"."CONTENTS$"=0 AND "TS"."BITMAPPED"<>0 AND ("TS"."ONLINE$"=1 OR
  
                            "TS"."ONLINE$"=4))
  
    46 - access("RB"."TS#"="FI"."TS#" AND "U"."KTFBUEFNO"="FI"."RELFILE#")
  
    50 - filter("U"."KTFBUESEGTSN"="RB"."TS#" AND "U"."KTFBUESEGFNO"="RB"."FILE#" AND
  
                            "U"."KTFBUESEGBNO"="RB"."BLOCK#")
  
    56 - filter("TS"."BITMAPPED"=0)
  
    57 - access("TS"."TS#"="U"."TS#")
  
    58 - access("U"."TS#"="FI"."TS#" AND "U"."SEGFILE#"="FI"."RELFILE#")
  
    59 - access("U"."TS#"="RB"."TS#")
  
    60 - filter("U"."SEGFILE#"="RB"."FILE#" AND "U"."SEGBLOCK#"="RB"."BLOCK#")
  
    61 - access("D"."TABLESPACE_NAME"="F"."TABLESPACE"(+))
  
    67 - filter("V"."FNNAM" IS NOT NULL AND "V"."FNTYP"=7)
  
    68 - filter("V"."FNFNO"="HC"."KTFTHCTFNO")
  
    70 - access("HC"."KTFTHCTSN"="TS"."TS#")
  
    71 - filter("TF"."TFDUP"<>0 AND BITAND("TF"."TFSTA",32)<>32 AND "V"."FNFNO"="TF"."TFNUM" AND
  
                            "TF"."TFFNH"="V"."FNNUM")
  
    77 - filter("X$KTSSO"."INST_ID"=USERENV('INSTANCE'))
  
    80 - filter("S"."INST_ID"=USERENV('INSTANCE') AND BITAND("S"."KSSPAFLG",1)<>0 AND
  
                            BITAND("S"."KSUSEFLG",1)<>0 AND "KTSSOSES"="S"."ADDR" AND "KTSSOSNO"="S"."KSUSESER" AND
  
                            "S"."INDX"="W"."KSLWTSID")
  
    81 - filter("W"."KSLWTEVT"="E"."INDX")

分析执行计划发现DBA_FREE_SPACE消耗了大量资源,DBA_FREE_SPACE是个视图,通过查看其创建视图的语句发现该视图关联了RECYCLEBIN$表。

RECYCLEBIN$是Oracle回收站,用来存放DROP删除的对象信息,查询发现该表有八十多万条记录,因此判断是回收站太大引起了表空间查询缓慢,建议对回收站进行定期清理。

1
purge dba_recylebin;