Oracle 降低高水位线的方法

Oracle  降低高水位线的方法 




高水位(HIGH WARTER MARK,HWM)好比水库中储水的水位,用于描述数据库中段的扩展方式。高水位对全表扫描方式有着至关重要的影响。当使用DELETE删除表记录时,高水位并不会下降,随之导致的是全表扫描的实际开销并没有任何减少。

例如,首先新建一张空表,大小占用64K,然后插入数据直到表大小变为50G,此时使用DELETE删除所有的数据并且提交,这个时候查询表的大小的时候依然是50G,这就是因为表的高水位没有释放的缘故,而在这时如果使用“SELECT * FROM TABLE_NAME;”语句来查询数据的话,那么查询过程就会很慢,因为Oracle要执行全表扫描,从高水位下所有的块都得去扫描,直到50G的所有块全部扫描完毕。曾遇到一个同事使用DELETE删除了一个很大的分区表,然后执行SELECT查询很久都没有结果,以为是数据库HANG住了,其实这个问题是由于高水位的缘故。所以,表执行了TRUNCATE操作,再次SELECT的时候就可以很快返回结果了。

释放表的高水位通常有如下几种办法:

(1)对表进行MOVE操作:ALTER TABLE TABLE_NAME MOVE;。若表上存在索引,则记得重建索引。

(2)对表进行SHRINK SPACE操作:ALTER TABLE TABLE_NAME SHRINK SPACE;,注意,在执行该指令之前必须开启行移动:ALTER TABLE TABLE_NAME ENABLE ROW MOVEMENT;。该方法的优点是:在碎片整理结束后,表上相关的索引仍然有效,缺点是会产生大量的UNDO和REDO。

(3)复制要保留的数据到临时表T,DROP原表,然后RENAME临时表T为原表。

(4)exp/imp或expdp/impdp重构表。

(5)若表中没有数据则直接使用TRUNCATE来释放高水位。

如何找出系统中哪些表拥有高水位呢?这里给出两种办法,①比较表的行数和表的大小关系。如果行数为0,而表的当前占用大小减去初始化时的大小(INITIAL_EXTENT)后依然很大,那么说明该表有高水位。②行数和块数的比率,即查看一个块可以存储多少行数据。如果一个块存储的行数少于5行甚至更少,那么说明有高水位。注意,这两种方法都不是十分准确,需要再对查询结果进行筛选。需要注意的是,在查询表的高水位时,首先需要分析表,以得到最准确的统计信息。

下面给出用于查询高水位的几个SQL语句:

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%' ;

或:

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;

最后再次提醒各位读者,若表执行了大量的DELETE操作后,则最好回收一下表的高水位。



http://docs.oracle.com/cd/E11882_01/server.112/e40540/logical.htm#CNCPT89022

Segment Space and the High Water Mark

To manage space, Oracle Database tracks the state of blocks in the segment. The high water mark (HWM) is the point in a segment beyond which data blocks are unformatted and have never been used.

MSSM uses free lists to manage segment space. At table creation, no blocks in the segment are formatted. When a session first inserts rows into the table, the database searches the free list for usable blocks. If the database finds no usable blocks, then it preformats a group of blocks, places them on the free list, and begins inserting data into the blocks. In MSSM, a full table scan reads allblocks below the HWM.

ASSM does not use free lists and so must manage space differently. When a session first inserts data into a table, the database formats a single bitmap block instead of preformatting a group of blocks as in MSSM. The bitmap tracks the state of blocks in the segment, taking the place of the free list. The database uses the bitmap to find free blocks and then formats each block before filling it with data. ASSM spread out inserts among blocks to avoid concurrency issues.

Every data block in an ASSM segment is in one of the following states:

  • Above the HWM

    These blocks are unformatted and have never been used.

  • Below the HWM

    These blocks are in one of the following states:

    • Allocated, but currently unformatted and unused
    • Formatted and contain data
    • Formatted and empty because the data was deleted

Figure 12-23 depicts an ASSM segment as a horizontal series of blocks. At table creation, the HWM is at the beginning of the segment on the left. Because no data has been inserted yet, all blocks in the segment are unformatted and never used.

