码迷,mamicode.com
首页 > 数据库 > 详细

ORACLE_DBA管理脚本

时间:2015-07-30 22:59:34      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

 

 

SYS @ prod >col index_name for a10

SYS @ prod >col table_name for a10

SYS @ prod >col start_monitoring for a20

SYS @ prod >col end_monitoring for a20

SYS @ prod >set linesize 1000

SYS @ prod >set pagesize 1000

监控索引是否使用

SYS @ prod >create index ind_deptno on childen(deptno);

 

Index created.

 

SYS @ prod >select * from v$object_usage where index_name = &index_name;

 

Enter value for index_name: ‘IND_DEPTNO‘

old   1: select * from v$object_usage where index_name = &index_name

new   1: select * from v$object_usage where index_name = ‘IND_DEPTNO‘

 

INDEX_NAME TABLE_NAME MON USE START_MONITORING     END_MONITORING

---------- ---------- --- --- -------------------- --------------------

IND_DEPTNO CHILDEN    YES NO  06/09/2014 01:38:27

 

求数据文件的I/O分布

SYS @ prod >select df.name,phyrds,phywrts,phyblkrd,phyblkwrt,singleblkrds,readtim,writetim from v$filestat fs,v$dbfile df where fs.file#=df.file# order by df.name;

 

NAME                                                   PHYRDS    PHYWRTS   PHYBLKRD  PHYBLKWRT SINGLEBLKRDS    READTIM   WRITETIM

-------------------------------------------------- ---------- ---------- ---------- ---------- ------------ ---------- ----------

/u01/app/oracle/oradata/prod/example01.dbf                  6          1          6          1            3          8          0

/u01/app/oracle/oradata/prod/sysaux01.dbf                 706        147       1588        195          483        444          3

/u01/app/oracle/oradata/prod/system01.dbf                4237         54       5164         65         4142       1344          8

/u01/app/oracle/oradata/prod/undotbs01.dbf                 27         57         27        100           24         65         34

/u01/app/oracle/oradata/prod/users01.dbf                    5          1          5          1            2          6          0

 

 

求系统中较大的latch

SYS @ prod >select name,sum(gets),sum(misses),sum(sleeps),sum(wait_time) from v$latch_children group by name having sum(gets) > 50 order by 2;

 

NAME         SUM(GETS) SUM(MISSES) SUM(SLEEPS) SUM(WAIT_TIME)

-------------------------------------------------- ---------- ----------- ----------- --------------

client/application info              72           0           0              0

Shared B-Tree                     79           0           0              0

channel handle pool latch                                  88           0           0              0

transaction allocation                                    116           0           0              0

In memory undo latch                                      120           0           0              0

OS process                                                219           0           0              0

parallel query alloc buffer                               276           0           0              0

post/wait queue                                           291           0           0              0

library cache pin allocation                              369           0           0              0

session idle bit                                          395           0           0              0

object queue header heap                                  507           0           0              0

library cache lock allocation                             699           0           0              0

kks stats                                                1753           0           0              0

redo allocation                                          1840           0           0              0

undo global data                                         3610           0           0              0

channel operations parent latch                         10662           0           0              0

object queue header operation                           11613           0           0              0

cache buffers lru chain                                 11724           1           1             39

simulator lru latch                                     13895           0           0              0

checkpoint queue latch                                  14910           0           0              0

simulator hash latch                                    15304           0           0              0

library cache lock                                      36630           0           0              0

enqueue hash chains                                     47441           0           0              0

SQL memory manager workarea list latch                  49156           0           0              0

library cache pin                                       82231           0           0              0

shared pool                                            103254           0           0              0

row cache objects                                      111045           0           0              0

library cache                                          133841           0           0              0

cache buffers chains                                   337148           0           0              0

 

求归档日志的切换频率(生产系统可能时间会很长)

