oracle 监测脚本

oracle 性能监测脚本

--索引占用空间

select segment_name,sum(bytes)/1024/1024 from user_segments where segment_type='INDEX' and segment_name = 'XPK_M_CJZB_QJ_M' group by segment_name;

--查对象大小

select segment_name,sum(bytes)/1024/1024 from user_segments group by segment_name;

--查表空间大小

select t.tablespace_name,d.file_name,d.autoextensible,d.bytes,d.maxbytes,d.status 
from dba_tablespaces t,dba_data_files d where t.tablespace_name = d.tablespace_name and t.tablespace_name='FDMTBS';

--查空闲空间

select t.name, --表空间名称
       free_space,  --剩余大小
       (total_space - free_space) used_space, --被使用大小
       total_space --总大小
  from (select tablespace_name, sum(bytes / 1024 / 1024 / 1024) free_space
          from sys.dba_free_space
         group by tablespace_name) free,
       (select b.name, sum(bytes / 1024 / 1024) total_space
          from sys.v_$datafile a, sys.v_$tablespace b
         where a.ts# = b.ts#
         group by b.name) t
 where free.tablespace_name = t.name;

--查表空间使用情况

select b.file_name,b.tablespace_name,(b.bytes/1024/1024/1024) as 总空间,
(b.bytes-sum(nvl(a.bytes,0)))/1024/1024/1024 as 已用,
substr((b.bytes-sum(nvl(a.bytes,0)))/(b.bytes)*100,1,5) as 空间使用率
from dba_free_space a,dba_data_files b
where a.file_id=b.file_id
group by b.tablespace_name,b.file_name,b.bytes order by b.tablespace_name;

--改表空间

alter tablespace FDMTBS add datafile '+DATA/hlgssdb/datafile/oradataFDMTBS02.DBF' size 30G autoextend on;

--查索引字段

select inde.table_name,ind.column_name from user_ind_columns ind inner join user_indexes inde on ind.index_name=inde.index_name and ind.table_name=inde.table_name;

select segment_name,segment_type,tablespace_name,extents from dba_segments where user='ZFETL' and segment_type='INDEX';

--分页查询索引信息
select * from(select A.*,rownum rn from(SELECT b.index_name,b.table_name,b.column_name,a.uniqueness,a.owner FROM all_indexes a,all_ind_columns b WHERE a.index_name = b.index_name)A where rownum <5) where rn >1;

--查看连接

select count(*) from v$process; --查看当前的连接数
select value from v$parameter where name = 'processes'; --查看数据库允许的最大连接数

select count(*) from v$session; --查看当前的连接数
select username,schemaname,osuser,machine,count(1) from v$session group by username,schemaname,osuser,machine; --查看哪些用户正在连接
select schemaname,osuser,count(1) from v$session group by schemaname,osuser order by schemaname; --查看哪些用户正在连接
select count(1) from v$session where schemaname like '%DEV'; --查看哪些用户正在连接
--修改最大连接数:
alter system set processes = 500 scope = spfile;

--查询总连接数
select value from v$parameter where name = 'processes';
--查询当前会话连接数
select count(*) from v$session;
--查询当前连接用户(需补充分页)
select username,schemaname,osuser,machine,count(1),sys_context('userenv', 'ip_address') as ipadd from v$session group by username,schemaname,osuser,machine;

--统计更新

