当前位置:数据库 > Oracle >>

回收站引发ORACLE查询表空间使用缓慢问题

回收站引发ORACLE查询表空间使用缓慢问题
 
一个哥们问我 ,他们查询 表空间使用率 跑了一个多小时,这个太坑爹了,让我 帮忙优化一下。
SQL语句如下
[html] 
select * from   
    (  
      select ts.tablespace_name,ts.contents "TABLESPACE_TYPE",  
             to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') monitor_date,  
             datafiles,total_gb  "TOTAL_SIZE_GB",  
            (total_gb-free_gb) "USED_SIZE_GB",  
             free_gb "FREE_SIZE_GB",  
             round((100-free_gb/total_gb*100),2) "USED_PCT",  
             round(free_gb/total_gb*100,2) "FREE_PCT"   
      from  dba_tablespaces ts,  
            (select tablespace_name,round(sum(bytes)/1024/1024/1024,2) free_gb   
             from dba_free_space group by tablespace_name) fr1,  
            (select tablespace_name,round(sum(bytes)/1024/1024/1024,2) total_gb,count(*) datafiles    
             from dba_data_files group by tablespace_name) df1   
      where ts.tablespace_name=fr1.tablespace_name and ts.tablespace_name=df1.tablespace_name order by "FREE_PCT"  
    )   
union all  
select 'TOTAL SUMMARY:','ALL',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') monitor_date,datafiles,  
       total_gb "TOTAL_SIZE_GB",  
       round(total_gb-free_gb,2) "USED_SIZE_GB",  
       free_gb "FREE_SIZE_GB",  
       round((100-free_gb/total_gb*100),2) "USED_PCT",  
       round(free_gb/total_gb*100,2) "FREE_PCT"   
from  (select round(sum(bytes)/1024/1024/1024,2) free_gb from dba_free_space) fr2,  
      (select count(*) datafiles,round(sum(bytes)/1024/1024/1024,2) total_gb from dba_data_files) df2  
union all  
select null,null,null,null,null,null,null,null,null from dual;  

 

 
这个SQL挺简单的, 就是访问几个数据字典而已,执行计划如下
 
[html] 
SQL> set autotrace traceonly;  
SQL>       select * from   
  2            (  
  3              select ts.tablespace_name,ts.contents "TABLESPACE_TYPE",  
  4                     to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') monitor_date,  
  5                     datafiles,total_gb  "TOTAL_SIZE_GB",  
  6                    (total_gb-free_gb) "USED_SIZE_GB",  
  7                     free_gb "FREE_SIZE_GB",  
  8                     round((100-free_gb/total_gb*100),2) "USED_PCT",  
  9                     round(free_gb/total_gb*100,2) "FREE_PCT"   
            from  dba_tablespaces ts,  
 10   11                    (select tablespace_name,round(sum(bytes)/1024/1024/1024,2) free_gb   
 12                     from dba_free_space group by tablespace_name) fr1,  
 13                    (select tablespace_name,round(sum(bytes)/1024/1024/1024,2) total_gb,count(*) datafiles    
 14                     from dba_data_files group by tablespace_name) df1   
 15              where ts.tablespace_name=fr1.tablespace_name and ts.tablespace_name=df1.tablespace_name order by "FREE_PCT"  
 16            )   
 17        union all  
 18        select 'TOTAL SUMMARY:','ALL',to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') monitor_date,datafiles,  
 19               total_gb "TOTAL_SIZE_GB",  
 20               round(total_gb-free_gb,2) "USED_SIZE_GB",  
 21               free_gb "FREE_SIZE_GB",  
 22               round((100-free_gb/total_gb*100),2) "USED_PCT",  
 23               round(free_gb/total_gb*100,2) "FREE_PCT"   
 24        from  (select round(sum(bytes)/1024/1024/1024,2) free_gb from dba_free_space) fr2,  
 25              (select count(*) datafiles,round(sum(bytes)/1024/1024/1024,2) total_gb from dba_data_files) df2 
 26        union all  
 27        select null,null,null,null,null,null,null,null,null from dual;  
  
  
  
  