SYS @ prod >select start_recid,start_time,end_recid,end_time,minutes from (select test.*, rownum as rn from (select b.recid start_recid,to_char(b.first_time,‘yyyy-mm-dd hh24:mi:ss‘) start_time,a.recid end_recid,to_char(a.first_time,‘yyyy-mm-dd hh24:mi:ss‘) end_time,round(((a.first_time-b.first_time)*24)*60,2) minutes from v$log_history a,v$log_history b where a.recid=b.recid+1 and b.first_time > sysdate - 1 order by a.first_time desc) test) y where y.rn < 30;

 

no rows selected

求回滚段正在处理的事务

 

SYS @ prod >select a.name,b.xacts,c.sid,c.serial#,d.sql_text from v$rollname a,v$rollstat b,v$session c,v$sqltext d,v$transaction e where a.usn=b.usn and b.usn=e.xidusn and c.taddr=e.addr and c.sql_address=d.address and c.sql_hash_value=d.hash_value order by a.name,c.sid,d.piece;

 

no rows selecte

 

 

 

求出无效的对象

SYS @ prod >select ‘alter procedure ‘||object_name||‘ compile;‘ from dba_objects where status=‘INVALID‘ and owner=‘&owner‘ and object_type in (‘PACKAGE‘,‘PACKAGE BODY‘,‘PROCEDURE‘);

Enter value for owner: SYS

old   1: select ‘alter procedure ‘||object_name||‘ compile;‘ from dba_objects where status=‘INVALID‘ and owner=‘&owner‘ and object_type in (‘PACKAGE‘,‘PACKAGE BODY‘,‘PROCEDURE‘)

new   1: select ‘alter procedure ‘||object_name||‘ compile;‘ from dba_objects where status=‘INVALID‘ and owner=‘SYS‘ and object_type in (‘PACKAGE‘,‘PACKAGE BODY‘,‘PROCEDURE‘)

 

‘ALTERPROCEDURE‘||OBJECT_NAME||‘COMPILE;‘

---------------------------------------------------------------------------------------------------------------------------------------------------------

alter procedure REMOVE_EMP compile;

求process/session的状态

SYS @ prod >select p.pid,p.spid,s.program,s.sid,s.serial# from v$process p,v$session s where s.paddr=p.addr;

 

       PID SPID         PROGRAM                                                 SID    SERIAL#

---------- ------------ ------------------------------------------------ ---------- ----------

         2 2908         oracle@cuug (PMON)                                      170          1

         3 2910         oracle@cuug (PSP0)                                      169          1

         4 2912         oracle@cuug (MMAN)                                      168          1

         5 2914         oracle@cuug (DBW0)                                      167          1

         6 2916         oracle@cuug (LGWR)                                      166          1

         7 2918         oracle@cuug (CKPT)                                      165          1

         8 2920         oracle@cuug (SMON)                                      160          1

         9 2922         oracle@cuug (RECO)                                      164          1

        10 2924         oracle@cuug (CJQ0)                                      163          1

        11 2926         oracle@cuug (MMON)                                      162          1

        12 2928         oracle@cuug (MMNL)                                      161          1

        15 2934         sqlplus@cuug (TNS V1-V3)                                159          3

        16 2936         oracle@cuug (ARC0)                                      156          1

        17 2938         oracle@cuug (ARC1)                                      155          1

        18 3074         sqlplus@cuug (TNS V1-V3)                                147         20

        19 2942         oracle@cuug (QMNC)                                      152          7

        20 2956         oracle@cuug (q000)                                      141          3

        25 2958         oracle@cuug (q001)                                      148          2

求当前session的状态

求表的索引信息

SYS @ prod >select ui.table_name,ui.index_name from user_indexes ui,user_ind_columns uic where ui.table_name=uic.table_name and ui.index_name=uic.index_name and ui.table_name like ‘&table_name%‘ and uic.column_name=‘&column_name‘;

Enter value for table_name: CHILDEN

