ORACLE查看表空间

--1、查看表空间的名称及大小
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size
FROM dba_tablespaces t, dba_data_files d
WHERE t.tablespace_name = d.tablespace_name
GROUP BY t.tablespace_name;

SELECT

tablespace_name as 表空间,

round((sum_alloc - nvl(sum_free,0))/1024/1024,1) as 已用空间M,

round(sum_max/1024/1024,1) as 总大小M,

round(100*(sum_alloc - nvl(sum_free,0))/sum_max,1) As 使用百分比

FROM ( SELECT tablespace_name, sum(bytes) AS sum_alloc, sum(decode(maxbytes,0,bytes,maxbytes)) AS sum_max

FROM dba_data_files

GROUP BY tablespace_name),( SELECT tablespace_name AS fs_ts_name, sum(bytes) AS sum_free

FROM dba_free_space

GROUP BY tablespace_name )

WHERE tablespace_name =fs_ts_name(+)

order by 使用百分比 desc;
--2、查看表空间物理文件的名称及大小
SELECT tablespace_name,
file_id,
file_name,
round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files
ORDER BY tablespace_name;
--3、查看回滚段名称及大小
SELECT segment_name,
tablespace_name,
r.status,
(initial_extent / 1024) initialextent,
(next_extent / 1024) nextextent,
max_extents,
v.curext curextent
FROM dba_rollback_segs r, v$rollstat v
WHERE r.segment_id = v.usn(+)
ORDER BY segment_name;
--4、查看控制文件
SELECT NAME FROM v$controlfile;
--5、查看日志文件
SELECT MEMBER FROM v$logfile;
--6、查看表空间的使用情况
SELECT SUM(bytes) / (1024 * 1024) AS free_space, tablespace_name
FROM dba_free_space
GROUP BY tablespace_name;
SELECT a.tablespace_name,
a.bytes total,
b.bytes used,
c.bytes free,
(b.bytes * 100) / a.bytes "% USED ",
(c.bytes * 100) / a.bytes "% FREE "
FROM sys.sm$ts_avail a, sys.sm$ts_used b, sys.sm$ts_free c
WHERE a.tablespace_name = b.tablespace_name
AND a.tablespace_name = c.tablespace_name;
--7、查看数据库库对象
SELECT owner, object_type, status, COUNT(*) count#
FROM all_objects
GROUP BY owner, object_type, status;
--8、查看数据库的版本 
SELECT version
FROM product_component_version
WHERE substr(product, 1, 6) = 'Oracle';
--9、查看数据库的创建日期和归档方式
SELECT created, log_mode, log_mode FROM v$database;


SQL2:

复制代码 代码如下:


--1G=1024MB
--1M=1024KB
--1K=1024Bytes
--1M=11048576Bytes
--1G=1024*11048576Bytes=11313741824Bytes
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

