其他
3个最常见案例详解DBA日常维护
导读:DBA的大部分工作都是围绕着对数据库的维护而展开的,常规的日常维护更是占了绝大多数。本节将围绕日常维护中最常见的三个案例展开讲解,与大家分享排查此类问题的思路。
SQL> select a.inst_id,
a.process,
a.sid,
a.serial#,
a.sql_id,
a.event,
a.status,
a.program,
a.machine,
connect_by_isleaf as isleaf,
sys_connect_by_path(a.SID || '@' || a.inst_id, ' <- ') tree,
level as tree_level
from gv$session a
start with a.blocking_session is not null
connect by (a.sid || '@' || a.inst_id) = prior
(a.blocking_session || '@' || a.blocking_instance);
<!--省略部分列-->
INST_ID PROCESS SID SERIAL# EVENT STATUS ISLEAF TREE TREE_LEVEL
------- ------- ---- ------- ----------------------------- ------- ------ --------------- ---------
1 7663 17 6749 enq: TX - row lock contention ACTIVE 0 <- 17@1 1
1 6198 25 9989 SQL*Net message from client INACTIVE 1 <- 17@1 <- 25@1 2
1 6310 28 23199 enq: TX - row lock contention ACTIVE 0 <- 28@1 1
1 6198 25 9989 SQL*Net message from client INACTIVE 1 <- 28@1 <- 25@1 2
INST_ID:会话所在的节点号。 PROCESS:客户端进程号,与v$process中的spid不是同一个。 SID、SERIAL#、SQL_ID、STATUS、PROGRAM、MACHINE:会话信息。 ISLEAF:是否为源头,0代表否,1代表是。 TREE:树形结构,锁的层次,例如,<- 152@2 <- 153@2 <- 161@1,从左到右依次表示为节点2的会话152被节点2的会话153堵塞,而节点2的会话153又被节点1的会话161堵塞。所以节点1的会话161是锁的源头。 TREE_LEVEL:树形层次。
SQL> select 'alter system kill session ''' || sid || '' || ',' || serial# || ',@' ||
inst_id || ''' immediate;' db_kill_session
from (select a.inst_id,
a.process,
a.sid,
a.serial#,
a.sql_id,
a.event,
a.status,
a.program,
a.machine,
connect_by_isleaf as isleaf,
sys_connect_by_path(a.SID || '@' || a.inst_id, ' <- ') tree,
level as tree_level
from gv$session a
start with a.blocking_session is not null
connect by (a.sid || '@' || a.inst_id) = prior
(a.blocking_session || '@' || a.blocking_instance))
where isleaf = 1
order by tree_level asc;
KILL_SESSION
---------------------------------------------------
alter system kill session '161,5579,@1' immediate;
alter system kill session '161,5579,@1' immediate;
SQL> select inst_id, 'kill -9 ' || spid os_kill_session
from (select p.inst_id,
p.spid,
a.sid,
a.serial#,
a.sql_id,
a.event,
a.status,
a.program,
a.machine,
connect_by_isleaf as isleaf,
sys_connect_by_path(a.SID || '@' || a.inst_id, ' <- ') tree,
level as tree_level
from gv$session a, gv$process p
where a.inst_id = p.inst_id
and a.paddr = p.addr
start with a.blocking_session is not null
connect by (a.sid || '@' || a.inst_id) = prior
(a.blocking_session || '@' || a.blocking_instance))
where isleaf = 1
order by tree_level asc;
INST_ID OS_KILL_SESSION
---------- --------------------------------
1 kill -9 30049
SQL> select 'alter system kill session ''' || ss.sid || '' || ',' || ss.serial# || ',@' ||
ss.inst_id || ''' immediate;' db_kill_session
from gv$session s, gv$session ss
where s.final_blocking_session is not null
and s.final_blocking_instance = ss.inst_id
and s.final_blocking_session = ss.sid
and s.sid <> ss.sid
DB_KILL_SESSION
--------------------------------------------------
alter system kill session '161,5579,@1' immediate;
alter system kill session '161,5579,@1' immediate;
SQL> select p.inst_id, 'kill -9 ' || p.spid os_kill_session
from gv$session s, gv$session ss, gv$process p
where s.final_blocking_session is not null
and s.final_blocking_instance = ss.inst_id
and s.final_blocking_session = ss.sid
and ss.paddr = p.addr
and ss.inst_id = p.inst_id
and s.sid <> ss.sid
INST_ID OS_KILL_SESSION
---------- --------------------------------
1 kill -9 30049
SQL> select username,sid,serial#,paddr,server,status from v$session where username = 'SCOTT';
USERNAME SID SERIAL# PADDR SERVER STATUS
---------- ----- ---------- ---------------- --------- --------
SCOTT 17 6733 00000000A34C7040 DEDICATED INACTIVE
SCOTT 158 9177 00000000A34D4998 DEDICATED INACTIVE
SQL> select b.sid,b.serial#,c.spid,b.status from v$session b,v$process c where
b.paddr = c.addr and b.sid in (17,158);
SID SERIAL# SPID STATUS
---- ---------- --------- --------
17 6733 23883 INACTIVE
158 9177 24120 INACTIVE
SQL> alter system kill session '17,6733';
SQL> alter system kill session '158,9177';
SQL> select username,sid,serial#,paddr,server,status from v$session where username = 'SCOTT';
USERNAME SID SERIAL# PADDR SERVER STATUS
---------- ---- ---------- ---------------- --------- --------
SCOTT 17 6733 00000000A3551F18 PSEUDO KILLED
SCOTT 158 9177 00000000A3551F18 PSEUDO KILLED
SQL> select b.sid,b.serial#,c.spid,b.status from v$session b,v$process c where
b.paddr = c.addr and b.sid in (17,158);
no rows selected
SQL> select 'alter system kill session ''' || c.sid || '' || ',' || c.serial# || '''
immediate;' kill_session from v$session c where status='KILLED';
KILL_SESSION
-----------------------------------------------
alter system kill session '17,6733' immediate;
alter system kill session '158,9177' immediate;
kgllkuse字段:“Address of the user session that holds the lock or pin”,主要用于记录持有lock或pin的用户地址。 kgllkhdl字段:“Address of the handle for the KGL object”,主要用于记录handle的 对象地址。
SQL> select inst_id,sid, event, p1,p1text,p1raw,p2,p2text,p2raw from gv$session
where wait_class<>'Idle';
INST_ID SID EVENT P1 P1TEXT P1RAW
------- ---- ------------------ ------------------------- ----------------
1 33 library cache pin 2081944584 handle address 000000007C17F408
SQL> select s.sid,s.sql_id,s.event,dk.* from dba_kgllock dk,v$session s where
s.saddr = dk.KGLLKUSE and KGLLKHDL='000000007C17F408';
SID SQL_ID EVENT KGLLKUSE KGLLKHDL KGLLKMOD KGLLKREQ KGLL
--- ------------ ------------- ------------------- ---------------- -------- -------- ----
33 087rrdjwc2act library cache pin 00000000A92FC040 000000007C17F408 3 0 Lock
33 087rrdjwc2act library cache pin 00000000A92FC040 000000007C17F408 0 3 Pin
SQL> select ADDR,INDX,INST_ID,KGLHDADR,KGLNAOWN,KGLNAOBJ from x$kglob where
KGLHDADR='000000007C17F408';
ADDR INDX INST_ID KGLHDADR KGLNAOWN KGLNAOBJ
---------------- ---- ------- ---------------- ---------- ---------
00007FE9B0B45850 4979 1 000000007C17F408 SYS DUMMY
SQL> select ADDR,INDX,INST_ID,KGLHDADR,KGLNAOWN,KGLNAOBJ from x$kglob where
KGLNAOBJ='DUMMY'
ADDR INDX INST_ID KGLHDADR KGLNAOWN KGLNAOBJ
---------------- ---- ------- ---------------- --------- ---------
00007F987B1D8ED0 4150 2 00000000AA193870 SYS DUMMY
SQL> select s.sid,s.sql_id,s.event,dk.* from dba_kgllock dk,v$session s where
s.saddr = dk.KGLLKUSE and KGLLKHDL='00000000AA193870';
SID SQL_ID EVENT KGLLKUSE KGLLKHDL KGLLKMOD KGLLKREQ KGLL
--- ------------ ----------------- ---------------- ---------------- -------- -------- ----
424 d4wnj5j8y1mq7 PL/SQL lock timer 00000000A9787DA0 00000000AA193870 1 0 Lock
424 d4wnj5j8y1mq7 PL/SQL lock timer 00000000A9787DA0 00000000AA193870 2 0 Pin
SQL> select * from emp as of timestamp to_timestamp('2019-11-05 08:00:00', 'YYYY-
MM-DD HH:MI:SS');
SQL> select t.THREAD#, t.SEQUENCE#, t.NAME
from v$archived_log t
where t.FIRST_TIME >=to_date('2019-11-05 10:24:30', 'yyyy-mm-dd hh24:mi:ss')
and t.NEXT_TIME <=to_date('2019-11-05 14:00:30', 'yyyy-mm-dd hh24:mi:ss');
THREAD# SEQUENCE# NAME
---------- ---------- --------------------------------------------------
1 2 /app_target/easdb_dg/arch/1_2_1023532682.dbf
1 1 /app_target/easdb_dg/arch/1_1_1023532682.dbf
1 3 /app_target/easdb_dg/arch/1_3_1023532682.dbf
SQL> @$ORACLE_HOME/rdbms/admin/dbmslm.sql
Package created.
Grant succeeded.
Synonym created.
SQL> execute dbms_logmnr.add_logfile(logfilename=>'/app_target/easdb_dg/arch/
1_2_1023532682.dbf',options=>dbms_logmnr.new);
<!--继续添加-->
SQL> execute dbms_logmnr.add_logfile(logfilename=>'/app_target/easdb_dg/arch/
1_1_1023532682.dbf',options=>dbms_logmnr.addfile);
SQL> execute dbms_logmnr.add_logfile(logfilename=>'/app_target/easdb_dg/arch/
1_3_1023532682.dbf',options=>dbms_logmnr.addfile);
注意:第一个添加日志选项是new,后续添加选项是addfile。
SQL> execute dbms_logmnr.start_logmnr(Options => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG);
SQL> select sql_redo from v$logmnr_contents where SEG_OWNER='SCOTT';
<!--sql_redo用于记录当时DML的操作记录-->
SQL> select sql_undo from v$logmnr_contents where SEG_OWNER='SCOTT';
<!--若是误操作回退,则可以使用sql_undo,执行还原操作-->
扫码关注【华章计算机】视频号
每天来听华章哥讲书
书讯 | 8月书讯(上)| 这些新书不可错过书讯 | 8月书讯(下)| 这些新书不可错过资讯 | Rust跨界前端全攻略书单 | 2021半年盘点,不想你错过的重磅新书干货 | Rust跨界前端全攻略收藏 | 快收藏!!整理了100个Python小技巧!!上新 | 【新书速递】深入浅出Pandas,用好Python必备赠书 | 【第67期】2021上半年朋友圈都在传的10本书都在这了