Enter value for column_name: DEPTNO

old   1: select ui.table_name,ui.index_name from user_indexes ui,user_ind_columns uic where ui.table_name=uic.table_name and ui.index_name=uic.index_name and ui.table_name like ‘&table_name%‘ and uic.column_name=‘&column_name‘

new   1: select ui.table_name,ui.index_name from user_indexes ui,user_ind_columns uic where ui.table_name=uic.table_name and ui.index_name=uic.index_name and ui.table_name like ‘CHILDEN%‘ and uic.column_name=‘DEPTNO‘

 

TABLE_NAME INDEX_NAME

---------- ----------

CHILDEN    IND_DEPTNO

 

显示表的外键信息

SYS @ prod >select table_name,constraint_name from user_constraints where constraint_type =‘R‘ and constraint_name in (select constraint_name from user_cons_columns where column_name=‘&1‘);

Enter value for 1: DEPTNO

old   1: select table_name,constraint_name from user_constraints where constraint_type =‘R‘ and constraint_name in (select constraint_name from user_cons_columns where column_name=‘&1‘)

new   1: select table_name,constraint_name from user_constraints where constraint_type =‘R‘ and constraint_name in (select constraint_name from user_cons_columns where column_name=‘DEPTNO‘)

 

TABLE_NAME CONSTRAINT_NAME

---------- ------------------------------

CHILDEN    FK_DEPTNO

 

显示表的分区及子分区(user_tab_subpartitions)

 

SYS @ prod >col table_name format a16

SYS @ prod >col partition_name format a16

SYS @ prod >col high_value format a81

SYS @ prod >select table_name,partition_name,HIGH_VALUE from user_tab_partitions where table_name=‘&table_name‘

  2  ;

Enter value for table_name: CHILDEN

old   1: select table_name,partition_name,HIGH_VALUE from user_tab_partitions where table_name=‘&table_name‘

new   1: select table_name,partition_name,HIGH_VALUE from user_tab_partitions where table_name=‘CHILDEN‘

 

no rows selected

 

 

使用dbms_xplan生成一个执行计划

SYS @ prod >explain plan set statement_id = ‘&sql_id‘ for &sql_text;

Enter value for sql_id: 062savj8zgzut

Enter value for sql: UPDATE sys.wri$_adv_parameters SET datatype = :1,value = :2, flags = :3, description = :4 WHERE task_id = :5 AND name = :6

 

old   1: explain plan set statement_id = ‘&sql_id‘ for &sql

new   1: explain plan set statement_id = ‘062savj8zgzut‘ for UPDATE sys.wri$_adv_parameters SET datatype = :1,value = :2, flags = :3, description = :4 WHERE task_id = :5 AND name = :6

 

Explained.

 

SYS @ prod >select * from table(dbms_xplan.display);

 

PLAN_TABLE_OUTPUT

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Plan hash value: 618325093

 

---------------------------------------------------------------------------------------------

| Id  | Operation          | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | UPDATE STATEMENT   |                        |     1 |  2064 |     0   (0)| 00:00:01 |

|   1 |  UPDATE            | WRI$_ADV_PARAMETERS    |       |       |            |          |

|*  2 |   INDEX UNIQUE SCAN| WRI$_ADV_PARAMETERS_PK |     1 |  2064 |     0   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

 

Predicate Information (identified by operation id):

---------------------------------------------------

 

   2 - access("TASK_ID"=TO_NUMBER(:5) AND "NAME"=:6)

 

其中

sql_id 对应 v$sql里的 SQL_ID,

Sql_text 对应v$sql里的SQL_TEXT

 

 

SYS @ prod >select * from table(dbms_xplan.display);

 

PLAN_TABLE_OUTPUT

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Plan hash value: 618325093

 

---------------------------------------------------------------------------------------------

| Id  | Operation          | Name                   | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------------

