sql收藏:杀进程、长时间操作、表空间利用率

一、根据系统进程号,检查运行的sql内容并kill session
在命令行通过ps -ef |grep XXXX.sh,获得进程号后,kill -9 进程号,有的时候会kill不干净,在数据库中还存在该进程。可以试试以下的办法:
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#’ ;

二、长时间操作
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 ;

三、表空间利用率
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;

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注