常用性能监测,分析和优化工具

1.autotrace
    在SQLPLUS中开启AUTOTRACE功能
    SQL>SET TIMING ON     //显示执行SQL语句的时间
    SQL>SET AUTOTRACE ON    //打开
    SET AUTOTRACE OFF    //关闭
    SET AUTOTRACE ON EXPLAIN    //显示结果集和执行计划,但不显示统计信息
    SET AUTOTRACE TRACEONLY        //显示执行计划和统计信息,但不显示结果集
    SET AUTOTRACE TRACEONLY STATISTICS    //只显示统计信息

SYS AS SYSDBA@ORCL>SELECT * FROM HR.RSC;

RSC_ID       RSC_NAME
---------- --------------------
1       123
2       RSC
3       GMY

3 rows selected.

Elapsed: 00:00:00.07

Execution Plan
----------------------------------------------------------
Plan hash value: 402549732

---------------------------------------------------------------------------
| Id  | Operation     | Name   | Rows  | Bytes | Cost (%CPU)| Time      |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT |      |    3 |    18 |    1   (0)| 00:00:01 |
|   1 |  INDEX FULL SCAN | RSC_IX |    3 |    18 |    1   (0)| 00:00:01 |
---------------------------------------------------------------------------


Statistics
----------------------------------------------------------
    818  recursive calls
      0  db block gets
    155  consistent gets
     16  physical reads
      0  redo size
    537  bytes sent via SQL*Net to client
    420  bytes received via SQL*Net from client
      2  SQL*Net roundtrips to/from client
     21  sorts (memory)
      0  sorts (disk)
      3  rows processed




2.数据库重放
    录制负载
    预处理负载
    重放负载
    回归分析
    

3.statspack
    3.1安装statspack
        创建表空间
        CREATE TABLESPACE PERFSTAT
        DATAFILE '/u01/app/oracle/oradata/ORCL/perfstat.dbf'
        SIZE 500M
        EXTENT MANAGEMENT LOCAL;
        SQL>@/u01/app/oracle/product/11.2.2/db_1/rdbms/admin/spcreate.sql    //安装statspack
        输入perfstat_password:123456
        输入default_tablespace:PERFSTAT
            输入temporary_tablespace:TEMP
        
       卸载statspack:
        sql>@/u01/app/oracle/product/11.2.2/db_1/rdbms/admin/spdrop.sql
        
    3.2手动使用statspack生成性能报告
        使用PERFSTAT用户登录ORACLE系统
        生成系统快照:
        PERFSTAT>EXECUTE statspack.snap
        
        过段时间再生成快照:
        PERFSTAT>EXECUTE statspack.snap
        
        生成数据库性能报告:
        sql>@/u01/app/oracle/product/11.2.2/db_1/rdbms/admin/spreport.sql
        指定快照编号后,将在当前目录下生成一个报告文本文件
        查看文本文件,就可以查看到报告

    3.3设置statspack工具的自动采样
        sql>@/u01/app/oracle/product/11.2.2/db_1/rdbms/admin/spauto.sql
        vim spauto.sql
        begin
      select instance_number into :instno from v$instance;
      dbms_job.submit(:jobno, 'statspack.snap;', trunc(sysdate+1/24,'HH'), 'trunc(SYSDATE+1/24,''HH'')', TRUE, :instno);
      commit;
        end;
        1/24:表示每一个小时执行一次作业,1/48:表示每半小时执行一次,1/144表示每十分钟执行一次

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.23

Job number for automated statistics collection for this instance
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note that this job number is needed when modifying or removing
the job:

     JOBNO
----------
    23


Job queue process
~~~~~~~~~~~~~~~~~
Below is the current setting of the job_queue_processes init.ora
parameter - the value for this parameter must be greater
than 0 to use automatic statistics gathering:

NAME                     TYPE          VALUE
------------------------------------ -------------------- ------------------------------
job_queue_processes             integer          1000


Next scheduled run
~~~~~~~~~~~~~~~~~~
The next scheduled run for this job is:

       JOB NEXT_DATE NEXT_SEC
---------- --------- --------------------------------
    23 13-JAN-15 15:00:00

