Oracle常用经典sql

---------------查看哪些用户连到了DB上,使用什么Program连接-----

SET LINESIZE 100
COLUMN spid FORMAT A10
COLUMN username FORMAT A10
COLUMN program FORMAT A45

SELECT s.sid, --Session identifier
s.serial#,–Session serial number. Used to uniquely identify a session’s objects.
p.spid, --Operating system process identifier
p.pid, --Oracle process identifier
s.username,–Oracle DB username
s.osuser,–Operating system client user name
s.TERMINAL,–Operating system terminal name
s.program,–Operating system program name
s.STATUS–Status of the session.ACTIVE,INACTIVE,KILLED,CACHED,SNIPED
FROM v s e s s i o n s , v session s,v process p
WHERE p.addr = s.paddr
AND s.type != ‘BACKGROUND’;

----------------------------Kill Session------------------------
Column SID FORMAT 9999999 heading "Sess|ID "
COLUMN SERIAL# FORMAT 9999999 heading "Serial# "
COLUMN OBJECT_NAME FORMAT A17 heading “OBJ NAME or|TRANS_ID” Trunc
COLUMN OSUSER FORMAT A10 heading “Op Sys|User ID”
COLUMN USERNAME FORMAT A8
select
B.SID,
C.SERIAL#,
C.USERNAME,
C.OSUSER,
DECODE(B.ID2, 0, A.OBJECT_NAME,‘Trans-’||to_char(B.ID1)) OBJECT_NAME,
B.TYPE,
DECODE(B.LMODE,0,’–Waiting–’,
1,‘Null’,
2,‘Row Share’,
3,‘Row Excl’,
4,‘Share’,
5,‘Sha Row Exc’,
6,‘Exclusive’,
‘Other’) “Lock Mode”,
DECODE(B.REQUEST,0,’ ',
1,‘Null’,
2,‘Row Share’,
3,‘Row Excl’,
4,‘Share’,
5,‘Sha Row Exc’,
6,‘Exclusive’,
‘Other’) “Req Mode”
from DBA_OBJECTS A, V L O C K B , V LOCK B, V SESSION C
where A.OBJECT_ID = B.ID1
and B.SID = C.SID
and C.USERNAME is not null
and A.object_name like upper(‘inv_rcv_std%’)
/

select t2.username,t2.sid,t2.serial#,t2.logon_time
from v l o c k e d o b j e c t t 1 , v locked_object t1,v session t2
where t1.session_id=t2.sid order by t2.logon_time;
–查看锁

–kill session的基本语法是:alter system kill session ‘sid,serial#’;
–把锁给KILL掉,下边的例子146为sid,21177为serial#
alter system kill session ‘146,21177’;

-------------------------All active sql-------------------
set feedback off
set serveroutput on size 9999
column username format a20
column sql_text format a55 word_wrapped
begin
for x in
(select username||’(’||sid||’,’||serial#||’) ospid = ‘|| process ||
’ program = ’ || program username,
to_char(LOGON_TIME,’ Day HH24:MI’) logon_time,
to_char(sysdate,’ Day HH24:MI’) current_time,
sql_address,
sql_hash_value
from v s e s s i o n w h e r e s t a t u s = A C T I V E a n d r a w t o h e x ( s q l a d d r e s s ) < > 0 0 a n d u s e r n a m e i s n o t n u l l ) l o o p f o r y i n ( s e l e c t s q l t e x t f r o m v session where status = 'ACTIVE' and rawtohex(sql_address) <> '00' and username is not null ) loop for y in (select sql_text from v sqlarea
where address = x.sql_address ) loop
if ( y.sql_text not like ‘%listener.get_cmd%’ and
y.sql_text not like ‘%RAWTOHEX(SQL_ADDRESS)%’ ) then
dbms_output.put_line( ‘--------------------’ );
dbms_output.put_line( x.username );
dbms_output.put_line( x.logon_time || ’ ’ || x.current_time || ’ SQL#=’ || x.sql_hash_value);
dbms_output.put_line( substr( y.sql_text, 1, 250 ) );
end if;
end loop;
end loop;
end;

-------------------------Time since last user activity------------------------
set lines 100 pages 999
select username
, floor(last_call_et / 60) “Minutes”
, status
from v$session
where username is not null
order by last_call_et

-----------------------已知spid,查看当前正在执行或最近一次执行的语句-------------
select /*+ ordered */ sql_text from v s q l t e x t s q l w h e r e ( s q l . h a s h v a l u e , s q l . a d d r e s s ) i n ( s e l e c t d e c o d e ( s q l h a s h v a l u e , 0 , p r e v h a s h v a l u e , s q l h a s h v a l u e ) , d e c o d e ( s q l h a s h v a l u e , 0 , p r e v s q l a d d r , s q l a d d r e s s ) f r o m v sqltext sql where (sql.hash_value, sql.address) in ( select decode(sql_hash_value, 0, prev_hash_value, sql_hash_value), decode(sql_hash_value, 0, prev_sql_addr, sql_address) from v session s where s.paddr = (select addr from v$process p where p.spid = to_number(’&pid’)))
order by piece asc;

猜你喜欢

转载自blog.csdn.net/weixin_43096537/article/details/87808877