--统计更新
exec DBMS_STATS.gather_schema_stats('hfmsspe',dbms_stats.AUTO_SAMPLE_SIZE);
--单表统计更新
exec DBMS_STATS.gather_table_stats('hfmsdev','ev_retloansign');
analyze table ev_dtlpers compute statistics for table for all indexes for all columns;
call dbms_stats.gather_table_stats(user, #tablename#);
--查看最后统计更新时间
select owner,table_name,last_analyzed from user_tab_statistics where table_name ='EV_RETLOANSIGN'; 
--查看表的统计信息
select owner,num_rows,blocks,empty_blocks,avg_space,chain_cnt,avg_row_len,last_analyzed from user_tables where table_name = 'EV_RETLOANSIGN';

--查看当前有哪些用户正在使用数据

SELECT osuser, a.username,cpu_time/executions/1000000||'s', sql_fulltext,machine  
from v$session a, v$sqlarea b;
SELECT osuser, a.username,machine,count(1)  
from v$session a, v$sqlarea b group by osuser, a.username,machine;

--锁

--查询锁
SELECT A.OWNER,A.OBJECT_NAME,B.XIDUSN,B.XIDSLOT,B.XIDSQN,B.SESSION_ID,
       B.ORACLE_USERNAME,B.OS_USER_NAME,B.PROCESS,B.LOCKED_MODE,C.MACHINE,
       C.STATUS,C.SERVER,C.PROGRAM,C.SID,C.SERIAL#,C.ACTION,C.MODULE,
       'ALTER SYSTEM KILL SESSION ''' || C.SID || ',' || C.SERIAL# || ''';' SQL
  FROM ALL_OBJECTS A, V$LOCKED_OBJECT B, SYS.GV_$SESSION C
 WHERE (A.OBJECT_ID = B.OBJECT_ID)
   AND (B.PROCESS = C.PROCESS)
 ORDER BY 1, 2;
 
 --分页查询锁信息
select * from (select A.*,rownum rn from (SELECT A.OWNER, A.OBJECT_NAME, B.SESSION_ID, B.ORACLE_USERNAME, B.LOCKED_MODE, C.STATUS, C.SID, C.SERIAL# FROM ALL_OBJECTS A, V$LOCKED_OBJECT B, SYS.GV_$SESSION C WHERE (A.OBJECT_ID = B.OBJECT_ID) AND (B.PROCESS = C.PROCESS) ORDER BY 1, 2) A where rownum <5 ) where rn > 1;
 
  --查看引起锁的sql
 select l.session_id sid, 
       s.serial#, l.locked_mode, 
       l.oracle_username, s.user#, 
       l.os_user_name, s.machine, 
       s.terminal, a.sql_text, a.action 
  from v$sqlarea a, v$session s, v$locked_object l 
where l.session_id = '838' ;

--查看正在执行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; 

--查询事务

select * from v$transaction;

--查找前十条性能差的sql

SELECT * FROM (select PARSING_USER_ID,EXECUTIONS,SORTS, 
COMMAND_TYPE,DISK_READS,sql_text FROM v$sqlarea 
order BY disk_reads DESC )where ROWNUM<100; 

--查某段时间执行的sql

select b.SQL_TEXT,b.FIRST_LOAD_TIME,b.SQL_ID,b.ELAPSED_TIME,b.LAST_LOAD_TIME,b.SQL_FULLTEXT
  from v$sqlarea b
where b.FIRST_LOAD_TIME between '2017-05-22/19:24:47' and
       '2017-05-22/23:32:47' order by b.FIRST_LOAD_TIME;

--表空间

--查询oralce表空间大小信息
SELECT D.TABLESPACE_NAME,  
       SPACE || 'M' "SUM_SPACE(M)",  
       SPACE - NVL (FREE_SPACE, 0) || 'M' "USED_SPACE(M)",  
       ROUND ( (1 - NVL (FREE_SPACE, 0) / SPACE) * 100, 2) || '%' 
          "USED_RATE(%)",  
       FREE_SPACE || 'M' "FREE_SPACE(M)" 
  FROM (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,  
                 SUM (BLOCKS) BLOCKS  
            FROM DBA_DATA_FILES  
        GROUP BY TABLESPACE_NAME) D,  
       (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) FREE_SPACE  
            FROM DBA_FREE_SPACE  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  
UNION ALL                                                           --如果有临时表空间
SELECT D.TABLESPACE_NAME,  
       SPACE || 'M' "SUM_SPACE(M)",  
       USED_SPACE || 'M' "USED_SPACE(M)",  
       ROUND (NVL (USED_SPACE, 0) / SPACE * 100, 2) || '%' "USED_RATE(%)",  
       NVL (FREE_SPACE, 0) || 'M' "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) / (1024 * 1024), 2) USED_SPACE,  
                 ROUND (SUM (BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE  
            FROM V$TEMP_SPACE_HEADER  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  
ORDER BY 1;

--查询表占用空间大小(需补充分页)
select A.TABLE_NAME ,A.owner,A.num_rows,A.b_MB,A.TABLESPACE_NAME from 
(select TABLE_NAME ,owner,num_rows,round(num_rows*avg_row_len/1024/1024,5) b_MB,TABLESPACE_NAME  from ALL_tables) A where A.B_MB is not null order by b_MB desc

--查询索引占用空间大小(需补充分页)
SELECT owner,
  segment_name,
  SUM(bytes)/1024/1024,
    tablespace_name     
  FROM dba_segments
  --WHERE owner='MYUSER'
  --And segment_name='MYSEG'
    --where segment_name = 'AA'
  GROUP BY owner,segment_name,tablespace_name
  ORDER BY 3 DESC;

--总空间大小
select T1.sum+T2.sum from 
(SELECT 
       sum(SPACE) as sum
  FROM (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,  
                 SUM (BLOCKS) BLOCKS  
            FROM DBA_DATA_FILES  
        GROUP BY TABLESPACE_NAME) D,  
       (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) FREE_SPACE  
            FROM DBA_FREE_SPACE  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+) ) T1,       --如果有临时表空间
 (SELECT 
       sum(SPACE) as sum
  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) / (1024 * 1024), 2) USED_SPACE,  
                 ROUND (SUM (BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE  
            FROM V$TEMP_SPACE_HEADER  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+) ) T2
 
 --总空闲空间
select T1.sum+T2.sum from 
(SELECT
       sum(FREE_SPACE) as sum
  FROM (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,  
                 SUM (BLOCKS) BLOCKS  
            FROM DBA_DATA_FILES  
        GROUP BY TABLESPACE_NAME) D,  
       (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) FREE_SPACE  
            FROM DBA_FREE_SPACE  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+) ) T1,      --如果有临时表空间
 (SELECT 
       NVL (FREE_SPACE, 0) as sum
  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) / (1024 * 1024), 2) USED_SPACE,  
                 ROUND (SUM (BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE  
            FROM V$TEMP_SPACE_HEADER  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  ) T2
 
 --查询表空间总数
select T1.count+T2.count from
(SELECT count(*) as count
  FROM (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) SPACE,  
                 SUM (BLOCKS) BLOCKS  
            FROM DBA_DATA_FILES  
        GROUP BY TABLESPACE_NAME) D,  
       (  SELECT TABLESPACE_NAME,  
                 ROUND (SUM (BYTES) / (1024 * 1024), 2) FREE_SPACE  
            FROM DBA_FREE_SPACE  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  ) T1,      --如果有临时表空间
 (SELECT count(*) as count
  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) / (1024 * 1024), 2) USED_SPACE,  
                 ROUND (SUM (BYTES_FREE) / (1024 * 1024), 2) FREE_SPACE  
            FROM V$TEMP_SPACE_HEADER  
        GROUP BY TABLESPACE_NAME) F  
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME(+)  ) T2
 
 --计算索引占用总空间
