参考博客:
http://panhongbin100.iteye.com/blog/1596414
https://blog.csdn.net/z69183787/article/details/18841621-----------------------------数据库连接数相关-------------------------------
--数据库允许的最大连接数
select value from v$parameter where name ='processes';
--当前的进程数
select count(*) from v$process;
--当前的连接数,两个参数间的关系:sessions=1.1*processes+5
select count(*) from v$session;
--并发连接数
select count(*) from v$session where status='ACTIVE';
--其中sessions_highwater是记录曾经到达的最大会话数
select * from v$parameter where name LIKE 'proc%';
---------------------------表空间相关----------------------------------------
--表空间使用率
SELECT a.tablespace_name "表空间名", total "表空间大小", free "表空间剩余大小",
(total - free) "表空间使用大小", total / (1024 * 1024 * 1024) "表空间大小(G)",
free / (1024 * 1024 * 1024) "表空间剩余大小(G)",
(total - free) / (1024 * 1024 * 1024) "表空间使用大小(G)",
round((total - free) / total, 4) * 100 "使用率 %" FROM (SELECT tablespace_name,
SUM(bytes) free FROM dba_free_space GROUP BY tablespace_name) a,
(SELECT tablespace_name, SUM(bytes) total FROM dba_data_files GROUP BY tablespace_name)
b WHERE a.tablespace_name = b.tablespace_name;
--表空间的文件路径,是否自动扩展
select tablespace_name, file_id,file_name,AUTOEXTENSIBLE ,
round(bytes/(1024*1024),0) total_space from dba_data_files
order by tablespace_name;
--修改数据文件大小
alter database datafile '/oraDatas/GMAMP796.dbf' resize 1024m;
--给表空间增加数据文件
ALTER TABLESPACE dataspace ADD DATAFILE
'/data/oradata/data/tablespace_dataspace/dataspace_6.dbf' SIZE 30G AUTOEXTEND ON NEXT 50M;
--查看表空间占用磁盘情况
SELECT B.FILE_ID 文件ID号,
B.TABLESPACE_NAME 表空间名,
B.BYTES/1024/1024 "total(M)",
(B.BYTES - SUM(NVL(A.BYTES, 0))/1024/1024) "used(M)",
SUM(NVL(A.BYTES, 0)/1024/1024) "free(M)",
round(SUM(NVL(A.BYTES, 0)) / (B.BYTES) * 100,2) 剩余百分比
FROM DBA_FREE_SPACE A, DBA_DATA_FILES B
WHERE A.FILE_ID = B.FILE_ID
GROUP BY B.TABLESPACE_NAME, B.FILE_ID, B.BYTES
ORDER BY B.FILE_ID;
-------------------服务名与实例名---------------------------------------------
--查看数据库的服务名
select name from v$database ;
--查看数据库的实例名
select * from v$instance;
-----------------错误信息--------------------------------------------------------
--查看错误信息
select * from user_errors;
--------------SGA信息------------------------------------------------------------
--查看SGA
select * from v$sga;
--查看share pool,large pool,java pool等信息
select component, (current_size/1024/1024) "当前大小(M)",
(min_size/1024/1024) "最小值(M)",(max_size/1024/1024) "最大值(M)" from v$sga_dynamic_components;
select * from v$sga_dynamic_components;
--监控sga内存分配信息
select * from v$sgainfo;
--监控每个用户的磁盘io及io命中率
--监控每个用户的磁盘io及io命中率
select v$sess_io.*,(block_gets+consistent_gets) reads,
((block_gets+consistent_gets-physical_reads)/(block_gets+consistent_gets)) ratio
from v$sess_io
where (block_gets+consistent_gets)>=1000
order by (block_gets+consistent_gets) desc;
--查询当前正在执行的sql及io信息
--查询当前正在执行的sql及io信息
select a.sql_text,
a.sql_fulltext,
a.cpu_time,
b.sid,
b.serial#,
b.username,
b.machine,
b.terminal,
b.program,
c.block_gets,
c.consistent_gets,
c.physical_reads,
c.block_changes,
c.consistent_changes
from v$sql a,
v$session b,
v$sess_io c
where a.address=b.sql_address
and b.sid=c.sid
order by c.block_changes desc;
--查询刚执行过的sql和io信息
--查询刚执行过的sql和io信息
select a.sql_text,
a.sql_fulltext,
a.cpu_time,
b.sid,
b.serial#,
b.username,
b.machine,
b.terminal,
b.program,
c.block_gets,
c.consistent_gets,
c.physical_reads,
c.block_changes,
c.consistent_changes
from v$sql a,
v$session b,
v$sess_io c
where a.address=b.prev_sql_addr
and b.sid=c.sid
order by c.block_changes desc;
--监控SGA命中率
--监控SGA命中率
SELECT A.VALUE + B.VALUE "logical_reads",
C.VALUE "phys_reads",
ROUND(100 * ((A.VALUE + B.VALUE) - C.VALUE) / (A.VALUE + B.VALUE)) "BUFFER HIT RATIO"
FROM V$SYSSTAT A, V$SYSSTAT B, V$SYSSTAT C
WHERE A.STATISTIC# = 38
AND B.STATISTIC# = 39
AND C.STATISTIC# = 40;
--性能最差的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;
--查询使用CPU多的用户session
SELECT A.SID,
SPID,
STATUS,
SUBSTR(A.PROGRAM, 1, 40) PROG,
A.TERMINAL,
OSUSER,
VALUE / 60 / 100 VALUE
FROM V$SESSION A, V$PROCESS B, V$SESSTAT C
WHERE C.STATISTIC# = 12
AND C.SID = A.SID
AND A.PADDR = B.ADDR
ORDER BY VALUE DESC;
--当前每个会话使用的对象数
--当前每个会话使用的对象数
SELECT A.SID, S.TERMINAL, S.PROGRAM, COUNT(A.SID)
FROM V$ACCESS A, V$SESSION S
WHERE A.OWNER <> 'SYS'
AND S.SID = A.SID
GROUP BY A.SID, S.TERMINAL, S.PROGRAM
ORDER BY COUNT(A.SID);
--内存命中率
--内存命中率
SELECT name profile,cnt,decode( total, 0, 0, round( cnt * 100 / total)) "内存命中率"
FROM ( SELECT name, value cnt, ( SUM( value ) OVER()) total
FROM v$sysstat
WHERE name LIKE 'workarea exec%');
-- 数据缓冲区高速缓存
-- 数据缓冲区高速缓存
SELECT physical_reads, db_block_gets, consistent_gets, NAME,
100 * ( 1 - ( physical_reads / (consistent_gets + db_block_gets - physical_reads))) "Data Buffer Hit Ratio"
FROM v$buffer_pool_statistics;
-- 重做日至缓冲区
-- 重做日至缓冲区
SELECT a.VALUE redo_entries, b.VALUE redo_buffer_allocation_retries,
ROUND ((1 - b.VALUE / a.VALUE) * 100, 4) log_buffer_ratio
FROM v$sysstat a, v$sysstat b
WHERE a.NAME = 'redo entries' AND b.NAME = 'redo buffer allocation retries';
-- 数据字典高速缓存
SELECT SUM (pinhits) / SUM (pins) * 100 "hit radio"
FROM v$librarycache;
--执行次数最多的top 10
--执行次数最多的top 10
select sql_text,executions
from (select sql_text,executions,
rank() over
(order by executions desc) exec_rank
from v$sql)
where exec_rank <=10;