Figure 12-23 HWM at Table Creation


Description of "Figure 12-23 HWM at Table Creation"


段空间和高水位标记


oracle数据库通过跟踪段中的块状态来管理空间。高水位标记(HWM)是段中的一个点,超过该点的数据块是未格式化和未使用过的。


MSSM使用空闲列表来管理段空间。在创建表时,段中的块并未被格式化。当一个会话初次向表中插入行时,数据库将搜索空闲列表来查找可用的块。如果数据库未找到可用的块,那么它会预格式化一组块,并将它们放置在空闲列表中,并开始将数据插入到块中。在MSSM中,全表扫描会读取HWM之下的所有块。


ASSM不使用空闲列表,所以必须以不同的方式管理空间。当会话初次向表中插入数据时,数据库只格式化一个单一位图块,而不像在MSSM中那样预格式化一组块。位图取代了空闲列表,用于跟踪在段中的块的状态。数据库使用位图来查找可用的块,然后在往块写入数据之前将其格式化。ASSM将插入操作分散到多个块,以避免并发问题。


在一个ASSM段中的每个数据块处于以下状态之一:


l 在HWM之上

    这些块是未格式化的,且从未使用过。

l 在HWM之下

这些块处于以下状态之一:

u 已分配,但当前未格式化且未使用

u 已格式化且包含数据

u 已格式化且为空,因为数据已被删除


图12-23将一个ASSM段描述为一系列水平的块。在创建表时,HWM在左侧段的开头。因为还未插入数据,段中的所有块都还是未格式化且从未使用过。


图将12-23在创建表时的HWM

Suppose that a transaction inserts rows into the segment. The database must allocate a group of blocks to hold the rows. The allocated blocks fall below the HWM. The database formats a bitmap block in this group to hold the metadata, but does not preformat the remaining blocks in the group.

In Figure 12-24, the blocks below the HWM are allocated, whereas blocks above the HWM are neither allocated or formatted. As inserts occur, the database can write to any block with available space. The low high water mark (low HWM) marks the point below which all blocks are known to be formatted because they either contain data or formerly contained data.

Figure 12-24 HWM and Low HWM


Description of "Figure 12-24 HWM and Low HWM"

In Figure 12-25, the database chooses a block between the HWM and low HWM and writes to it. The database could have just as easily chosen any other block between the HWM and low HWM, or any block below the low HWM that had available space. In Figure 12-25, the blocks to either side of the newly filled block are unformatted.

Figure 12-25 HWM and Low HWM


Description of "Figure 12-25 HWM and Low HWM"

The low HWM is important in a full table scan. Because blocks below the HWM are formatted only when used, some blocks could be unformatted, as in Figure 12-25. For this reason, the database reads the bitmap block to obtain the location of the low HWM. The database reads all blocks up to the low HWM because they are known to be formatted, and then carefully reads only the formatted blocks between the low HWM and the HWM.

Assume that a new transaction inserts rows into the table, but the bitmap indicates that insufficient free space exists under the HWM. In Figure 12-26, the database advances the HWM to the right, allocating a new group of unformatted blocks.

Figure 12-26 Advancing HWM and Low HWM


Description of "Figure 12-26 Advancing HWM and Low HWM"

When the blocks between the HWM and low HWM are full, the HWM advances to the right and the low HWM advances to the location of the old HWM. As the database inserts data over time, the HWM continues to advance to the right, with the low HWM always trailing behind it. Unless you manually rebuild, truncate, or shrink the object, the HWM never retreats.

  •  
  •    


  
   
     
   

    
     
      
  
  

    
   

>
  
  
  
  
  
  


                    

                                   

>

>

>

>

                    

                                     

>  
  
  
  
  
  
  

>

>
                    

                                   

>

>

>

>

>

>
                    

                                     

>
  
  
  
  
  
  

>

>
                    

                                   

>

>

>
                    

                                     

>
  
  
  
  
  
  

>

>

>
                    

                                   

>

>

