在对指定表做append操作,其他再做truncate时候,会产生锁表,如下验证步骤,
澄迈网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联2013年至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
1、创建测试表,
create table test_lock(id number, value varchar2(200));
2、执行append语句;并且不做提交,insert /*+append*/ into test_lock values(1,1);
3、再次执行清表语句,truncate table test_lock;报锁表错误,
4、查看锁表语句,发现被锁表,
select b.object_name, t.*
from v$locked_object t, user_objects b
where t.object_id = b.object_id
首先你要知道表锁住了是不是正常锁?因为任何DML语句都会对表加锁。\x0d\x0a\x0d\x0a你要先查一下是那个会话那个sql锁住了表,有可能这是正常业务需求,不建议随便KILLsession,如果这个锁表是正常业务你把sessionkill掉了会影响业务的。\x0d\x0a建议先查原因再做决定。\x0d\x0a\x0d\x0a(1)锁表查询的代码有以下的形式:\x0d\x0aselectcount(*)fromv$locked_object;\x0d\x0aselect*fromv$locked_object;\x0d\x0a(2)查看哪个表被锁\x0d\x0aselectb.owner,b.object_name,a.session_id,a.locked_modefromv$locked_objecta,dba_objectsbwhereb.object_id=a.object_id;\x0d\x0a(3)查看是哪个session引起的\x0d\x0aselectb.username,b.sid,b.serial#,logon_timefromv$locked_objecta,v$sessionbwherea.session_id=b.sidorderbyb.logon_time;\x0d\x0a\x0d\x0a(4)查看是哪个sql引起的\x0d\x0aselectb.username,b.sid,b.serial#,c.*fromv$locked_objecta,v$sessionb,v$sqlcwherea.session_id=b.sid\x0d\x0aandb.SQL_ID=c.sql_idandc.sql_id=''\x0d\x0aorderbyb.logon_time;\x0d\x0a\x0d\x0a(5)杀掉对应进程\x0d\x0a执行命令:altersystemkillsession'1025,41';\x0d\x0a其中1025为sid,41为serial#.
1、生成Kill Session语句
select 'alter system kill session ''' || SID ||',' || SERIAL# || ''';' from
(
select distinct a.sid, a.Serial#, status, machine, LOCKWAIT, logon_time
from v$session a, v$locked_object b
where (a.status = 'ACTIVE' or a.status = 'INACTIVE')
and a.sid = b.session_id
and b.ORACLE_USERNAME='XYHISTEST'--加上用户名是避免把其他系统的会话也关闭,以免伤及无辜
)
2、批量执行第一步生成的语句
alter system kill session 'sid,serial#';
--alter system kill session '6976,33967';
3、查询oracle用户名,机器名,锁表对象
SELECT l.session_id sid, s.serial#, l.locked_mode,l.oracle_username,
l.os_user_name,s.machine, s.terminal, o.object_name, s.logon_time
FROM v$locked_object l, all_objects o, v$session s
WHERE l.object_id = o.object_id
AND l.session_id = s.sid
ORDER BY sid, s.serial# ;
/*或者
select s.SID, s.OSUSER, p.spid as OSPID, s.MACHINE, s.TERMINAL, s.PROGRAM
from v$session s, v$process p
where s.sid = 6 --session_id
and s.paddr = p.addr;
*/
4、查询是执行何sql语句导致锁表的
select b.sql_text
from v$session a, v$sql b
where a.sid = 6 --session_id
and a.SQL_ADDRESS = b.ADDRESS(+);
/*--或者
SELECT l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, s.user#,
l.os_user_name,s.machine, s.terminal,a.sql_text, a.action
FROM v$sqlarea a,v$session s, v$locked_object l
WHERE l.session_id = s.sid
AND s.prev_sql_addr = a.address
ORDER BY sid, s.serial#;
*/
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$session
where paddr in (select addr from v$process where spid in ('5648612','256523'));
3. 如果上一步sql_id或者 hash_value不为空,则可用v$sqlarea查出当前正在使用的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.查询哪些对象被锁:
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.下面的语句用来杀死一个进程:
alter system kill session '524,1095'; (其中24,111分别是上面查询出的sid,serial#)
3.再一次查询目前锁定的对象,若发现以上方法不能解除锁定的表,则用以下方法:
3.1 执行下面的语句获得进程(线程)号:
select spid, osuser, s.program
from v$session s,v$process p
where s.paddr=p.addr and s.sid=524 (524是上面的sid)