--查询表空间使用情况
  SELECTUPPER(F.TABLESPACE_NAME) "表空间名",
  D.TOT_GROOTTE_MB"表空间大小(M)",
  D.TOT_GROOTTE_MB- F.TOTAL_BYTES "已使用空间(M)",
  TO_CHAR(ROUND((D.TOT_GROOTTE_MB- F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",
  F.TOTAL_BYTES"空闲空间(M)",
  F.MAX_BYTES"最大块(M)"
  FROM (SELECTTABLESPACE_NAME,
  ROUND(SUM(BYTES)/ (1024 * 1024), 2) TOTAL_BYTES,
  ROUND(MAX(BYTES)/ (1024 * 1024), 2) MAX_BYTES
  FROMSYS.DBA_FREE_SPACE
  GROUP BYTABLESPACE_NAME) F,
  (SELECTDD.TABLESPACE_NAME,
  ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
  FROMSYS.DBA_DATA_FILES DD
  GROUP BYDD.TABLESPACE_NAME) D
  WHERED.TABLESPACE_NAME = F.TABLESPACE_NAME
  ORDER BY 1;
  --查询表空间的free space
  selecttablespace_name,
  count(*) asextends,
  round(sum(bytes)/ 1024 / 1024, 2) as MB,
  sum(blocks)as blocks
  fromdba_free_space
  group bytablespace_name;


--查询表空间的总容量
  selecttablespace_name, sum(bytes) / 1024 / 1024 as MB
  fromdba_data_files
  group bytablespace_name;


--查询表空间使用率
  selecttotal.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 (selecttablespace_name, sum(bytes) / 1024 / 1024 as MB
  fromdba_free_space
  group bytablespace_name) free,
  (selecttablespace_name, sum(bytes) / 1024 / 1024 as MB
  fromdba_data_files
  group bytablespace_name) total
  wherefree.tablespace_name = total.tablespace_name;

 

 

1.查找当前表级锁的SQL如下:
select sess.sid,
    sess.serial#,
    lo.oracle_username,
    lo.os_user_name,
    ao.object_name,
    lo.locked_mode
    from v$locked_object lo,
    dba_objects ao,
    v$session sess
where ao.object_id = lo.object_id and lo.session_id = sess.sid;

2.杀掉锁表进程:
alter system kill session '436,35123';

3.RAC环境中锁查找:
SELECT inst_id,DECODE(request,0,'Holder: ','Waiter: ')||sid sess,
        id1, id2, lmode, request,type,block,ctime
FROM GV$LOCK
WHERE (id1, id2, type) IN
       (SELECT id1, id2, type FROM GV$LOCK WHERErequest>0)
ORDER BY id1, request;
  


4.监控当前数据库谁在运行什么SQL语句
select osuser, username, sql_text 
from  v$session a, v$sqltext b
where  a.sql_address =b.address order by address, piece;

 

5.找使用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;


6.查看死锁信息
SELECT (SELECT username
          FROM v$session
         WHERE SID = a.SID) blocker,a.SID, 'is blocking',
       (SELECT username
          FROM v$session
         WHERE SID = b.SID) blockee,b.SID
  FROM v$lock a, v$lock b
 WHERE a.BLOCK = 1 AND b.request > 0 AND a.id1 = b.id1 AND a.id2 =b.id2;


7.具有最高等待的对象
SELECT   o.OWNER,o.object_name, o.object_type, a.event,
         SUM (a.wait_time +a.time_waited) total_wait_time
    FROM v$active_session_history a, dba_objects o
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND a.current_obj# = o.object_id
GROUP BY o.OWNER,o.object_name, o.object_type, a.event
ORDER BY total_wait_time DESC;


SELECT   a.session_id, s.osuser, s.machine, s.program, o.owner,o.object_name,
         o.object_type, a.event,
         SUM (a.wait_time +a.time_waited) total_wait_time
    FROM v$active_session_history a, dba_objects o, v$session s
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND a.current_obj# = o.object_id
     AND a.session_id = s.SID
GROUP BY o.owner,
         o.object_name,
         o.object_type,
         a.event,
         a.session_id,
         s.program,
         s.machine,
         s.osuser
ORDER BY total_wait_time DESC;

 

8.查询当前连接会话数
select s.value,s.sid,a.username
from
v$sesstat S,v$statname N,v$session A
where
n.statistic#=s.statistic# and
name='session pga memory'
and s.sid=a.sid
order by s.value;

 

9.等待最多的用户
SELECT   s.SID, s.username, SUM (a.wait_time + a.time_waited)total_wait_time
    FROM v$active_session_history a, v$session s
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
GROUP BY s.SID, s.username
ORDER BY total_wait_time DESC;

 

10.等待最多的SQL
SELECT   a.program, a.session_id, a.user_id, d.username, s.sql_text,
         SUM (a.wait_time +a.time_waited) total_wait_time
    FROM v$active_session_history a, v$sqlarea s, dba_users d
   WHERE a.sample_time BETWEEN SYSDATE - 30 / 2880 AND SYSDATE
     AND a.sql_id = s.sql_id
     AND a.user_id = d.user_id
GROUP BY a.program, a.session_id, a.user_id, s.sql_text, d.username;

 

11.查看消耗资源最多的SQL
SELECT hash_value, executions, buffer_gets, disk_reads, parse_calls
FROM V$SQLAREA
WHERE buffer_gets > 10000000 OR disk_reads > 1000000
ORDER BY buffer_gets + 100 * disk_reads DESC;

 

12.查看某条SQL语句的资源消耗
SELECT hash_value, buffer_gets, disk_reads, executions, parse_calls
FROM V$SQLAREA
WHERE hash_Value = 228801498 AND address = hextoraw('CBD8E4B0');


13.查询会话执行的实际SQL
SELECT   a.SID, a.username, s.sql_text
    FROM v$session a, v$sqltext s
   WHERE a.sql_address = s.address
     AND a.sql_hash_value = s.hash_value
     AND a.status = 'ACTIVE'
ORDER BY a.username, a.SID, s.piece;


14.显示正在等待锁的所有会话
SELECT * FROM DBA_WAITERS;

 


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页