Oracle日常监控及日常运维常用SQL大全

----------------------性能监控语句--------------
1、查看前一天所有表空间的增长量
select C.tablespace_name,
D.“Total(MB)”,
D.“Used(MB)” - C.“Used(MB)” AS “Increment(MB)”,
to_char(trunc(sysdate - 30),‘yyyy/mm/dd’) “TIME”
from (select B.name tablespace_name,
case when B.name not like ‘UNDO%’ then round(A.tablespace_size * 8 / 1024)
when B.name like ‘UNDO%’ then round(A.tablespace_size * 8 / 1024 / 2)
END as “Total(MB)”,
round(A.tablespace_usedsize*8 / 1024) “Used(MB)”,
A.rtime
from DBA_HIST_TBSPC_SPACE_USAGE A, vKaTeX parse error: Expected 'EOF', got '#' at position 51: …space_id = B.TS#̲ and…tablespace B
where A.tablespace_id = B.TS#
and to_char(to_date(replace(rtime, ‘/’, null),
‘mmddyyyy hh24:mi:ss’),
‘yyyymmdd hh24:mi’) =
to_char(trunc(sysdate), ‘yyyymmdd hh24:mi’)) D
where C.tablespace_name = D.tablespace_name;

2、-每日增量统计
with tmp as
(select rtime,
sum(tablespace_usedsize_kb) tablespace_usedsize_kb,
sum(tablespace_size_kb) tablespace_size_kb
from (select rtime,
e.tablespace_id,
(e.tablespace_usedsize) * (f.block_size) / 1024 tablespace_usedsize_kb,
(e.tablespace_size) * (f.block_size) / 1024 tablespace_size_kb
from dba_hist_tbspc_space_usage e,
dba_tablespaces f,
v$tablespace g
where e.tablespace_id = g.TS#
and f.tablespace_name = g.NAME
and f.contents not in (‘TEMPORARY’, ‘UNDO’))
group by rtime)
select tmp.rtime,
tablespace_usedsize_kb,
tablespace_size_kb,
(tablespace_usedsize_kb - LAG(tablespace_usedsize_kb, 1, NULL)
OVER(ORDER BY tmp.rtime)) AS DIFF_KB
from tmp,
(select max(rtime) rtime from tmp group by substr(rtime, 1, 10)) t2
where t2.rtime = tmp.rtime;

