041139411160服务咨询:工作日9:00 - 18:00

Oracle数据库表空间和常用SQL操作

Linux, Windows

Oracle数据库是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的 适应高吞吐量的数据库解决方案。常见应用于JAVA环境当中。

1) 查询表空间相关信息

使用系统用户登录,然后使用如下命令,查看数据库表空间相关信息:

select * from dba_data_files;

2) 创建表空间

创建临时表空间

create temporary tablespace exercise_temp
tempfile 'D:\oracle\product\10.2.0\oradata\orcl\exercise_temp.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;

注意:目录[D:\oracle\product\10.2.0\oradata\orcl]自己定义,并确保存在,且所在磁盘大小要超过参数maxsize的值。

创建数据表空间

create tablespace exercise_data
logging
datafile 'D:\oracle\product\10.2.0\oradata\orcl\exercise_data.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;

创建用户并指定表空间

create user username identified by password
default tablespace exercise_data
temporary tablespace exercise_temp;

用户授权

grant connect,resource,dba to username;

3) 查询表空间使用情况

SELECT UPPER(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 (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_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) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 1;

4) 查询表空间总容量

select tablespace_name, sum(bytes) / 1024 / 1024 as MB
from dba_data_files
group by tablespace_name;

5) 删除表空间

删除用户

drop user username cascade;

删除表空间

drop tablespace tablespace_name including contents and datafiles;

6) 查询当前表级锁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;

7) 杀掉表锁进程

alter system kill session '436,35123';

8) 监控当前数据库谁在运行什么SQL语句

select osuser, username, sql_text  
from  v$session a, v$sqltext b 
where  a.sql_address =b.address order by address, piece;

9) 找使用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;

10) 查看死锁信息

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;

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;
lxfangs