|   0 | UPDATE STATEMENT   |                        |     1 |  2064 |     0   (0)| 00:00:01 |

|   1 |  UPDATE            | WRI$_ADV_PARAMETERS    |       |       |            |          |

|*  2 |   INDEX UNIQUE SCAN| WRI$_ADV_PARAMETERS_PK |     1 |  2064 |     0   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

 

Predicate Information (identified by operation id):

---------------------------------------------------

 

   2 - access("TASK_ID"=TO_NUMBER(:5) AND "NAME"=:6)

 

14 rows selected.

求某个事务的重做信息(bytes)

SYS @ prod >select s.name,m.value from v$mystat m,v$statname s where m.statistic#=s.statistic# and s.name like ‘%redo size%‘;

 

NAME                                                    VALUE

-------------------------------------------------- ----------

redo size                                               11940

 

 

求cache中缓存超过其5%的对象

SYS @ prod >select o.owner,o.object_type,o.object_name,count(b.objd) from v$bh b,dba_objects o where b.objd = o.object_id and rownum <2 group by o.owner,o.object_type,o.object_name having count(b.objd) > (select to_number(value)*0.05 from v$parameter where name = ‘db_block_buffers‘) ;

求谁阻塞了某个session(10g)

SYS @ prod > select sid, username, event, blocking_session, seconds_in_wait, wait_time from v$session where state in (‘WAITING‘) and wait_class != ‘Idle‘;

 

       SID USERNAME                       EVENT                                                            BLOCKING_SESSION SECONDS_IN_WAIT  WAIT_TIME

---------- ------------------------------ ---------------------------------------------------------------- ---------------- --------------- ----------

       145 SYS                            enq: TM - contention                                                          147             192          0

求session的OS进程ID

SYS @ prod >select p.spid "OS Thread", b.name "Name-User", s.program from v$process p, v$session s, v$bgprocess b where p.addr = s.paddr and p.addr = b.paddr UNION ALL select p.spid "OS Thread", s.username "Name-User", s.program from v$process p, v$session s where p.addr = s.paddr and s.username is not null;

 

OS Thread    Name-User                      PROGRAM

------------ ------------------------------ ------------------------------------------------------

2934         PMON                           oracle@cuug (PMON)

2936         PSP0                           oracle@cuug (PSP0)

2938         MMAN                           oracle@cuug (MMAN)

2940         DBW0                           oracle@cuug (DBW0)

2942         LGWR                           oracle@cuug (LGWR)

2944         CKPT                           oracle@cuug (CKPT)

2946         SMON                           oracle@cuug (SMON)

2948         RECO                           oracle@cuug (RECO)

2950         CJQ0                           oracle@cuug (CJQ0)

2952         MMON                           oracle@cuug (MMON)

2954         MMNL                           oracle@cuug (MMNL)

2962         ARC0                           oracle@cuug (ARC0)

2964         ARC1                           oracle@cuug (ARC1)

2966         ARC2                           oracle@cuug (ARC2)

2968         QMNC                           oracle@cuug (QMNC)

2960         SYS                            sqlplus@cuug (TNS V1-V3)

 

查会话的阻塞

 

求DISK READ较多的SQL

 

SYS @ prod >select st.sql_text from v$sql sql1,v$sqltext st where sql1.address=st.address and sql1.hash_value=st.hash_value and sql1.disk_reads > 300;

 

求DISK SORT严重的SQL

 

SYS @ prod >select sess.username, sql.sql_text, sort1.blocks from v$session sess, v$sqlarea sql, v$sort_usage sort1 where sess.serial# = sort1.session_num  and sort1.sqladdr = sql.address and sort1.sqlhash = sql.hash_value and sort1.blocks > 200;

 

求对象的创建代码

 

SYS @ prod >select dbms_metadata.get_ddl(‘TABLE‘,‘&1‘) from dual;

Enter value for 1: FATHER

old   1: select dbms_metadata.get_ddl(‘TABLE‘,‘&1‘) from dual