1 row selected.

    3.4取消自动采样作业
        查看当前的自动采样作业:
        SYS AS SYSDBA@ORCL>SELECT JOB,LOG_USER,PRIV_USER,LAST_DATE,NEXT_DATE, INTERVAL FROM USER_JOBS;

       JOB LOG_USER              PRIV_USER             LAST_DATE NEXT_DATE
---------- ------------------------------ ------------------------------ --------- ---------
INTERVAL
------------------------------------------------------------------------------------------------------------------------
    23 SYS                  SYS                       13-JAN-15
trunc(SYSDATE+1/24,'HH')


1 row selected.
    可以看到采样作业的编号为23
    取消自动采样作业:
    SQL>execute dbms_job.remove('23');
    SYS AS SYSDBA@ORCL>EXECUTE DBMS_JOB.REMOVE('23');

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.02
SYS AS SYSDBA@ORCL>SELECT JOB,LOG_USER,PRIV_USER,LAST_DATE,NEXT_DATE, INTERVAL FROM USER_JOBS;

no rows selected

    3.5:删除statspack数据(一定要以PERFSTAT用户登录才能删除)
        以PERFSTAT用户登录系统,查看PERFSTAT方案中的表名:
        SQL>SELECT TABLE_NAME FROM USER_TABLES;
        指要删除表STATS$SNAPSHOT中的数据,其他表中的数据就会级联删除
      也可以执行以下命令删除:
        SQL>@/u01/app/oracle/product/11.2.2/db_1/rdbms/admin/sptrunc.sql;
        
        
                    




4.使用动态系统视图

5.数据库审计
    标准审计
    精细审计
    


    使用SELECT语句查看ORACLE的各种命中率和性能统计数据
    1.Buffer 命中率(从缓冲区中获得的数据与所有访问数据之间的比例)
    
    命中率低的原因:SGA中的DATA BLOCK BUFFER参数的影响,也可以调整BUFFER池来提高命中率
    
    SYS AS SYSDBA@ORCL>select round((1-(physical.value-direct.value-lobs.value)/logical.value)*100,2) "Buffer Cache Hit Ratio" from v$sysstat physical,v$sysstat direct,v$sysstat lobs,v$sysstat logical where physical.name='physical reads' and direct.name='physical reads direct' and lobs.name='physical reads direct (lob)' and logical.name='session logical reads';

Buffer Cache Hit Ratio
----------------------
         94.58
    
    2.Library 命中率
        库缓存命中率,指ORACLE执行SQL语句的过程中,通过内存直接得到对象命名空间的比率
        影响的因数:ORACLE SGA中的共享池参数    
    SYS AS SYSDBA@ORCL>select round(sum(pins-reloads)/sum(pins)*100,2) "Library Cache Hit Ratio" from v$librarycache;

Library Cache Hit Ratio
-----------------------
          99.86
    

    3.Latch命中率
        内部结构维护锁命中率,通常高于99%,如果低,是因为shared_pool_size参数值过大,或者没有使用绑定变量导致硬解析过多    
SYS AS SYSDBA@ORCL>select round((1-sum(misses+immediate_misses)/sum(gets+immediate_gets))*100,2) "Latch Hit Ratio" from v$latch;

Latch Hit Ratio
---------------
      99.98


    4.In-Memory Sort命中率
        指在内存中完成排序的数量和在硬盘上进行排序的数量之比
        影响:正常指标为99%-100%,受sort_area_size或pga_aggregate_target参数影响
        SYS AS SYSDBA@ORCL>select round((1-disk.value/(disk.value+memory.value))*100,2) "In-Memory Sort" from v$sysstat disk, v$sysstat memory where disk.name='sorts (disk)' and memory.name='sorts (memory)';

In-Memory Sort
--------------
       100

    
    5.Buffer NoWait
    指在缓冲区中获取数据未等待的比率
    影响:正确指标为99%-100%,受db_block_buffers或db_cache_size等参数影响
    SYS AS SYSDBA@ORCL>select round((1-busy.value/tol.value)*100,2) "Buffer busy Nowait Ratio" from (select sum(count) value from v$waitstat where class in ('data block','segment header','undo header','undo block')) busy,(select value from v$sysstat where name='session logical reads') tol;

