Oracle高水位线(HWM)及性能优化

2020-06-02  本文已影响0人  baishangbing
ORACLE在逻辑存储上分4个粒度:表空间,段,区和块.
确定碎片程度
  SELECT table_name,
         ROUND ( (blocks * 8), 2) "高水位空间 k",
         ROUND ( (num_rows * avg_row_len / 1024), 2) "真实使用空间 k",
         ROUND ( (blocks * 10 / 100) * 8, 2) "预留空间(pctfree) k",
         ROUND (
            (  blocks * 8
             - (num_rows * avg_row_len / 1024)
             - blocks * 8 * 10 / 100),
            2)
            "浪费空间 k"
    FROM user_tables
   WHERE temporary = 'N'
ORDER BY 5 DESC;
查看表上次收集统计信息时间
 select table_name,last_analyzed from dba_tables where owner = 'SCHEMA_NAME';
SQL> alter table TABLE_NAME enable ROW MOVEMENT;--启动行移动功能
SQL> alter table TABLE_NAME shrink space compact;  --只整理碎片 不回收空间
SQL> alter table TABLE_NAME disable ROW MOVEMENT;--关闭行移动

如果要同时压缩表的索引,可以发布:ALTER TABLE TEST_TAB SHRINK SPACE CASCADE

SQL> alter table TABLE_NAME enable ROW MOVEMENT;--启动行移动功能
SQL> alter table TABLE_NAME shrink space; --整理碎片并回收空间,并调整水位线。业务少时执行
SQL> alter table TABLE_NAME disable ROW MOVEMENT;--关闭行移动
释放表的高水位通常有如下几种办法:
如何找出系统中哪些表拥有高水位呢?
SELECT OWNER,

       SEGMENT_NAME TABLE_NAME,

       SEGMENT_TYPE,

       GREATEST(ROUND(100 * (NVL(HWM - AVG_USED_BLOCKS, 0) /

                      GREATEST(NVL(HWM, 1), 1)),

                      2),

                0) WASTE_PER

  FROM (SELECT A.OWNER OWNER,

               A.SEGMENT_NAME,

               A.SEGMENT_TYPE,

               B.LAST_ANALYZED,

               A.BYTES,

               B.NUM_ROWS,

               A.BLOCKS BLOCKS,

               B.EMPTY_BLOCKS EMPTY_BLOCKS,

               A.BLOCKS - B.EMPTY_BLOCKS - 1 HWM,

               DECODE(ROUND((B.AVG_ROW_LEN * NUM_ROWS *

                            (1 + (PCT_FREE / 100))) / C.BLOCKSIZE,

                            0),

                      0,

                      1,

                      ROUND((B.AVG_ROW_LEN * NUM_ROWS *

                            (1 + (PCT_FREE / 100))) / C.BLOCKSIZE,

                            0)) + 2 AVG_USED_BLOCKS,

               ROUND(100 *

                     (NVL(B.CHAIN_CNT, 0) / GREATEST(NVL(B.NUM_ROWS, 1), 1)),

                     2) CHAIN_PER,

               B.TABLESPACE_NAME O_TABLESPACE_NAME

          FROM SYS.DBA_SEGMENTS A, SYS.DBA_TABLES B, SYS.TS$ C

         WHERE A.OWNER = B.OWNER

           AND SEGMENT_NAME = TABLE_NAME

           AND SEGMENT_TYPE = 'TABLE'

           AND B.TABLESPACE_NAME = C.NAME)

 WHERE GREATEST(ROUND(100 * (NVL(HWM - AVG_USED_BLOCKS, 0) /

                      GREATEST(NVL(HWM, 1), 1)),

                      2),

                0) > 50

   AND OWNER NOT LIKE '%SYS%'

   AND BLOCKS > 100

 ORDER BY WASTE_PER DESC;
SELECT D.OWNER,

       ROUND(D.NUM_ROWS / D.BLOCKS, 2),

       D.NUM_ROWS,

       D.BLOCKS,

       D.TABLE_NAME,

 ROUND((d.BLOCKS*8-D.INITIAL_EXTENT/1024)/1024)  t_size

  FROM DBA_TABLES D

 WHERE D.BLOCKS > 10

   AND ROUND(D.NUM_ROWS / D.BLOCKS, 2) < 5

 AND d.OWNER NOT LIKE '%SYS%' ;
上一篇 下一篇

猜你喜欢

热点阅读