oracle数据库(十)_表空间

表空间

概述
  • 数据库的最高逻辑层
  • 应该根据具体应用情况,建立不同类型的表空间,用以提升效率。例如专门存放表数据或索引
  • 逻辑结构:数据库,表空间,段,盘区,数据块
  • 物理结构:数据文件,操作系统块
默认表空间
  • example:存放个样例
  • sysaux:syste表空间的辅佐空间,存放数据字典以外的数据对象,减少system表空间的负荷
  • system:存放数据字典,表,视图,存储过程
  • temp:存放临时表数据,比如排序占用空间
  • undotbs1:存放撤销数据,用于回滚,闪回
  • users:存放“应用系统”所使用的的数据库对象
创建及维护表空间
-- 创建表空间,自动扩张,自动空间管理
create tablespace tbs datafile 'c:\tbs.dbf' 
size 10m 
extent management local autoallocate
segment space management auto;

-- 修改传统表空间,可能包含多个数据文件
alter database datafile 'c:\tbs.dbf'
resize 100m;

-- 建立大文件表空间
create bigfile tablespace tbs_big datafile 'c:\tbs_big.dbf'
size 2g;

-- 修改大文件表空间大小
alter tablespace tbs_big resize 1g;

-- 设置默认的临时表空间
alter database default temprory tablespace tbs_temp;

-- 设置默认的永久表空间
alter database default tablespace tbs;

-- 改变表空间状态
alter tablespace tbs read only;
alter tablespace tbs read write;

-- 重命名表空间
alter tablespace tbs rename to tbs_new;

-- 删除表空间
drop  tablespace tbs
including contents -- 忽略数据存在
cascade constraints; -- 忽略完整性限制

-- 向表空间添加数据文件
alter tablespace users add datafile 'c:\tbs.dbf' maxsize unlimited;

-- 从表空间删除数据文件
alter tablespace users drop datafile 'c:\tbs.dbf';

-- 查需表空间数据文件是否自动扩张
col file_name for a50;
select file_name,autoextensible from dba_data_files where tablespace_name='TBS';

-- 设置数据文件自动扩张
alter database datafile 'c:\tbs.dbf'
autoextend on next 10m maxsize unlimited;

撤销表空间

作用
  • 读写一致:未commit的修改,其他用户读取时,读取undo表空间的数据
  • 支持回滚:执行rollback,根据uodo的记录,取消当前会话所有的事务修改
  • 事务恢复:事务恢复,如果断电或内存故障,重启oracle时smon进程会回滚未提交事务
  • 闪回操作:通过undo实现的数据恢复功能
使用
-- 查看例程正在使用的undo表空间
show parameter undo_tablespace;

-- 查看undo的管理模式
show parameter undo_management;

-- 查看undo数据最大保留时间
show parameter undo_retention;

-- 创建undo表空间
create undo tablespace undo_tbs datafile 'c:\undotbs.dbf'
size 100m;

-- 向表空间undo_tbs添加新的数据文件,设置文件大小为2g
alter tablespace undo_tbs add datafile 'c:\undotbs_add.dbf'
size 2g;

-- 设定例程使用的undo表空间
alter system set undo_tablespace=undo_tbs;

-- 查询当前实例拥有的所有undo表空间
select tablespace_name from dba_tablespaces where contents='UNDO';

-- 删除undo表空间
alter systemset undo_tablespace=undotbs1;
drop tablespace undo_tbs;

-- 查询undo回退块的生产信息
select to_char(begin_time,'yyyy-mm-dd hh24:mi:ss') 开始时间
	,to_char(end_time,'yyyy-mm-dd hh24:mi:ss') 结束时间
	,undoblks 回退块数
from v$undostat
order by 开始时间;

-- 动态性能视图监视特定undo段的信息
select rn.name,rs.xacts,rs.writes,rs.extents
from v$rollname rn,v$rollstat rs
where rn,usn = rs.usn;

-- 查询动态性能视图显示事务名称和状态
select name,status from v$transaction;

-- 通过数据字典查询段信息,段号,段大小,段状态
select segment_name,extent_id,bytes,status 
from dba_undo_extents
where segment_name = '_SYSSMU3_991555123$';
误删闪回
-- 查看是否开启回收站
select value from v$parameter where name = 'recyclebin';

-- 开启回收站
ALTER SYSTEM SET recyclebin = ON;     开启系统回收站
ALTER SESSION SET recyclebin = ON;    开启当前连接回收站
ALTER SYSTEM SET recyclebin = OFF;    关闭系统回收站
ALTER SESSION SET recyclebin = OFF;    关闭当前连接回收站

-- 新建表并删除
create table test(
       id number(4),
       value varchar2(10)
);
insert into test values(1,'zzz');
insert into test values(2,'bbb');
select * from test;

-- 查看回收站
select * from dba_recyclebin where owner = 'SCOTT' and original_name = 'TEST';

-- 闪回前一个工作点
FLASHBACK table scott.test to before drop; 
FLASHBACK table “BIN$5ZRRkUZVQLapP71eoBBN0g==$0to before drop;
select * from SCOTT.test;

临时表空间

概述
  • 用于内存排序区不够而必须将数据写到磁盘的逻辑区域
  • 用的的情形:distinct,union,minus,analyze分析,链接两个没有索引的表;
使用
-- 创建临时表空间
create temporary tablespace temp tempfile 'c:\temp.tpf' size 300m;

-- 设置默认临时表空间
alter database default temporary tablespace temp;

-- 查询临时表信息,临时表文件,空间大小,空间名称
col file_name for a40;
col tablespace_name for a10;
select file_name,bytes,tablespace_name from dba_temp_files;
临时表空间组
--  创建2个临时表空间,并把它们归组
create temporary tablespace tp1 tempfile 'c:\temp1.tpf' size 10m tablespace group group1;
create temporary tablespace tp2 tempfile 'c:\temp2.tpf' size 10m tablespace group group1;

-- 创建1个临时表空间,并把它们归组
create temporary tablespace tp3 tempfile 'c:\temp3.tpf' size 10m tablespace group group2;

-- 改变临时表空间所属组
alter tablespace tp1 tablespace group group2;

-- 把表空间组分配给hr用户使用
alter user hr temporary tablespace group2;

-- 修改数据库默认表空间组
alter database orcl default temporary tablespace group2;

-- 删除表空间组(删除组成的所有临时表空间)
drop tablespace tp1 including contents and datafiles;
drop tablespace tp3 including contents and datafiles;

-- 通过数据字典查看表空间组是否存在
select * from dba_tablespace_groups where group_name='GROUP2';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值