Oracle常用维护SQL汇总

1. 查看表空间相关信息

SELECT TABLESPACE_NAME,
       BLOCK_SIZE, --db_block_size大小
       INITIAL_EXTENT
  FROM DBA_TABLESPACES

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 UPPER(F.TABLESPACE_NAME) AS TABLESPACE_NAME, --表空间名称 
     D.TOTAL_BYTES  AS TOTAL_BYTES,               --表空间大小M
       F.FREE_BYTES   AS FREE_BYTES,                --空闲大小M
     D.TOTAL_BYTES - F.FREE_BYTES AS USEAGE_BYTES, --使用大小M
     TO_CHAR(ROUND((D.TOTAL_BYTES - F.FREE_BYTES) / D.TOTAL_BYTES * 100, 2), '990.99') 
                                  AS USEAGE_PERCENT, --使用占比
     F.MAX_BYTES  AS BLOCK_MAX_BYTES               --最大BLOCK块
 FROM (SELECT TABLESPACE_NAME, 
             ROUND(SUM(BYTES) / (1024 * 1024), 2) FREE_BYTES, 
             ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES 
        FROM SYS.DBA_FREE_SPACE 
       GROUP BY TABLESPACE_NAME) F, 
     (SELECT DD.TABLESPACE_NAME, 
             ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOTAL_BYTES 
        FROM SYS.DBA_DATA_FILES DD 
       GROUP BY DD.TABLESPACE_NAME) D 
 WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME 
 ORDER BY 4 DESC


4. 查看数据库版本相关信息

SELECT PRODUCT,VERSION,STATUS FROM PRODUCT_COMPONENT_VERSION

5. 查看表大小

analyze table tablename compute statistics; 
SELECT NUM_ROWS * AVG_ROW_LEN FROM USER_TABLES WHERE TABLE_NAME = 'tablename';

6. 查看正在执行的SQL
SELECT b.sid oracleID,
       b.username,
       b.machine,
       b.PROGRAM,
       b.serial#,
       spid ,
       STATUS,
       paddr,
       sql_text,
       c.CPU_TIME
FROM v$process a, v$session b, v$sqlarea c
WHERE a.addr = b.paddr
   AND b.sql_hash_value = c.hash_value

7. 查看当前表被锁对象

SELECT SESS.SID,
       SESS.SERIAL#,
       LO.ORACLE_USERNAME,
       LO.OS_USER_NAME,
       AO.OBJECT_NAME,
       LO.LOCKED_MODE,
       SESS.MACHINE,
       SESS.TERMINAL,
       SESS.LOGON_TIME
  FROM V$LOCKED_OBJECT LO, DBA_OBJECTS AO, V$SESSION SESS
 WHERE AO.OBJECT_ID = LO.OBJECT_ID
   AND LO.SESSION_ID = SESS.SID;
  
  ALTER SYSTEM KILL SESSION 'SID,SERIAL#';   --杀掉锁表进程


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值