new   1: select dbms_metadata.get_ddl(‘TABLE‘,‘FATHER‘) from dual

 

DBMS_METADATA.GET_DDL(‘TABLE‘,‘FATHER‘)

--------------------------------------------------------------------------------

 

  CREATE TABLE "SYS"."FATHER"

   (    "DEPTNO" NUMBER(2,0),

        "DNAME" VARCHAR2(14

求表的索引

SYS @ prod >select a.index_name,a.column_name,b.status, b.index_type from user_ind_columns a,user_indexes b where a.index_name=b.index_name and a.table_name=‘&1‘;

Enter value for 1: CHILDEN

old   1: select a.index_name,a.column_name,b.status, b.index_type from user_ind_columns a,user_indexes b where a.index_name=b.index_name and a.table_name=‘&1‘

new   1: select a.index_name,a.column_name,b.status, b.index_type from user_ind_columns a,user_indexes b where a.index_name=b.index_name and a.table_name=‘CHILDEN‘

 

INDEX_NAME

------------------------------

COLUMN_NAME

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

STATUS   INDEX_TYPE

-------- ---------------------------

IND_DEPTNO

DEPTNO

VALID    NORMAL

 

索引中行数较多的            

SYS @ prod >select index_name,blevel,num_rows,CLUSTERING_FACTOR,status from user_indexes where num_rows > 10000 and blevel > 0 ;

 

 

SYS @ prod >select table_name,index_name,blevel,num_rows,CLUSTERING_FACTOR,status from user_indexes where status <> ‘VALID‘;

 

 

求表中有外键但是外键没索引的表

求有外键的表

SYS @ prod >select owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_name,status from user_constraints where constraint_type=‘R‘ and table_name=‘CHILDEN‘;

 

OWNER CONSTRAINT_NAME      CONSTRAINT_TYPE      TABLE_NAME                     R_OWNER                        R_CONSTRAINT_NAME              STATUS

----- -------------------- -------------------- ------------------------------ ------------------------------ ------------------------------ --------

SYS   FK_DEPTNO            R                    CHILDEN                        SYS                            PK_DEPTNO                      ENABLED

求外键定义在表的哪个字段

 

SYS @ prod >select owner,constraint_name,table_name,column_name from user_cons_columns where table_name=‘CHILDEN‘;

 

OWNER CONSTRAINT_NAME   TABLE_NAME      COLUMN_NAM

------------------------------------------------------------------------------------------------------

SYS   FK_DEPTNO            CHILDEN          DEPTNO

求未定义索引的表

SYS @ prod >select table_name from user_tables where table_name not in (select table_name from user_ind_columns);

 

TABLE_NAME

------------------------------

FET$

SEG$

UET$

TSQ$

 

 

 

 

SYS @ prod >select * from user_ind_columns where table_name=‘CHILDEN‘ and column_name=‘DEPTNO‘;

 

INDEX_NAME                     TABLE_NAME                     COLUMN_NAM COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC

------------------------------ ------------------------------ ---------- --------------- ------------- ----------- ----

IND_DEPTNO                     CHILDEN                        DEPTNO                   1            22           0 ASC

查看用户使用内存情况

SYS @ prod >select username, sum(sharable_mem)/1024/1024 "SIZE:M", sum(persistent_mem)/1024/1024 "SIZE:M", sum(runtime_mem)/1024/1024 "SIZE:M" from sys.v_$sqlarea a, dba_users b where a.parsing_user_id = b.user_id group by username;

 

USERNAME                           SIZE:M     SIZE:M     SIZE:M

------------------------------ ---------- ---------- ----------

EXFSYS                         .040836334 .014293671 .012340546

SYS                            3.79944134 3.11385727 2.83478165

 

ORACLE_DBA管理脚本

标签:

原文地址:http://www.cnblogs.com/iyoume2008/p/4690625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!