31 rows selected.  
  
Elapsed: 00:50:32.18  
  
Execution Plan  
----------------------------------------------------------  
Plan hash value: 3463738489  
  
--------------------------------------------------------------------------------------------------------------------  
| Id  | Operation                               | Name             | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |  
--------------------------------------------------------------------------------------------------------------------  
|   0 | SELECT STATEMENT                        |                  |   140 | 15495 |       |  5104K(100)| 17:00:53 |  
|   1 |  UNION-ALL                              |                  |       |       |       |            |          |  
|   2 |   VIEW                                  |                  |   138 | 15456 |       |  2552K(100)| 08:30:27 |  
|   3 |    SORT ORDER BY                        |                  |   138 | 13248 |       |  2552K(100)| 08:30:27 |  
|*  4 |     HASH JOIN                           |                  |   138 | 13248 |       |  2552K(100)| 08:30:27 |  
|   5 |      NESTED LOOPS                       |                  |     2 |   132 |       |     7  (15)| 00:00:01 |  
|   6 |       VIEW                              |                  |     2 |    86 |       |     5  (20)| 00:00:01 |  
|   7 |        HASH GROUP BY                    |                  |     2 |    60 |       |     5  (20)| 00:00:01 |  
|   8 |         VIEW                            | DBA_DATA_FILES   |     2 |    60 |       |     4   (0)| 00:00:01 |  
|   9 |          UNION-ALL                      |                  |       |       |       |            |          |  
|  10 |           NESTED LOOPS                  |                  |     1 |   236 |       |     2   (0)| 00:00:01 |  
|  11 |            NESTED LOOPS                 |                  |     1 |   215 |       |     1   (0)| 00:00:01 |  
|  12 |             NESTED LOOPS                |                  |     1 |   202 |       |     1   (0)| 00:00:01 |  
|* 13 |              FIXED TABLE FULL           | X$KCCFN          |     1 |   182 |       |     0   (0)| 00:00:01 |  
|* 14 |              TABLE ACCESS BY INDEX ROWID| FILE$            |     1 |    20 |       |     1   (0)| 00:00:01 |  
|* 15 |               INDEX UNIQUE SCAN         | I_FILE1          |     1 |       |       |     0   (0)| 00:00:01 |  
|* 16 |             FIXED TABLE FIXED INDEX     | X$KCCFE (ind:1)  |     1 |    13 |       |     0   (0)| 00:00:01 |  
|  17 |            TABLE ACCESS CLUSTER         | TS$              |     1 |    21 |       |     1   (0)| 00:00:01 |  
|* 18 |             INDEX UNIQUE SCAN           | I_TS#            |     1 |       |       |     0   (0)| 00:00:01 |  
|  19 |           NESTED LOOPS                  |                  |     1 |   279 |       |     2   (0)| 00:00:01 |  
|  20 |            NESTED LOOPS                 |                  |     1 |   258 |       |     1   (0)| 00:00:01 |  
|  21 |             NESTED LOOPS                |                  |     1 |   245 |       |     1   (0)| 00:00:01 |  
|  22 |              NESTED LOOPS               |                  |     1 |   234 |       |     0   (0)| 00:00:01 |  
|* 23 |               FIXED TABLE FULL          | X$KCCFN          |     1 |   182 |       |     0   (0)| 00:00:01 |  
|* 24 |               FIXED TABLE FIXED INDEX   | X$KTFBHC (ind:1) |     1 |    52 |       |     0   (0)| 00:00:01 |  
|* 25 |              TABLE ACCESS BY INDEX ROWID| FILE$            |     1 |    11 |       |     1   (0)| 00:00:01 |  
|* 26 |               INDEX UNIQUE SCAN         | I_FILE1          |     1 |       |       |     0   (0)| 00:00:01 |  
|* 27 |             FIXED TABLE FIXED INDEX     | X$KCCFE (ind:1)  |     1 |    13 |       |     0   (0)| 00:00:01 |  
|  28 |            TABLE ACCESS CLUSTER         | TS$              |     1 |    21 |       |     1   (0)
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,