Buffer busy Nowait Ratio
------------------------
             100
    
    6.Redo NoWait
    指在重做缓冲区中获取数据未等待的比率
    影响:正确指标为99%-100%,受Log_buffer_size等参数的影响,比率低的原因还可能是归档速度太慢,联机日志文件太不或者联机日志文存放在比较慢的存储设备上
    SYS AS SYSDBA@ORCL>select round((1-waits.value/redos.value)*100,2) "Redo Nowait Ratio" from v$sysstat waits,v$sysstat redos where waits.name='redo log space requests' and redos.name='redo entries';

Redo Nowait Ratio
-----------------
        99.99

    7.execute to parse
        指SQL语句执行和分析的比率
        影响:正确指标接近100%,受shared_pool_size等参数的影响
    SYS AS SYSDBA@ORCL>select round((1-hard.value/total.value)*100,2) "Execute to Parse Ratio" from v$sysstat hard,v$sysstat total where hard.name='parse count (hard)' and total.name='parse count (total)';

Execute to Parse Ratio
----------------------
         76.91

    8.Parse CPU to Parse Elapsed
        指数据库用于分析CPU的时间和分析完成的比率,如果很低,说明在分析过程中CPU在等待其他资源
    SYS AS SYSDBA@ORCL>select round((1-cpu.value/total.value)*100,2) "Parse CPU to Parse Elapsed" from v$sysstat cpu, v$sysstat total where cpu.name='parse time cpu' and total.name='parse time elapsed';

Parse CPU to Parse Elapsed
--------------------------
             63.16
    
    9.Non-Parse CPU
        指查询实际运行时间占SQL语句运行加解析时间之和的比例,如果值太低,则表示解析SQL语句消耗的时间过长
        SYS AS SYSDBA@ORCL>select round((1-parse.value/total.value)*100,2) "Non-Parse CPU Ratio" from v$sysstat parse, v$sysstat total where parse.name='parse time cpu' and total.name='CPU used by this session';

Non-Parse CPU Ratio
-------------------
          75.87

    10.回滚段竞争情况
        比率越小越好,受回滚段大小的影响
        SYS AS SYSDBA@ORCL>select name,waits,gets,round(waits/gets*100,2) "Ratio" from v$rollstat a ,v$rollname b where a.usn=b.usn;
    

    11.表空间的I/0比例
     比例越小越好,受db_block_size参数影响,也和数据文件在磁盘上的分布有关
SYS AS SYSDBA@ORCL>select df.tablespace_name,sum(f.phyrds),sum(f.phyblkrd),sum(f.phywrts),sum(f.phyblkwrt) from v$filestat f, dba_data_files df where f.file#=df.file_id group by df.tablespace_name order by df.tablespace_name;

    12.数据文件的I/0比例:
        比例越小越好,受db_block_size参数影响,也和数据文件在磁盘上的分布有关
        SYS AS SYSDBA@ORCL>select df.name,sum(f.phyrds),sum(f.phyblkrd),sum(f.phywrts),sum(f.phyblkwrt) from v$filestat f, v$datafile df where f.file#=df.file# group by df.name order by df.name;

NAME             SUM(F.PHYRDS) SUM(F.PHYBLKRD) SUM(F.PHYWRTS) SUM(F.PHYBLKWRT)
-------------------- ------------- --------------- -------------- ----------------
/u01/app/oracle/orad        15        17        0         0
ata/ORCL/SYSTEM02

/u01/app/oracle/orad         2         2        0         0
ata/ORCL/UNDOTBS01.D
BF
    
    13.重做日志缓存区命中率
       命中率越大越好,应该大于90%,受log_buffer_size参数影响
    SYS AS SYSDBA@ORCL>select name,gets,misses,immediate_gets,immediate_misses, 100-round(decode(gets,0,0,misses/gets *100),2) ratio1, 100-round(decode(immediate_gets+immediate_misses,0,0,immediate_misses/(immediate_gets+immediate_misses)*100),2) ratio2 from v$latch where name in ('redo allocation','redo copy');

NAME               GETS     MISSES IMMEDIATE_GETS IMMEDIATE_MISSES     RATIO1      RATIO2
-------------------- ---------- ---------- -------------- ---------------- ---------- ----------
redo copy             78      0         9700        61      100       99.38
redo allocation        3644      1         9699         0    99.97         100

    14.碎片程序(FSFI)
        碎片程序越大越好,通常大于30%,受db_block_size和segment_size参数影响
        SYS AS SYSDBA@ORCL>select tablespace_name,sqrt(max(blocks)/sum(blocks))*(100/sqrt(sqrt(count(blocks)))) FSFI from dba_free_space group by tablespace_name order by tablespace_name;

