Oracle脚本系列01-SQL脚本-杀进程、长时间操作、表空间利用率
Oracle脚本系列01-SQL脚本-杀进程、长时间操作、表空间利用率
一、根据系统进程号,检查运行的sql内容并kill session
在命令行通过ps -ef |grep XXXX.sh,获得进程号后,kill -9 进程号,有的时候会kill不干净,在数据库中还存在该进程。可以试试以下的办法:
[mw_shl_code=sql,false]
1、ps -ef |grep xxx.sh 获得该脚本的进程号(假设进程号为111)
2、ps -ef |grep sqlplus 获得该脚本的sqlplus的进程号(一般来说sqlplus的进程号比脚本的进程号大1,我们假设该脚本的sqlplus进程号为112)
3、SELECT a.SID,a.SERIAL#,a.MACHINE,a.MODULE,b.SQL_TEXT FROM v$session a,v$sqlarea b
WHERE b.ADDRESS=a.SQL_ADDRESS
AND process=’&unix_process_id’ /*此处的unix_process_id为刚刚查到的sqlplus进程号,如112 */
ORDER BY PROCESS,a.MACHINE,a.PROGRAM;
我们在获得该sid和serial#的时候,还可以看看它的sql_text,验证一下是不是我们当前在执行的sql。
4、根据刚刚查到的sid和serial# ,执行:alter system kill session ‘sid,serial#’ ;
[/mw_shl_code]
二、查询Oracle长事物操作与长时间运行的SQL语句
[mw_shl_code=sql,false]1、根据刚才的操作(kill session操作的第1、2、3步骤),获得sid和serial#,记下这两个数字。
2、SELECT DECODE(TARGET_DESC,
NULL,
DECODE(TARGET,
NULL,
OPNAME,
CONCAT(OPNAME, CONCAT(‘ – ‘, TARGET))),
DECODE(TARGET,
NULL,
CONCAT(OPNAME, CONCAT(‘ : ‘, TARGET_DESC)),
CONCAT(OPNAME,
CONCAT(‘ : ‘,
CONCAT(TARGET_DESC, CONCAT(‘ – ‘, TARGET)))))) 当前操作,
SOFAR 已处理,
TOTALWORK 总共需处理,
UNITS,
START_TIME,
TO_CHAR(ELAPSED_SECONDS, ‘99999990.00’) "已经耗时(秒)",
DECODE(SOFAR,
0,
0,
ROUND(ELAPSED_SECONDS * (TOTALWORK – SOFAR) / SOFAR)) "剩余时间(秒)"
FROM V$SESSION_LONGOPS
WHERE SID = 13 /*刚刚记下的sid*/
AND SERIAL# = 15 /*刚刚记下的serial#*/
AND SOFAR < TOTALWORK ;[/mw_shl_code]
三、查询Oracle表空间使用比例和空间的SQL脚本
[mw_shl_code=sql,false]SELECT d.status "Status",
d.tablespace_name "Name",
d.contents "Type",
d.extent_management "Extent Management",
to_char(nvl(a.bytes / 1024 / 1024, 0), ‘99,999,990.900’) "Size (M)",
to_char(nvl(a.bytes – nvl(f.bytes, 0), 0) / 1024 / 1024,
‘99999999.999’) || ‘/’ ||
to_char(nvl(a.bytes / 1024 / 1024, 0), ‘99999999.999’) "Used (M)",
to_char(nvl((a.bytes – nvl(f.bytes, 0)) / a.bytes * 100, 0),
‘990.00’) "Used %"
FROM sys.dba_tablespaces d,
(SELECT tablespace_name, SUM(bytes) bytes
FROM dba_data_files
GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(bytes) bytes
FROM dba_free_space
GROUP BY tablespace_name) f
WHERE d.tablespace_name = a.tablespace_name(+)
AND d.tablespace_name = f.tablespace_name(+)
AND NOT
(d.extent_management LIKE ‘LOCAL’ AND d.contents LIKE ‘TEMPORARY’)
UNION ALL
SELECT d.status "Status",
d.tablespace_name "Name",
d.contents "Type",
d.extent_management "Extent Management",
to_char(nvl(a.bytes / 1024 / 1024, 0), ‘99,999,990.900’) "Size (M)",
to_char(nvl(t.bytes, 0) / 1024 / 1024, ‘99999999.999’) || ‘/’ ||
to_char(nvl(a.bytes / 1024 / 1024, 0), ‘99999999.999’) "Used (M)",
to_char(nvl(t.bytes / a.bytes * 100, 0), ‘990.00’) "Used %"
FROM sys.dba_tablespaces d,
(SELECT tablespace_name, SUM(bytes) bytes
FROM dba_temp_files
GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(bytes_cached) bytes
FROM v$temp_extent_pool
GROUP BY tablespace_name) t
WHERE d.tablespace_name = a.tablespace_name(+)
AND d.tablespace_name = t.tablespace_name(+)
AND d.extent_management LIKE ‘LOCAL’
AND d.contents LIKE ‘TEMPORARY’
ORDER BY "Used %" DESC;[/mw_shl_code]
四、附文档:Oracle脚本系列01-SQL脚本-杀进程、长时间操作、表空间利用率.pdf
2616