3、查看哪些会话被阻塞
select c.terminal||’ (‘’‘||a.sid||’,‘||c.serial#||’‘’) is blocking ‘||b.sid
||’,'||d.serial# block_msg, a.block
from v l o c k a , v lock a,v locka,vlock b,v s e s s i o n c , v session c,v sessionc,vsession d
where a.id1=b.id1
and a.id2=b.id2
and a.block>0
and a.sid <>b.sid
and a.sid=c.sid
and b.sid=d.SID

4、查看某个会话正在执行的SQL
select sql_text from v s q l t e x t w i t h n e w l i n e s w h e r e ( h a s h v a l u e , a d d r e s s ) i n ( s e l e c t s q l h a s h v a l u e , s q l a d d r e s s f r o m v sqltext_with_newlines where (hash_value,address) in (select sql_hash_value,sql_address from v sqltextwithnewlineswhere(hashvalue,address)in(selectsqlhashvalue,sqladdressfromvsession where sid=145) order by address,piece;

5、查看数据库当前连接数及最大连接数
select count(0) from v p r o c e s s ; − − − 当前的连接数 s e l e c t v a l u e f r o m v process;---当前的连接数 select value from v process;当前的连接数selectvaluefromvparameter where name=‘processes’ --数据库允许的最大连接数

6、组装批量杀进程语句
select b.username,‘alter system kill session ‘||’’‘’||b.sid||‘,’||b.serial#||‘’‘’||‘;’,logon_time
from v l o c k e d o b j e c t a , v locked_object a,v lockedobjecta,vsession b
where a.session_id = b.sid order by b.logon_time;
----------------------运维常用SQL语句--------

1、查看表空间使用率
set line 220
select total.tablespace_name,round(total.MB, 2) as Total_MB,round(total.MB - free.MB, 2) as Used_MB,round((1 - free.MB total.MB) * 100, 2) || ‘%’ as Used_Pct
from (select tablespace_name,sum(bytes) 1024 1024 as MB from dba_free_space group by tablespace_name) free,
(select tablespace_name,sum(bytes) 1024 1024 as MB from dba_data_files group by tablespace_name) total
where free.tablespace_name = total.tablespace_name order by used_pct desc;
2、查询单个表空间使用率
select total.tablespace_name,
round(total.MB, 2) as Total_MB,
round(total.MB - free.MB, 2) as Used_MB,
round((1 - free.MB total.MB) * 100, 2) || ‘%’ as Used_Pct from
(select tablespace_name,sum(bytes) 1024 1024 as MB from dba_free_space where tablespace_name=‘TBL_SPACE’ group by tablespace_name) free,
(select tablespace_name,sum(bytes) 1024 1024 as MB from dba_data_files where tablespace_name=‘TBL_SPACE’ group by tablespace_name) total
where free.tablespace_name = total.tablespace_name order by used_pct desc;
3、查看临时表空间数据文件位置,大小,及是否自动扩展
select tablespace_name,file_name,bytes/1024/1024 mb ,autoextensible from dba_data_files where tablespace_name in (‘’) order by tablespace_name;
select tablespace_name,file_name,bytes/1024/1024 file_size,autoextensible from dba_temp_files;
–查看所有临时表空间大小
SELECT D.TABLESPACE_NAME,SPACE “SUM_SPACE(M)”,BLOCKS SUM_BLOCKS,
USED_SPACE “USED_SPACE(M)”,ROUND(NVL(USED_SPACE,0)/SPACE100,2) “USED_RATE(%)”,
NVL(FREE_SPACE,0) “FREE_SPACE(M)”
FROM
(SELECT TABLESPACE_NAME,ROUND(SUM(BYTES)/(1024
1024),2) SPACE,SUM(BLOCKS) BLOCKS
FROM DBA_TEMP_FILES
GROUP BY TABLESPACE_NAME) D,
(SELECT TABLESPACE_NAME,ROUND(SUM(BYTES_USED)/(10241024),2) USED_SPACE,
ROUND(SUM(BYTES_FREE)/(1024
1024),2) FREE_SPACE
FROM V T E M P S P A C E H E A D E R G R O U P B Y T A B L E S P A C E N A M E ) F W H E R E D . T A B L E S P A C E N A M E = F . T A B L E S P A C E N A M E ( + ) ; 4 、查看 A S M 磁盘空间 s e l e c t n a m e , s t a t e , t y p e , f r e e m b , t o t a l m b , u s a b l e f i l e m b f r o m v TEMP_SPACE_HEADER GROUP BY TABLESPACE_NAME) F WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+); 4、查看ASM磁盘空间 select name,state,type,free_mb,total_mb,usable_file_mb from v TEMPSPACEHEADERGROUPBYTABLESPACENAME)FWHERED.TABLESPACENAME=F.TABLESPACENAME(+);4、查看ASM磁盘空间selectname,state,type,freemb,totalmb,usablefilembfromvasm_diskgroup;
5、查询oracle的连接数
select count(*) from v s e s s i o n ; 6 、查看不同用户的连接数 s e l e c t u s e r n a m e , c o u n t ( u s e r n a m e ) f r o m v session; 6、查看不同用户的连接数 select username,count(username) from v session;6、查看不同用户的连接数selectusername,count(username)fromvsession where username is not null group by username;
7、查看回收站
show recyclebin
8、清空回收站
PURGE recyclebin
9、删除表,但不进入回收站
drop table tableName purge;
10、查询用户下所有创建表的语句
select
‘select dbms_metadata.get_ddl(’||‘’‘’||‘TABLE’||‘’‘’||‘,’||‘’‘’||table_name||‘’‘’||‘) from dual;’||chr(10)||‘select ‘||’’‘’||‘/’||‘’‘’|| ’ from dual;’
from user_tables;
11、查询当时创建用户的语句
select dbms_metadata.get_ddl(‘USER’,‘USERNAME’) from dual;
12、查询普通用户语句
select username from dba_users where account_status=‘OPEN’;
13、修改数据文件大小
alter database datafile ‘&path_name’ resize 10G;
alter database datafile &{file_id} resize 10G;
14、添加数据文件
alter tablespace &tablespace_name ADD datafile ‘&datafile_name’ SIZE xxx;
15、临时表空间扩容、添加临时表空间数据文件
ALTER TABLESPACE &tablespace_name ADD TEMPFILE ‘&datafile_name’ SIZE xxx;
15.大文件表空间扩容
ALTER TABLESPACE &tablespace_name RESIZE xxx;
16、大文件表空间扩容

ALTER TABLESPACE &tablespace_name RESIZE xxx
17、查询告警日志文件位置
show parameter dump
select * from vKaTeX parse error: Expected 'EOF', got '#' at position 190: …,b.sid,b.serial#̲,logon_time fro…lock_object a,v s e s s i o n b w h e r e a . s e s s i o n i d = b . s i d o r d e r b y b . l o g o n t i m e 22 、查询数据库中所有用户下占用物理空间内存大小 s e l e c t o w n e r , s u m ( b y t e s ) / 1024 / 1024 M B f r o m d b a s e g m e n t s g r o u p b y o w n e r ; 23 、日志切换 a l t e r s y s t e m s w i t c h l o g f i l e ; 24 、查看归档是否开启 a r c h i v e l o g l i s t ; s e l e c t l o g m o d e f r o m v session b where a.session_id = b.sid order by b.logon_time 22、查询数据库中所有用户下占用物理空间内存大小 select owner,sum(bytes)/1024/1024 MB from dba_segments group by owner; 23、日志切换 alter system switch logfile; 24、查看归档是否开启 archive log list; select log_mode from v sessionbwherea.sessionid=b.sidorderbyb.logontime22、查询数据库中所有用户下占用物理空间内存大小selectowner,sum(bytes)/1024/1024MBfromdb

你可能感兴趣的:(oracle,运维,sql)