TABLESPACE_NAME              FSFI
------------------------------ ----------
EXAMPLE                67.4301698
PERFSTAT               84.0633758
    
    
    使用初始化参数启用数据库审计
    alter system set audit_trail=db scope=spfile;
    scope参数:
        spfile:在spfile中改变一个参数的值,不影响当前的实例
        memory:仅将改变用于当前的实例
        both:将改变应用于当前实例,并且保存到SPFILE文件中
    
    查看是否启用:
    SYS AS SYSDBA@ORCL>show parameter audit_trail

NAME                     TYPE          VALUE
------------------------------------ -------------------- ------------------------------
audit_trail                 string          DB


    管理语句审计
        对指定类型的SQL语句执行进行监视和记录
        audit all| sql语句
            [by 用户[,用户]...]
            [by session | access]
            [whenever[not] successful]
        
    eg:
        audit select table by hr    //对用户HR执行SELECT语句进行审订
        audit index by hr        //对用户HR执行创建,修改和删除索引的操作进行审计
    查看当前已经启动的语句审计:
    select user_name,audit_option from dba_stmt_audit_opts;
    
    查看数据库审计记录            
    SYS AS SYSDBA@ORCL>select username,timestamp,action_name from dba_audit_trail where username='HR';

    删除审计:
    NOAUDIT {语计}
        [BY<用户>[,[用户]...]
        [WHENEVER [NOT] SUCCESSFUL]
    eg:
    SYS AS SYSDBA@ORCL>noaudit select table;

    Noaudit succeeded.
    
    管理权限审计(将成功记录添加到SYS.AUD$)
    eg:
    AUDIT CREATE ANY TALBE;
    
    从DBA_PRIV_AUDIT_OPTS视图中查看已启用的权限审计信息

    SYS AS SYSDBA@ORCL>select privilege,user_name from dba_priv_audit_opts;
    
    删除权限审计:
    SYS AS SYSDBA@ORCL>NOAUDIT CREATE ANY TABLE;
    
    管理对象审计
    
    监视和记录关于指定对象的SQL语句:包括:SELECT,INSERT,UPDATE,EXECUTE等
    eg:
    AUDIT SELECT ON HR.DEPARTMENT BY ACCESS;
    从DBA_OBJ_AUDIT_OPTS视图中可以查看到已启用的对象审计信息
    SYS AS SYSDBA@ORCL>SELECT OWNER,OBJECT_NAME,OBJECT_type,sel from dba_obj_audit_opts;
        
    (sel:select,ins:insert,upd:update,del:delete)
    
    删除权限审计:
    NOAUDIT SELECT ON HR.DEPARTMENTS;


    
    

    管理精细审计
    使用DBMS_FGA程序中的ADD_POLICY函数来创建精细审计
    SYS AS SYSDBA@ORCL>EXEC DBMS_FGA.ADD_POLICY(object_schema=>'HR',object_name=>'departments0',policy_name=>'test_aud',audit_column=>'department_name',enable=>fdse,statement_types=>'update');
        
    启用精细审计:
    EXEC DBMS_FGA.ENABLE_POLICY(object_schema=>'HR',object_name=>'DEPARTMENTS',policy_name=>'TEST_AUD');
        
    禁用精细审计:
    EXEC DBMS_FGA.DISABLE_POLICY(OBJECT_SCHEMA=>'HR',object_name=>'departments',policy_name=>'TEST_AUD')
    
    
    DBA_AUDIT_POLICIES视图中可查询已定义的精细审计:
    SYS AS SYSDBA@ORCL>select policy_name,object_schema,object_name,policy_column,enabled,audit_trail from dba_audit_policies;
    
    DBA_FGA_AUDIT_TRAIL中可以查询精细审计跟踪的内容:
    SYS AS SYSDBA@ORCL>select policy_name,db_user,timestamp,userhost from dba_fga_audit_trail;
    

    DBMS_FGA.DROP_POLICY可以删除指定的精细审计:
    EXEC DBMS_FGA.DROP_POLICY(object_schema=>'HR',object_name=>'DEPARTMENTS',policy_name=>'TEST_AUD');
    
    
   

你可能感兴趣的:(计划,统计,信息)