13.检查用户的当前操作及其使用的资源
将V$SESSION和V$SQLTEXT连接就可以显示目前每一个会话正在执行的SQL语句,如下面的程序清单所示。这在有些时候是极为有用的,例如DBA希望查看某一个给定的时间点上系统究竟执行了哪些操作。
select a.sid, a.username, s.sql_text from v$session a, v$sqltext s where a.sql_address = s.address and a.sql_hash_value = s.hash_value order by a.username, a.sid, s.piece;
select a.username, b.block_gets, b.consistent_gets,b.physical_reads, b.block_changes, b.consistent_changes from v$session a, v$sess_io b where a.sid = b.sid order by a.username;
USERNAME BLOCK_GETS CONSISTENT_GETS PHYSICAL_READS
------------------------------ ---------- --------------- --------------
DBSNMP 27 118917 246
DBSNMP 4383 111119 268
SYSMAN 45617 123434 660
SYSMAN 9416 94902 157
SYSMAN 380 37019 47
SYSTEM 11 256588 147
14.查找用户正在访问的对象
通过查询V$ACCESS视图可查看在给定的时间点上用户所访问的所有对象。这有助于查明有问题的对象,在想修改一个特定的对象时也很有用(查找谁在访问它)。然而,当系统有一个很大的共享池和数百个用户时,这个操作的开销将很大。
select a.sid, a.username, b.owner, b.object, b.type from v$session a, v$access b where a.sid = b.sid;
15.使用索引
Oracle 9i提供了监控索引使用的功能。这个新的视图表示索引是否被引用,但不能反映索引使用的频率。要监控的索引需要单独打开和关闭。可以使用alter index命令来初始化监控工作,然后通过对视图V$OBJECT_USAGE的查询来实现索引的跟踪。
select * from v$object_usage;
开始监控索引:
alter index HRDT_INDEX1 monitoring usage;
select index_name, table_name, monitoring, used,start_monitoring, end_monitoring from v$object_usage;
INDEX_NAME TABLE_NAME MON USE START_MONITORING END_MONITORING
----------- ---------- --- --- ------------------- ------------------
HRDT_INDEX1 HRS_DETAIL YES NO 10/13/2002 03:11:34
16.确定锁定问题
确定锁定问题将有助于定位正在等待其他某些用户或者某些东西的用户。可以使用这个策略来确定当前被锁定在系统中的用户。这也使DBA们可以确认一个相关的Oracle进程是否真地被锁定了,还是仅仅运行得比较慢。您还能够识别当前的语句是否正在执行锁定用户的操作。
select /*+ ordered */ b.username, b.serial#, d.id1, a.sql_text from v$lock d, v$session b, v$sqltext a where b.lockwait = d.kaddr and a.address = b.sql_address and a.hash_value = b.sql_hash_value;
查看系统中是哪个用户造成了前一个用户被锁定的问题
select /*+ ordered */ a.serial#, a.sid, a.username, b.id1, c.sql_text from v$lock b, v$session a, v$sqltext c where b.id1 in(select /*+ ordered */ distinct e.id1 from v$lock e, v$session d where d.lockwait= e.kaddr) and a.sid = b.sid and c.hash_value = a.sql_hash_value and b.request = 0;
17. 关闭有问题的会话
select username, sid, serial#, program, terminal from v$session;
alter system kill session '11,18';
You can't kill your own session though:
alter system kill session '10,4';
*ERROR at line 1:ORA-00027: cannot kill current session
18.查找使用多会话的用户
有些时候,用户喜欢使用多会话来一次完成多个任务,但这会引起问题。开发人员也会有同样的问题,如果他开发了一个创建了会派生大量进程的糟糕的应用程序。所有这些都可能降低系统的综合性能。用户名NULL是后台进程。
SQL> select username, count(*) from v$session group by username;
USERNAME COUNT(*)
------------------------------ ----------
17
SYSTEM 4
SYSMAN 3
DBSNMP 2
19.查找磁盘I/O问题
视图V$DATAFILE、V$FILESTAT和V$DBA_DATA_FILES提供了数据库中所有数据文件和磁盘的文件I/O活动信息。理想情况下,物理的读和写应当平均分布。如果没有合理的配置系统,其综合性能就会受到影响。
select a.file#, a.name, a.status, a.bytes,b.phyrds, b.phywrts from v$datafile a, v$filestat b where a.file# = b.file#;
FILE# NAME STATUS BYTES PHYRDS PHYWRTS
---------- ----------------- ------- ---------- ---------- ----------
1 D:/ORACLE/ORADATA SYSTEM 534773760 7732 1506
2 D:/ORACLE/ORADATA ONLINE 31457280 54 2778
3 D:/ORACLE/ORADATA ONLINE 356515840 2680 7905
4 D:/ORACLE/ORADATA ONLINE 5242880 25 3
20.查找回滚段的内容
这个有帮助的查询显示了一个回滚段的实际等待数。可以显示回滚信息(包括自动撤消)。还可以从程序清单显示的视图中查询Shrink和 wrap信息。查询V$ROLLNAME、V$ROLLSTAT和V$TRANSACTION视图可以提供用户如何使用回滚段和撤消表空间的信息。通常情况下,在一个时间点上不应让多个用户访问同一个回滚段(尽管这是被允许的)。
select a.name, b.extents, b.rssize, b.xacts,b.waits, b.gets, optsize, status from v$rollname a, v$rollstat b where a.usn = b.usn;
NAME EXTENTS RSSIZE XACTS WAITS GETS STATUS
----------------- ---------- ---------- ---------- ---------- ---------- -------
SYSTEM 6 385024 0 0 215 ONLINE
_SYSSMU1$ 3 1171456 0 0 3191 ONLINE
21.检查空闲列表是否充足
如果使用多进程完成大量的插入操作,空闲列表(空闲的数据库数据块的列表)的默认值1可能是不够的。如果没有使用自动空间段管理(Automatic Space Segment Management,简称ASSM),您可能需要增加空闲列表,或者空闲列表组。在使用多进程完成大量的插入操作时,应确保有足够的空闲列表和空闲列表组。空闲列表的默认存储值是1。如果您使用了ASSM,Oracle将为您管理这些参数,但是一个有大量数据交换的事务环境中,在应用ASSM前应经过仔细的测试。虽然如此,但通常最好使用ASSM。
select ((A.Count/(B.Value + C.Value))*100)Pct from V$WaitStat A, V$SysStat B, V$SysStat C where A.Class = 'free list' and B.Statistic# = ( select Statistic# from V$StatName where Name = 'db block gets') and C.Statistic# = (select Statistic# from V$StatName where Name = 'consistent gets');
22.检查角色和权限设置
根据用户名进行授权的对象级特权
select b.owner || '.' || b.table_name obj,b.privilege what_granted, b.grantable,a.username from sys.dba_users a, sys.dba_tab_privs b where a.username = b.grantee order by 1,2,3;
根据被授权人进行授权的对象级特权
Select owner || '.' || table_name obj,privilege what_granted, grantable, grantee from sys.dba_tab_privs where not exists(select 'x'from sys.dba_users where username = grantee)order by 1,2,3;
根据用户名进行授予的系统级特权
select b.privilege what_granted,b.admin_option, a.username from sys.dba_users a, sys.dba_sys_privs b where a.username = b.grantee order by 1,2;
根据被授权人进行授予的系统级特权
select privilege what_granted,admin_option, grantee from sys.dba_sys_privs where not exists ( select 'x' from sys.dba_users where username = grantee ) order by 1,2;
根据用户名授予的角色
select b.granted_role ||decode(admin_option, 'YES',' (With Admin Option)',null) what_granted, a.username from sys.dba_users a, sys.dba_role_privs b where a.username = b.grantee order by 1;
根据被授权人授予的角色
select granted_role ||decode(admin_option, 'YES',' (With Admin Option)', null) what_granted,grantee from sys.dba_role_privs where not exists(select 'x'from sys.dba_users where username = grantee ) order by 1;
用户名及已被授予的相应权限
select a.username,b.granted_role || decode(admin_option,'YES',' (With Admin Option)',null) what_granted from
sys.dba_users a,sys.dba_role_privs b where a.username = b.grantee
UNION
select a.username,b.privilege || decode(admin_option,'YES',' (With Admin Option)', null) what_granted from
sys.dba_users a,sys.dba_sys_privs b where a.username = b.grantee
UNION
select a.username,b.table_name ||'-' || b.privilege|| decode(grantable,'YES',' (With Grant Option)',null)
what_granted from sys.dba_users a, sys.dba_tab_privs b where a.username = b.granteeorder by 1;
查询用户名及相应的配置文件、默认的表空间和临时表空间
Select username, profile, default_tablespace,temporary_tablespace, created from sys.dba_users order by username;
23.等待事件V$视图
在Oracle 10g中V$SESSION_WAIT中的所有等待事件列现在都在V$SESSION中。因此,确保查询等待信息的 V$SESSION,因为它是一个更快的视图。V$ACTIVE_SESSION_HISTORY (ASH)将许多重要统计数据合并为一个视图或一个报表(ASH报表)。
马上该谁等待--查询V$SESSION_WAIT / V$SESSION
select event, sum(decode(wait_time,0,1,0)) "Waiting Now",sum(decode(wait_time,0,0,1)) "Previous Waits",count(*) "Total" from v$session_wait group by event order by count(*);
马上该谁等待;SPECIFIC Waits--查询V$SESSION_WAIT
SELECT /*+ ordered */ sid, event, owner, segment_name, segment_type,p1,p2,p3 FROM v$session_wait sw, dba_extents de WHERE de.file_id = sw.p1 AND sw.p2 between de.block_id and de.block_id+de.blocks - 1 AND (event = 'buffer busy waits' OR event = 'write complete waits') AND p1 IS NOT null ORDER BY event,sid;
谁在等待 - 最后10 个等待数--查询V$SESSION_WAIT_HISTORY
SELECT /*+ ordered */ sid, event, owner, segment_name, segment_type,p1,p2,p3 FROM v$session_wait sw, dba_extents de WHERE de.file_id = sw.p1 AND sw.p2 between de.block_id and de.block_id+de.blocks - 1 AND (event = 'buffer busy waits' OR event = 'write complete waits') AND p1 IS NOT null ORDER BY event,sid;
查找P1, P2, P3代表什么--查询 V$EVENT_NAME
select event#,name,parameter1 p1,parameter2 p2,parameter3 p3 from v$event_name where name in ('buffer busy waits', 'write complete waits');
会话开始后的所有等待数--查询 V$SESSION_EVENT
select sid, event, total_waits, time_waited, event_id from v$session_event where time_waited > 0 order by time_waited;
类的所有会话等待数--查询V$SESSION_WAIT_CLASS
select sid, wait_class, total_waits from v$session_wait_class;
系统启动后的所有等待数--查询V$SYSTEM_EVENT
select event, total_waits, time_waited, event_id from v$system_event where time_waited > 0 order by time_waited;
类的系统等待数--查询V$SYSTEM_WAIT_CLASS
select wait_class, total_waits from v$system_wait_class order by total_waits desc;
类的系统等待数--查询V$ACTIVE_SESSION_HISTORY
--In the query below, the highest count session is leader in non-idle wait events.
select session_id,count(1) from v$active_session_history group by session_id order by 2;
--In the query below, find the SQL for the leader in non-idle wait events.
select c.sql_id, a.sql_text from v$sql a, (select sql_id,count(1) from v$active_session_history b where sql_id is not null group by sql_idorder by 2 desc ) c where rownum <= 5 order by rownum;
oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html