创建表空间语法
create [undo|temporary]tablespace 表空间名
datafile[tempfile] '文件名' size 文件大小 [autoextend on next 扩展大小 maxsize unlimited]
extent management local|dictionary //local 默认
autoallocate|uniform size 大小 //autoallocate默认
segment space management auto|manual //auto默认
1、持久表空间
drop tablespace tbs1 including contents and datafiles;
create tablespace tbs1
datafile
'E:\oracle\product\10.2.0\oradata\mydb\tbs101.dbf' size 5M,
'E:\oracle\product\10.2.0\oradata\mydb\tbs102.dbf' size 5M;
select * from dba_tablespaces;
select * from dba_data_files;
drop table t01;
create table t01 tablespace tbs1 as select * from dba_objects;
create table t02(id number) tablespace tbl1 storage(buffer_pool keep);
select * from dba_segments where segment_name='T01';
header_file: 存储在的文件编号
header_block: 段头所在的块号 11
bytes: 表大小
blocks:使用块数
extents:使用区数
buffer_pool: 段数据内存存储 keep_pool
select * from dba_extents where segment_name='T01';
extent_id: 区编号,从0开始
file_id: 所在文件编号
block_id: 给段分配的区的起始块号
bytes:区的大小
blocks:区的块数
块使用分析
tbs1: 1~8块,数据文件头使用
从第9块开始分配给表,第一个区从第9块到16块,11块是块头,9和10号块
查询数据存储位置
select rowid from t01 where rownum<2;
AAACgVAAFAAAAAMAAA
rowid解释
AAACgV: 数据对象编号 2*64*64 32*64 21 8192+2048+21
AAF:相对文件编号: 5
AAAAAM:块编号: 12
AAA:行编号: 0
A~Z 0~25
a~z 26~51
0~9 52~61
+/ 62,63
数据对象编号解释
select * from dba_objects where object_name='T01'
object_id: 10261, 对象id,不变
data_object_id: 10261,数据对象编号,可变,表移动时变
alter system dump datafile 5 block 9;
alter system dump datafile 5 block min 9 block max 10;
会话信息和进程信息查询
v$session: 记录会话信息
select * from v$session;
取当前会话sid
sid: session id
select * from v$mystat where rownum<2
取当前会话对应服务器进程地址
select paddr from v$session where sid=150;
v$process:记录进程信息
select spid from v$process where addr='6CE4C01C';
查询当前会话对应服务器进程 id
select spid from v$process where addr=(select paddr from v$session where sid=(select distinct sid from v$mystat))
监控CPU使用,跟踪会话
sqlplus system/1
declare
i number;
begin
loop
i:=i+1;
end loop;
end;
使用工具ProcessExplorer查CPU使用情况,找到进程id
根据进程id找到session
select sid,serial#,program,machine,sql_id from v$session where paddr=(select addr from v$process where spid=3412)
根据sql_id查看执行语句
select * from v$sql where sql_id='700dz3jf0nvrs';
杀掉session
alter system kill session '144,64';
查询表空间的剩余空间
select * from dba_free_space;
select * from dba_extents
select tablespace_name,sum(bytes) from dba_extents group by tablespace_name
select tablespace_name,sum(bytes) from dba_data_files group by tablespace_name
查询表空间使用情况
select f.tablespace_name,sum(f.bytes) total,sum(e.bytes) used
from dba_data_files f left join dba_extents e
on f.tablespace_name=e.tablespace_name
group by f.tablespace_name
2、undo表空间
create undo tablespace undo01
datafile 'E:\oracle\product\10.2.0\oradata\mydb\undo01.dbf'
size 100M;
SQL> show parameter undo
NAME TYPE VALUE
------------------------------------ ----------- ---------------
undo_management string AUTO
undo_retention integer 900
undo_tablespace string UNDOTBS1
alter system set undo_tablespace=undo01;
alter tablespace undo01 retention guarantee;
select * from dba_tablespaces
alter system set undo_tablespace=UNDOTBS1;
drop tablespace undo01 including contents and datafiles;
3、临时表空间
作用:order by,union ,distinct ,group by, 临时表
create temporary tablespace temp01
tempfile 'E:\oracle\product\10.2.0\oradata\mydb\temp001.dbf'
size 10M autoextend on next 1M;
select * from dba_temp_files
练习:
创建用户指定临时表空间
create user u1 identified by u1
default tablespace users temporary tablespace temp01
quota unlimited on users;
grant dba to u1;
登录
sqlplus u1/u1
create table t as select * from dba_objects;
insert into t select * from t;
select * from t order by ....;
观察临时表空间使用
select * from v$sort_usage
临时表空间组
drop tablespace temp01 including contents and datafiles;
drop tablespace temp02 including contents and datafiles;
create temporary tablespace temp01
tempfile 'E:\oracle\product\10.2.0\oradata\mydb\temp001.dbf'
size 10M autoextend on next 1M
tablespace group tempg;
create temporary tablespace temp02
tempfile 'E:\oracle\product\10.2.0\oradata\mydb\temp002.dbf'
size 10M autoextend on next 1M
tablespace group tempg;
select * from dba_tablespace_groups
练习
修改用户u1的临时表空间,指定到临时表空间组
alter user u1 temporary tablespace tempg;
登录排序
session1:
sqlplus u1/u1
select * from t order by ....;
session2:
sqlplus u1/u1
select * from t order by ....;
观察临时表空间使用
select * from v$sort_usage
4、表空间维护
修改:
增加文件
alter tablespace 表空间名 add datafile|tempfile '';
修改文件属性
alter database datafile 'a.dbf' autoextend on next 1M;
alter database datafile 'a.dbf' resize 10M;
删除
drop tablespace xxxx including contents and datafiles;
5、OMF-oracle manage file
SQL> show parameter file_dest
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_create_file_dest string
alter system set db_create_file_dest = 'E:\oracle\product\10.2.0\oradata\mydb';
create tablespace xxx;
drop tablespace xxx;
select * from dba_tablespaces;
select * from dba_data_files;