暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle数据库碎片的整理方法

鑫海方圆 2021-07-13
1457


碎片计算


由于自由空间碎片是由几部分组成,如范围数量、最大范围尺寸等,我们可用 FSFI--Free Space Fragmentation Index (自由空间碎片索引)值来直观体现:
    FSFI=100*SQRT(max(extent)/sum(extents))*1/SQRT(SQRT(count(extents)))


可以看出, FSFI 的最大可能值为 100 (一个理想的单文件表空间)。随着范围的增加, FSFI 值缓慢下降,而随着最大范围尺寸的减少, FSFI 值会迅速下降。


下面的脚本可以用来计算 FSFI 值:

        rem FSFI Value Compute
        rem fsfi.sql
        column FSFI format 999,99
        select tablespace_name,sqrt(max(blocks)/sum(blocks))*
        (100/sqrt(sqrt(count(blocks)))) FSFI
        from dba_free_space
        group by tablespace_name order by 1;
        spool fsfi.rep;
        /
        spool off;


比如,在某数据库运行脚本 fsfi.sql, 得到以下 FSFI 值:

        TABLESPACE_NAME FSFI

        -------------------------------------
        
        RBS 74.06

        SYSTEM 100.00

        TEMP 22.82

        TOOLS 75.79

        USERS 100.00

        USER_TOOLS 100.00

        YDCX_DATA 47.34

        YDCX_IDX 57.19

        YDJF_DATA 33.80

        YDJF_IDX 75.55


统计出了数据库的 FSFI 值,就可以把它作为一个可比参数。在一个有着足够有效自由空间,且FSFI 值超过 30 的表空间中,很少会遇见有效自由空间的问题。当一个空间将要接近可比参数时,就需要做碎片整理了。





 碎片整理


1)表空间的 pctincrease 值为非 0。
可以将表空间的缺省存储参数 pctincrease 改为非 0 。一般将其设为 1 ,如:

alter tablespace temp

default storage(pctincrease 1);


这样 SMON 便会将自由范围自动合并。也可以手工合并自由范围: alter tablespace temp coalesce。


 碎片整理


我们知道,段由范围组成。在有些情况下,有必要对段的碎片进行整理。要查看段的有关信息,可查看数据字典 dba_segments ,范围的信息可查看数据字典 dba_extents 。如果段的碎片过多, 将其数据压缩到一个范围的最简单方法便是用正确的存储参数将这个段重建,然后将旧表中的数据插入到新表,同时删除旧表。这个过程可以用 Import/Export (输入 输出)工具来完成。


Export ()命令有一个(压缩)标志,这个标志在读表时会引发 Export 确定该表所分配的物理空间量,它会向输出转储文件写入一个新的初始化存储参数 -- 等于全部所分配空间。若这个表关闭, 则使用 Import ()工具重新生成。这样,它的数据会放入一个新的、较大的初始段中。例如:

    exp user/password file=exp.dmp compress=Y grants=Y indexes=Y
    tables=(table1,table2);


若输出成功,则从库中删除已输出的表,然后从输出转储文件中输入表:

    imp user/password file=exp.dmp commit=Y buffer=64000 full=Y

这种方法可用于整个数据库。






下面是一种如何自动处理表空间碎片的代码,希望对上大家看上文有用

        Coalesce Tablespace Automatically
        This technique comes from Sandeep
         Naik, a database administrator
        for GSXXI, Inc. in New York City, New York
        Here is a handy script which can be
        scheduled to automatically run
        and coalesces the tablespaces.
        This script is designed to run in NT
        but can be run in any operating system
        by slight modifications in the path where the file spools
        from the SQLPLUS environment.
        It assumes that the user who runs the script
        has priviledges to view the data dictionary.
        Start of code
        --------------------------------------
        sqlplus
        prompt this script will coalesce the
        tablespace automatically
        set verify off;
        set termout off;
        set head off;
        spool c: empcoalesce.log
        select alter tablespace
        ||TABLESPACE_NAME|| coalesce ;
        from DBA_FREE_SPACE_COALESCED where
        PERCENT_EXTENTS_COALESCED <100
        or PERCENT_BLOCKS_COALESCED<100 ;
        spool off;
        @ c: empcoalesce.log
        set head on;
        set termout on;
        set verify on;
        prompt Tablespaces are coalesced successfully



我们的优势:

Microsoft中国核心经销商

Oracle的OPN及ESL

VMWARE企业级代理商

Symantec 企业级合作伙伴

REDHAT企业级合作伙伴

易迅通金牌合作伙伴




文章转载自鑫海方圆,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论