select sum(T1.space) from
(SELECT 
  SUM(bytes)/1024/1024 as space   
  FROM dba_segments
  GROUP BY owner,segment_name,tablespace_name) T1

--计算表占用总空间
select sum(T1.space) from
(select 
A.b_MB as space
from 
(select TABLE_NAME ,owner,num_rows,round(num_rows*avg_row_len/1024/1024,5) b_MB  
from ALL_tables) A where A.B_MB is not null order by b_MB desc) T1
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,294评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,780评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,001评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,593评论 1 289
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,687评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,679评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,667评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,426评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,872评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,180评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,346评论 1 345
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,019评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,658评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,268评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,495评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,275评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,207评论 2 352

推荐阅读更多精彩内容

  • 第一天 7月13日OCP笔记: Oracle Ocp11g准备资料: OracleFundmentals 书 管理...
    fjxCode阅读 2,813评论 0 4
  • MySQL——掘金小册《MySQL 是怎样运行的:从根儿上理解 MySQL》学习笔记 InnoDB 记录存储结构 ...
    小王的平凡生活_jerome阅读 1,216评论 0 1
  • MySQL优化相关序 为什么需要MySQL优化?避免出现页面访问错误由于数据库连接timeout产生页面5xx的错...
    云三木阅读 193评论 0 0
  • 1.MySQL Server 系统架构 逻辑模块组成:MySQL 可以看成是二层架构,第一层我们通常叫做SQL ...
    渡边Hok阅读 7,190评论 0 0
  • 很久没写博客了, 感觉自己越来越懒,这样子不行 -0-,明明那么菜还懒,还是写点东西,加深自己的印象。 MySql...
    Garwer阅读 791评论 0 1