1. 先通過top命令查看產(chǎn)用資源較多的spid號(hào)
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比太仆寺網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式太仆寺網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋太仆寺地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。
2.查詢當(dāng)前耗時(shí)的會(huì)話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$session
where paddr in (select addr from v$process where spid in ('5648612','256523'));
3. 如果上一步sql_id或者 hash_value不為空,則可用v$sqlarea查出當(dāng)前正在使用的sql
select sql_text
from v$sqltext_with_newlines
where hash_value = hash_value
order by piece;
也可直接使用:
select a.*,b.SQL_TEXT from (
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$session
where paddr in (select addr from v$process where spid in ('23226'))
) a,v$sql b
where a.sql_id? = b.SQL_ID(+)
4.kill占用大資源的session
Alter system kill session 'SID,SERIAL#'
解鎖:
1.查詢哪些對(duì)象被鎖:
select object_name,machine,s.sid,s.serial#
from v$locked_object l,dba_objects o ,v$session s
where l.object_id = o.object_id and l.session_id=s.sid;
2.下面的語句用來殺死一個(gè)進(jìn)程:
alter system kill session '524,1095'; (其中24,111分別是上面查詢出的sid,serial#)
3.再一次查詢目前鎖定的對(duì)象,若發(fā)現(xiàn)以上方法不能解除鎖定的表,則用以下方法:
3.1 執(zhí)行下面的語句獲得進(jìn)程(線程)號(hào):
select spid, osuser, s.program
from v$session s,v$process p
where s.paddr=p.addr and s.sid=524 (524是上面的sid)
Session1創(chuàng)建測(cè)試表:
SQL create table test (id number (10) not null , name varchar(20), primary key(id));
Table created.
SQL desc test;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(10)
NAME VARCHAR2(20)
SQL insert into test values(001,'tom');
1 row created.
SQL insert into test values(002,'lisa');
1 row created.
SQL insert into test values(003,'joy');
1 row created.
SQL insert into test values(004,'jia');
1 row created.
查看test表信息
SQL update test set name='xue' where name='joy';
1 row updated.
SQL commit;
Commit complete.
SQL select * from test updata;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
重新打開session 2:
SQL select * from test;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
update模擬鎖表
SQL update test set name='da' where name='tom';
1 row updated.
注:不提交
Session2查詢:
SQL select * from test;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
查看哪個(gè)表被鎖
SQL select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;
OWNER
------------------------------
OBJECT_NAME
--------------------------------------------------------------------------------
SESSION_ID LOCKED_MODE
---------- -----------
SYS
TEST
23 3
查看是哪個(gè)session引起的
SQL select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time;
USERNAME SID SERIAL# LOGON_TIM
------------------------------ ---------- ---------- ---------
SYS 23 23 02-JAN-20
殺掉對(duì)應(yīng)進(jìn)程
SQL alter system kill session'23,23';
System altered.
其中23為sid,23為serial#.
SQL select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id;
no rows selected
SQL select b.username,b.sid,b.serial#,logon_time from v$locked_object a,v$session b where a.session_id = b.sid order by b.logon_time;
no rows selected
session 1查詢:
SQL select * from test;
select * from test
*
ERROR at line 1:
ORA-00028: your session has been killed
SQL select * from test;
select * from test
*
ERROR at line 1:
ORA-01012: not logged on
Process ID: 5366
Session ID: 23 Serial number: 23
重新連接SQL
[oracle@localhost ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Thu Jan 2 11:39:53 2020
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL select * from test updata;
ID NAME
---------- --------------------
1 tom
2 lisa
3 xue
4 jia
在對(duì)指定表做append操作,其他再做truncate時(shí)候,會(huì)產(chǎn)生鎖表,如下驗(yàn)證步驟,
1、創(chuàng)建測(cè)試表,
create table test_lock(id number, value varchar2(200));
2、執(zhí)行append語句;并且不做提交,insert /*+append*/ into test_lock values(1,1);
3、再次執(zhí)行清表語句,truncate table test_lock;報(bào)鎖表錯(cuò)誤,
4、查看鎖表語句,發(fā)現(xiàn)被鎖表,
select b.object_name, t.*
from v$locked_object t, user_objects b
where t.object_id = b.object_id
一般先查詢并找到被鎖定的表,解鎖代碼如下:
--釋放SESSION?SQL:?
--alter?system?kill?session?'sid,?serial#';?
ALTER?system?kill?session?'23,?1647';
oracle數(shù)據(jù)庫(kù)分行級(jí)鎖和表級(jí)鎖。用select * from table-name for update完成行級(jí)鎖。用delete或update完成表級(jí)鎖。你鎖定的資源 別人會(huì)等待你的提交語句或回退語句完成以后再繼續(xù)進(jìn)行。