解析oracle對select加鎖的方法以及鎖的查詢
一、oracle對select加鎖方法
複製代碼 代碼如下:create table test(a number,b number);
insert into test values(1,2);
insert into test values(3,4);
insert into test values(8,9);
commit;
---session 1 類比選中一個號碼
SQL> select * from test where a =1 for update skip locked;
A B
---------- ----------
1 2
---session 2 對a=1再進行select
SQL> select * from test where a = 1 for update skip locked;
未選定行
-- session 3 全表select
SQL> select * from test for update skip locked;
A B
---------- ----------
3 4
8 9
SQL>
二、查詢那些使用者,操縱了那些表造成了鎖機
複製代碼 代碼如下:SELECT s.username,
decode(l.type,'TM','TABLE LOCK',
'TX','ROW LOCK',
NULL) LOCK_LEVEL,
o.owner,o.object_name,o.object_type,
s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser
FROM v$session s,v$lock l,all_objects o
WHERE l.sid = s.sid
AND l.id1 = o.object_id(+)
AND s.username is NOT Null
三、查出被鎖的表,和鎖住這個表的會話ID
select a.session_id ,b.* from v$locked_object a,all_objects b
where a.object_id=b.object_id
四、 查出對應的SQL語句複製代碼 代碼如下:select vs.SQL_TEXT,vsess.sid,vsess.SERIAL#,vsess.MACHINE,vsess.OSUSER
,vsess.TERMINAL,vsess.PROGRAM,vs.CPU_TIME,vs.DISK_READS
from v$sql vs,v$session vsess
where vs.ADDRESS=vsess.SQL_ADDRESS
and vsess.sid=(上面查出來的會話ID)
五、
1.查哪個過程被鎖
查V$DB_OBJECT_CACHE視圖:
SELECT * FROM V$DB_OBJECT_CACHE WHERE OWNER='過程的所屬使用者' AND LOCKS!='0';
2. 查是哪一個SID,通過SID可知道是哪個SESSION.
查V$ACCESS視圖:
SELECT * FROM V$ACCESS WHERE OWNER='過程的所屬使用者' AND NAME='剛才查到的過程名';
3. 查出SID和SERIAL#
查V$SESSION視圖:
SELECT SID,SERIAL#,PADDR FROM V$SESSION WHERE SID='剛才查到的SID'
查V$PROCESS視圖:
SELECT SPID FROM V$PROCESS WHERE ADDR='剛才查到的PADDR';
4. 殺進程
(1).先殺ORACLE進程:
ALTER SYSTEM KILL SESSION '查出的SID,查出的SERIAL#';
(2).再殺作業系統進程:
KILL -9 剛才查出的SPID
或
ORAKILL 剛才查出的SID 剛才查出的SPID
六、尋找最耗費系統資源的SQL
複製代碼 代碼如下:--CPU
select b.sql_text,
a.buffer_gets,
a.executions,
a.buffer_gets/decode(a.executions , 0 , 1 , a.executions),
c.username
from V$sqlarea a,
v$sqltext_with_newlines b,
dba_users c
where a.parsing_user_id = c.user_id
and a.address = b.address
order by a.buffer_gets desc , b.piece
複製代碼 代碼如下:--IO
select b.sql_text,
a.disk_reads,
a.executions,
a.disk_reads/decode(a.executions , 0 , 1 , a.executions),
c.username
from v$sqlarea a,
v$sqltext_with_newlines b,
dba_users c
where a.parsing_user_id = c.user_id
and a.address = b.address
order by a.disk_reads desc , b.piece
複製代碼 代碼如下:select s.sid,s.value "CPU Used"
from v$sesstat s,v$statname n
where s.statistic#=n.statistic# and n.name='CPU used by this session'
and s.value>0
order by 2 desc;