(前面两部分来自http://blog.itpub.net/21251711/viewspace-1102677/)
--查询Oracle正在执行的sql语句及执行该语句的用户
SELECT b.sid oracleID,
b.username 登录Oracle用户名,
b.serial#,
spid 操作系统ID,
paddr,
sql_text 正在执行的SQL,
b.machine 计算机名
FROM v$process a, v$session b, v$sqlarea c
WHERE a.addr = b.paddr
AND b.sql_hash_value = c.hash_value;
--查看正在执行sql的发起者的发放程序
SELECT OSUSER 电脑登录身份,
PROGRAM 发起请求的程序,
USERNAME 登录系统的用户名,
SCHEMANAME,
B.Cpu_Time 花费cpu的时间,
STATUS,
B.SQL_TEXT 执行的sql
FROM V$SESSION A
LEFT JOIN V$SQL B ON A.SQL_ADDRESS = B.ADDRESS
AND A.SQL_HASH_VALUE = B.HASH_VALUE
ORDER BY b.cpu_time DESC;
--查出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#;
--kill掉当前的锁对象
alter system kill session 'sid, s.serial#';
--查看表空间使用情况
SELECTUPPER(F.TABLESPACE_NAME) AS"表空间名称",
ROUND(D.AVAILB_BYTES, 2) AS"当前表空间大小(G)",
ROUND(D.MAX_BYTES, 2) AS"自增后(G)",
ROUND((D.AVAILB_BYTES - F.USED_BYTES), 2) AS "已用空间(G)",
TO_CHAR(ROUND((D.AVAILB_BYTES - F.USED_BYTES) / D.AVAILB_BYTES * 100,
2),
'999.99') AS "当前使用比(%)",
TO_CHAR(ROUND((D.AVAILB_BYTES - F.USED_BYTES) / D.MAX_BYTES * 100, 2),
'999.99') AS "自增使用比(%)",
ROUND(F.USED_BYTES, 2) AS"当前空闲(G)",
ROUND((D.MAX_BYTES - (D.AVAILB_BYTES - F.USED_BYTES)), 2) AS "自增后空闲(G)",
ROUND(F.MAX_BYTES, 2) AS"最大块(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024 * 1024), 6)USED_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024 * 1024), 6)MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUPBY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024 * 1024), 6)AVAILB_BYTES,
ROUND(SUM(DECODE(DD.MAXBYTES,0, DD.BYTES, DD.MAXBYTES)) /
(1024 * 1024 * 1024),
6) MAX_BYTES
FROM SYS.DBA_DATA_FILES DD
GROUPBY DD.TABLESPACE_NAME) D
WHERED.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDERBY4DESC
--查看执行很慢的sql(通过top命令,获得执行慢的pid)
SELECT /*+ ORDERED */
sql_text
FROM v$sqltext a
WHERE (a.hash_value, a.address) IN (
SELECT DECODE (sql_hash_value,
0, prev_hash_value,
sql_hash_value
),
DECODE (sql_hash_value, 0, prev_sql_addr, sql_address)
FROM v$session b
WHERE b.paddr = (SELECT addr
FROM v$process c
WHERE c.spid = '&pid'))
ORDER BY piece ASC
/
注意:v$process表中的addr实际对应于v$session表中的paddr。
------. 查询选择性高于百分之20的列,(并且包含在where条件内)
with tj1
as(
select a.TABLE_NAME,b.column_name,b.num_distinct,b.num_distinct/a.NUM_ROWS*100 选择性
from user_tables a , user_tab_columns b
where a.TABLE_NAME=b.TABLE_NAME
-- and a.table_name='TEST'
and b.num_distinct/a.NUM_ROWS*100>20
and a.NUM_ROWS!=0
group by a.TABLE_NAME,b.column_name,b.num_distinct,a.NUM_ROWS)
select distinct a.* from tj1 a ,user_ind_columns b
where a.TABLE_NAME=b.TABLE_NAME
and a.column_name not in (select column_name from user_ind_columns);
------. 抓出走了全表扫描的sql,并且, 抓出这个表访问了多少个列, 表一共有多少个列,过滤条件是什么。
SELECT a.object_owner 用户,
a.sql_id,
b.sql_text,
a.object_name 表名,
d.bytes / 1024 / 1024 表大小,
length(replace(b.sql_text, ',', ',,'))-length(b.sql_text)+1 列访问数,
count(c.column_name) 总列数,
a.filter_predicates 过滤条件,
'' ftext
FROM v$sql_plan a, v$sql b, user_tab_columns c, user_segments d
WHERE a.sql_id = b.sql_id
AND a.object_name = c.table_name
AND a.object_name = d.segment_name
AND a.options = 'FULL'
GROUP BY a.object_owner,
a.sql_id,
b.sql_text,
a.object_name,
a.filter_predicates,
b.pinned_total,
d.bytes;