>

>

>
                    

                                     

>
  
  
  
  
  
  


                              

                                          

>

>

>
                              

                                         

>

>
                              

                                          

>

>

>
                              

                                          

    

    

    

  

 

  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.    

    

  

 

  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.              

  

 

  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.                

    

  

 

  1. >        
  2.   
  3.              
  4.     
  5.                              

    
      
    

  

 

  1. >      
  2.   
  3.   
  4.   
  5.                           
  6.   
  7. >      
  8.   
  9.   
  10.   
  11.                                                
  12.   
  13. >      
  14.   
  15.   
  16.   
  17.                                               

    
    
    
    
     
     
     
    
   

  

 

  1.      
  2.                            

    
    
    
    

    

    
    
    
    
     
    
    
    
    
    
    
    
    
      
    
    
    
    
    
    

  

 

  1. >        
  2.   
  3.     
  4.    
  5.                  

    
    
    

  

 

  1. >      
  2.   
  3.   
  4.   
  5.        

    
    
    
    
    
    
    
    
    
    
    
    
    
   
    

    
    
    
    >
    
    
    
     
    
    
     
     
    
      
      
      
     
    

    



 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
 
 
 
 

 

 

 

 

   

   
       
   
   

>

>

>

 

 

 

      

      

      

    

    

      

      

      

      

 
 

 


>
>

 

 

 

 

 

 

 

 

 

 
 

 

 

 
 
       
       
 
 
       
     
     
       
       
       
      

 

 
 

 

 

>

  

 

 
   

 

 

 

 

 

 

>

 

 

 

 

 

 

>

 

 

 

 

 
 
 
 
 
 
 
 

 

 

 

 

 

 

 
 

 

 
 
      
     

      
      
      
    
    
      
      
      
      

 

 

 
 
 

 

 

 

 

 
 
 
 
 
 
 

 

 

 

 

 

 

 

 

 

 

 

 
 
 
 
 
 

 

 

 

 

 

 

 

 

 

 

 

 

>

 

 
 

 

 

 

 

        

 

 

 

 

   
   

   
   
   

 

 

 

>

 

>

 

 

   

 

>

 

>

 

 

>

 

 

>

 

 

 

 

 

><

 

 

>

 

 

 

 

 

 

>

 

>

 

 

 

 

 

 

 
 
 

   
 
 

     
    
   
                                                                                  
 
                                                                                                           
                                                                                                           
 

 
 

     
    
      
 
                             
 

   
 

 
 

 
 

     
    
   
                                                                                  
 
                                                                                                           
                                                                                                        
 
 

     
    
      
 
                      
 
 

 

 

 


     
    
      
 
                          
 
 

 

 


     
    
      
 
                             

   

  

 

  

 

       

        

                   

                  

        

          

          

      

            

                 

    

 

   

            

   >          

      

                

            

               

              

 

  

         

         

 

 

 

  

 

     

 

                                                                

 

       

        

    

 

   

               

 

 

    

       

 

          




           

               

<>

<>

            

>

              

    

                           

             

                

>

           

>

      

     

              

             

>

             

>

          

>

 

>

 

>

     

     

                 

 

>

                        

>

                   

>

                   

>

>

>

     

    

                   

                               

>

  

 

             

                

              

                

             

                 

        



    

时间: 2024-09-20 05:36:09

Oracle 降低高水位线的方法的相关文章

INITIAL参数设置导致TRUNCATE TABLE不能降低高水位线案例

在一个数据库使用下面SQL找出了一批需要降低高水位线的表,其中有几个表没有数据,于是我打算用TRUNCATE来降低高水位线HWM SELECT a.owner,        a.segment_name,        a.segment_type,        a.tablespace_name,        a.blocks              "real block",        a.bytes / 1024 / 1024 "realSizeMB&quo

关于降低高水位线的尝试

