1 查看所有数据文件
select file_name from dba_data_files
union
select file_name from dba_temp_files
union
select name from v controlfileunionselectvaluefromv c o n t r o l f i l e u n i o n s e l e c t v a l u e f r o m v parameter where name=’spfile’
union
select member from v$logfile;
2 查看正在运行的job
select
djr.sid sess,
djr.job jid,
dj.log_user subu,
dj.priv_user secd,
dj.what proc,
to_char(djr.last_date,’MM/DD’) lsd,
substr(djr.last_sec,1,5) lst,
to_char(djr.this_date,’MM/DD’) nrd,
substr(djr.this_sec,1,5) nrt,
djr.failures fail
from
sys.dba_jobs dj, sys.dba_jobs_running djr
where djr.job = dj.job
/
3 根据sid查找进程相关信息
select sid,serial#,sql_hash_value,PREV_HASH_VALUE,username,program,machine,process
from v$session where sid=&sid;
4 根据spid查找进程相关信息
select
a.sid,a.serial#,a.sql_hash_value,a.PREV_HASH_VALUE,
a.username,a.program,a.machine,a.process
from v sessiona,v s e s s i o n a , v process b where a.paddr=b.addr and spid=&spid;
5 通过hash_value获取sql语句
Select sql_text from v$sqltext where hash_value=&hash_value order by piece;
6 使用explain plan for 获取执行计划
Explain plan for &sql; 这里对应具体sql语句
Select * from table(dbms_xplan.display);
7.查找当前活动进程以及对应具体操作
Select sid,serial#,username,status,command,machine,sql_hash_value,prev_hash_value From v$session where status=’ACTIVE’ and type<>’BACKGROUND’;
8.查看数据版本以及位数
使用file
cd ORACLEHOME/bin[oracle@localhostbin] O R A C L E H O M E / b i n [ o r a c l e @ l o c a l h o s t b i n ] file oracle
查看视图
select * from v$version;
通过登录信息查看
sqlplus / as sysdba
9.查看数据库锁信息
以上sql语句查询出的都是堵塞进程,ctime代表堵塞的时间
select * from v$lock where block=1;
通过以下sql可以查看对应堵塞进程在做什么操作
Select a.sql_text from v sqltexta,v s q l t e x t a , v session b,v$lock c Where a.hash_value=b.sql_hash_value and b.sid=c.sid and c.block=1;
set linesize 200;
select p.spid,
c.object_name,
c.subobject_name,
b.session_id,
b.oracle_username,
b.os_user_name
from gv processp,gv p r o c e s s p , g v session a, gv lockedobjectb,allobjectscwherep.addr=a.paddranda.process=b.processandc.objectid=b.objectidandobjectname!=‘OBJ l o c k e d o b j e c t b , a l l o b j e c t s c w h e r e p . a d d r = a . p a d d r a n d a . p r o c e s s = b . p r o c e s s a n d c . o b j e c t i d = b . o b j e c t i d a n d o b j e c t n a m e ! = ‘ O B J ’;
–查看阻塞
select
(select username from v sessionwheresid=a.sid)blocker,a.sid,′isblocking‘,(selectusernamefromv s e s s i o n w h e r e s i d = a . s i d ) b l o c k e r , a . s i d , ′ i s b l o c k i n g ‘ , ( s e l e c t u s e r n a m e f r o m v session where sid=b.sid) blockee,
b.sid
from v locka,v l o c k a , v lock b
where a.block = 1
and b.request > 0
and a.id1 = b.id1
and a.id2 = b.id2;
10.杀数据库进程
Select sid,serial# from vsession where sid=&sid;
Select b.spid from v session where sid=&sid; Select b.spid from v session a,v$process b where a.paddr=b.addr and a.sid=&sid;
方法一:从数据库杀进程
Alter system kill session ‘&sid,&serial#’;
方法二:从操作系统杀掉进程
kill -9 spid
注意使用这种方法一定要确认所杀的进程是不是数据库后台进程,方法如下:
ps –ef|grep spid如果输出是带有LOCAL=NO信息可以杀掉,
如果不是需要在数据库里面进一步确认:
select a.type from v sessiona,v s e s s i o n a , v process b where a.paddr=b.addr and b.spid=&spid;
如果没输出就不是后台进程。
11.监控索引
启用索引监控
Alter index ind_name monitoring usage;
停止索引监控:
Alter index ind_name nomonitoring usage;
查看索引是否使用:
Select * from v$object_usage where index_name=’&index_name’;
打开索引一段时间后,查看以上视图,如果monitoring=no说明索引没有使用,可以删掉
12.查看undo表空间使用情况
SELECT TO_CHAR(s.sid)||’,’||TO_CHAR(s.serial#) sid_serial,
NVL(s.username, ‘None’) orauser,
s.program,
r.name undoseg,
t.used_ublk * TO_NUMBER(x.value)/1024||’K’ “Undo”
FROM sys.v_rollname r,
sys.v_ rollname r, sys.v_ session s,
sys.v_transaction t,
sys.v_ transaction t, sys.v_ parameter x
WHERE s.taddr = t.addr
AND r.usn = t.xidusn(+)
AND x.name = ‘db_block_size’;
13.查看temp表空间排序情况
SELECT a.username, a.sid, a.serial#, a.osuser, b.tablespace, b.blocks, c.sql_text
FROM v sessiona,v s e s s i o n a , v sort_usage b, v$sqlarea c
WHERE a.saddr = b.session_addr
AND c.address= a.sql_address
AND c.hash_value = a.sql_hash_value
ORDER BY b.tablespace, b.blocks;
14.查看表空间使用百分比
SELECT d.tablespace_name Name,
TO_CHAR(NVL(a.bytes / 1024 / 1024, 0),’99,999,990.900’) Size_M,
TO_CHAR(NVL(a.bytes - NVL(f.bytes, 0), 0)/1024/1024,’99999999.999’)||’/’||TO_CHAR(NVL(a.bytes/1024/1024, 0), ‘99999999.999’) Used_M,
TO_CHAR(NVL((a.bytes - NVL(f.bytes, 0)) / a.bytes * 100, 0), ‘990.00’)||’%’ Used
FROM sys.dba_tablespaces d, (select tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) a,
(select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) f
WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = f.tablespace_name(+)
AND NOT (d.extent_management like ‘LOCAL’ AND d.contents like ‘TEMPORARY’)
UNION ALL
SELECT d.tablespace_name Name,
TO_CHAR(NVL(a.bytes / 1024 / 1024, 0),’99,999,990.900’) Size_M,
TO_CHAR(NVL(t.bytes, 0)/1024/1024,’99999999.999’) ||’/’||TO_CHAR(NVL(a.bytes/1024/1024, 0),’99999999.999’) Used_M,
TO_CHAR(NVL(t.bytes / a.bytes * 100, 0), ‘990.00’)||’%’ Used
FROM sys.dba_tablespaces d, (select tablespace_name, sum(bytes) bytes from dba_temp_files group by tablespace_name) a,
(select tablespace_name, sum(bytes_cached) bytes from v$temp_extent_pool group by tablespace_name) t
WHERE d.tablespace_name = a.tablespace_name(+) AND d.tablespace_name = t.tablespace_name(+) AND
d.extent_management like ‘LOCAL’ AND d.contents like ‘TEMPORARY’
order by 4 desc;
15.查找无效的对象
select owner,object_type,object_name from dba_objects where status=’INVALID’;
select constraint_name,table_name from dba_constraints where status=’INVALID’;
16.查找未定义索引的表
select table_name from user_tables where table_name not in (select table_name from user_ind_columns);
17.查找某个进程,并对它进行跟踪
select s.sid,s.serial# from v sessions,v s e s s i o n s , v process p where s.paddr=p.addr and p.spid=&1;
exec dbms_system.SET_SQL_TRACE_IN_SESSION(&1,&2,true);–开始跟踪
exec dbms_system.SET_SQL_TRACE_IN_SESSION(&1,&2,false);–结束跟踪
结束跟踪后会在 ORACLEBASE/admin/ O R A C L E B A S E / a d m i n / SID/udump目录下产生一个文件,这个文件里面包括相关sql的信息
18.显示用户正在执行什么操作
SELECT OSUSER,SERIAL#,SQL_TEXT FROM V SESSION,V S E S S I O N , V SQL WHERE V SESSION.SQLADDRESS=V S E S S I O N . S Q L A D D R E S S = V SQL.ADDRESS AND V$SESSION.STATUS = ‘ACTIVE’;
19.查找消耗CPU资源高的语句
Select hash_value,LIO
From (Select HASH_VALUE,BUFFER_GETS/EXECUTIONS LIO from v$sqlarea
Where EXECUTIONS<>0 order by 2 desc) Where rownum<11;
通过hash_value定位sql语句
实时查询最耗CPU资源的SQL语句
(1). 先通过top命令查看产用资源较多的spid号
(2).查询当前耗时的会话ID,用户名,sqlID等:
select sid,serial#,machine,username,program,sql_hash_value,sql_id,
to_char(logon_time,’yyyy/mm/dd hh24:mi:ss’) as login_time from v sessionwherepaddrin(selectaddrfromv s e s s i o n w h e r e p a d d r i n ( s e l e c t a d d r f r o m v process where spid in (‘5648612’,’256523’));
(3). 如果上一步sql_id或者 hash_value不为空,则可用v sqlarea查出当前正在使用的sqlselectsqltextfromv s q l a r e a 查 出 当 前 正 在 使 用 的 s q l s e l e c t s q l t e x t f r o m v sqltext_with_newlines
where hash_value = &hash_value
order by piece;
select * from v$sql where sql_id=”
20.查找消耗磁盘IO高的语句
Select hash_value,WIO
From ( Select HASH_VALUE,DISK_READS/EXECUTIONS WIO from v$sqlarea
Where EXECUTIONS<>0 order by 2 desc) Where rownum<11;
通过hash_value定位sql语句
21.查看正在执行全表扫描语句相关信息
SELECT sid,serial#,target,opname,sofar,totalwork,round((sofar/totalwork)*100,2) completed,sql_hash_value
FROM gv$session_longops
WHERE sofar<>totalwork and totalwork<>0 and opname not like ‘RMAN%’ order by 8;
通过sid可以查找对应进程信息。
通过sql_hash_value可以查找对应sql语句。
22.查看数据库是否启用归档
Select log_mode from v$database;
Noarchivelog代表未启用归档
Archivelog 代表启用归档
也可以在sys用户下执行:archive log list;
23.查看数据库使用什么优化器
show parameter optimizer_mode
NAME TYPE VALUE
optimizer_mode string ALL_ROWS
修改优化器:
alter system set optimizer_mode=rule;
24.导入导出规则(exp/imp)
导出规则:使用低版本exp导出
导入规则:使用目标版本imp导入
注:
1) 出于性能考虑建议使用expdp和impdp导入导出
2) Oracle Database 11g不支持exp和imp导入,可以使用,但是出现问题oracle不会解决该问题
25.找出是哪一条SQL语句将排序数据段中的空间用光了:
select s.sid || ‘,’ || s.serial# sid_serial, s.username,
o.blocks * t.block_size / 1024 / 1024 mb_used, o.tablespace,
o.sqladdr address, h.hash_value, h.sql_text
from v sortusageo,v s o r t u s a g e o , v session s, v$sqlarea h, dba_tablespaces t
where o.session_addr = s.saddr
and o.sqladdr = h.address (+)
and o.tablespace = t.tablespace_name
order by s.sid;
26.找出是哪一个session使用了临时表空间,信息是以汇总的形式给出的:
select s.sid || ‘,’ || s.serial# sid_serial, s.username, s.osuser, p.spid,
s.module,s.program,
sum (o.blocks) * t.block_size / 1024 / 1024 mb_used, o.tablespace,
count(*) sorts
from v sortusageo,v s o r t u s a g e o , v session s, dba_tablespaces t, v$process p
where o.session_addr = s.saddr
and s.paddr = p.addr
and o.tablespace = t.tablespace_name
group by s.sid, s.serial#, s.username, s.osuser, p.spid, s.module,
s.program, t.block_size, o.tablespace
order by sid_serial;
26.查看某一时段内所有执行过的SQL(默认保留7天)
select n.sql_text,m.sample_time from v$sqlarea n,dba_hist_active_sess_history m where m.sample_time >= TO_DATE(‘2017-10-16 15:15:00’,’YYYY-MM-DD HH24:MI:SS’) and m.sample_time < TO_DATE(‘2017-10-16 15:25:00’,’YYYY-MM-DD HH24:MI:SS’) and n.sql_id=m.sql_id;
27.统计某一时段内执行次数的排序
SELECT TO_CHAR(sysdate,’YYYY-MM-DD HH24:MI:SS’) AS START_TIME FROM DUAL;
SELECT M.SQL_ID ,TO_CHAR(N.BEGIN_INTERVAL_TIME,’yyyy/mm/dd hh:mi:ss’) “DATETIME”, SUM(M.EXECUTIONS_DELTA) EXECUTIONS FROM DBA_HIST_SQLSTAT M, DBA_HIST_SNAPSHOT N WHERE M.SNAP_ID= N.SNAP_ID AND M.DBID = N.DBID AND
M.INSTANCE_NUMBER = N.INSTANCE_NUMBER AND M.INSTANCE_NUMBER=1 AND TO_CHAR (N.BEGIN_INTERVAL_TIME,’yyyy/mm/dd hh:mi:ss’) > ‘2017/10/16 15:15:00’ and
TO_CHAR(N.BEGIN_INTERVAL_TIME,’yyyy/mm/dd hh:mi:ss’) < ‘2017/10/16 15:35:00’ GROUP BY M.SQL_ID , TO_CHAR(N.BEGIN_INTERVAL_TIME,’yyyy/mm/dd hh:mi:ss’) ORDER BY EXECUTIONS desc ;
28.oracle查询SQL语句执行的耗时和执行时间
select a.sql_text SQL语句,
b.etime 执行耗时,
c.user_id 用户ID,
c.SAMPLE_TIME 执行时间,
c.INSTANCE_NUMBER 实例数,
u.username 用户名, a.sql_id SQL编号
from dba_hist_sqltext a,
(select sql_id, ELAPSED_TIME_DELTA / 1000000 as etime
from dba_hist_sqlstat
where ELAPSED_TIME_DELTA / 1000000 >= 1) b,
dba_hist_active_sess_history c,
dba_users u
where a.sql_id = b.sql_id
and u.username = ‘XXXX’
and c.user_id = u.user_id
and b.sql_id = c.sql_id
–and a.sql_text like ‘%IN%’
order by SAMPLE_TIME desc,
b.etime desc;