在前一段时间,生产环境中有几个很大的分区表,由于存在太多的碎片,导致表里的数据就几十条,但是查询的时候特别慢.很明显是高水位线导致的问题. 一般来说这类问题,使用备份->truncate->insert的方式比较保守,不适用于在线操作. 而在10g开始的一个新特性shrink算是一个比较理想的方案,按照新特性的预期,速度也是很快的,而且是在线操作.可以分批释放表中的冗余空间. 所以做了一个尝试,在生产系统中使用这个新特性来降低高水位线. 生产中的表pub_log,sub_log都是分区表,分区

Oracle的高水位线HWM简介

高水位是记录段里能容纳数据的上限,高水位存在段里 全表扫先读段头块,而后在段头块里面找到HWM 下面用实验由内而外来理解Oracle的HWM --t表有一条数据 hr@ORCL> select * from t; ID NAME ---------- ---------- 1 AAAAA --找t段的段头块 hr@ORCL> select header_file,header_block from dba_segments where segment_name='T' and owner='H

高水位线和全表扫描

   高水位线好比水库中储水的水位线,用于描述数据库中段的扩展方式.高水位线对全表扫描方式有着至关重要的影响.当使用delete 操作 表记录时,高水位线并不会下降,随之导致的是全表扫描的实际开销并没有任何减少.本文给出高水位线的描述,如何降低高水位线,以及高水 位线对全表扫描的影响.   一.何谓高水位线    如前所述,类似于水库中储水的水位线.只不过在数据库中用于描述段的扩展方式.     可以将数据段或索引段等想象为一个从左到右依次排开的一系列块.当这些块中未填充任何数据时,高水位线位于

oracle点知识——HWM(高水位线) 下

1.何时应该降低 HWM table中包含两种空闲的block,在HWM之上的空闲block 和 在HWM之下的空闲block. 1.在HWM之上的空闲block : 运行analyze table后,在HWM之上的空心啊block会在user_tables 的 empty_blocks中 被统计,这些空闲的blocks实际上是从来没有存储过数据的,可以用以下命令来释放这些空间: SQL>  alter table table_name  deallocate unused; 下面做一个实验来验

oracle点知识 ——HWM(高水位线) 上

在Oracle数据的存储中,可以把存储空间想象为一个水库,数据想象为水库中的水.水库中的水的位置有一条线叫做水位线,在Oracle中,这条线被称为高水位线(High-warter mark, HWM).在数据库表刚建立的时候,由于没有任何数据,所以这个时候水位线是空的,也就是说HWM为最低值.当插入了数据以后,高水位线就会上涨,但是这里也有一个特性,就是如果你采用delete语句删除数据的话,数据虽然被删除了,但是高水位线却没有降低,还是你刚才删除数据以前那么高的水位.也就是说,这条高水位线在日

降低SQLSET相关表WRI$_SQLSET_PLAN_LINES高水位线,释放SYSAUX表空间

客户提出SYSAUX空间太大,已经占据了20多G的空间,登陆系统发觉SYSAUX表空间中占据TOP SEGMENT的主要就是WRI$_SQLSET_PLAN_LINES表 SQL> select *   2    from (select bytes / 1024 / 1024 / 1024, segment_name, owner, segment_type   3            from dba_segments   4           where tablespace_name

高水位线条款:中国基金激励的现实选择

在美国对冲基金中广泛应用的高水位线条款,如果能以适当的方式引入到我国开放式基金中,对促进我国开放式基金的发展或将起到很好的推动作用 文/李曜 史丹丹 在不到十年的时间里,美国的对冲基金从1000家迅速增长到7000家,资产规模在2007年时超过1.5万亿美元.为什么对冲基金如此成功? 西方学术界发现,对冲基金与公募基金的重要区别之一就是高水位线条款(high water marks, 以下简称HWM),这是以美国为代表的西方私募资产管理业,特别是对冲基金业管理费设计的一个关键技术条款.HWM条款

oracle存储过程返回数组的方法

oracle 存储过程返回数组的方法: 1.建立包 create or replace package test isTYPE filename_array IS TABLE OF varchar2(1);filename filename_array;end test; 2. 建立存储过程 create or replace procedure test_array(v_cfjg out test.filename_array ) isbegin DECLARE i number;D_cfjg