Detailed Oracle table space (tablespace)

1 Overview

Draw the Oracle storage model in the form of an entity-relationship diagram, with the logical structure on the left and the physical structure on the right.
Insert picture description here

  • There is a relationship drawn with a dotted line, which represents a many-to-many relationship between segments and data files. The dashed line is used to indicate the relationship because the relationship should not exist. A good relationship engineer should not allow many-to-many relationships.
  • The table space entity eliminates the many-to-many relationship between the segment and the data file. A table space can contain multiple segments, and can be composed of multiple data files.

hint:

Creating tableand indexwhen we must bring属主(对应用户的表空间)

2 Grammar

2.1 Create

  • Table space creator must have create tablespacesystem privileges
  • When creating a table space, there are many default/optional clauses, as long as we know the most commonly used clauses.
  • For a very complete one, please refer to: Oracle 12.2 Official Document-create tablespace

Common syntax: where [] means the parameter is optional

create [temporary | undo] tablespace "tbs"
datafile 'D:\oracle\tbs_01.dbf'
size 100m
[loggin | nologgin] -- 默认 loggin
[autoextend off] | [autoextend on next n1 maxsize m] -- 自动扩展, 默认 off, n1 默认 , m 默认 unlimited <= size n
[extent management local [uniform size n]] -- 默认 local, n = 64K;
[segment space management auto] -- 默认 auto, 且不建议被修改

-- 创建新用户,使用该表空间,方便测试
create user ODSDATA identified by 12345 default tablespace tbs
temporary tablespace temp quota unlimited on odstbs;
grant create session to ODSDATA;
grant resource to ODSDATA;

Syntax analysis:

1. create tablespace tbs 创建一个名为 "tbs" 的表空间(最常用)
	(1) temporary: 临时表空间,用于临时数据的存放
	(2) undo: 撤销表空间,用于重做日志文件的存放

2. datafile 'D:\oracle\tbs_01.dbf' size 5m 指定数据文件的位置和大小
	(1) 数据文件位置: "D:\oracle\tbs_01.dbf" -- 推荐使用绝对路径
	(2) 数据文件大小: 5m
	(3) 如果有多个文件,可以用逗号 , 隔开. 但是每个每个文件都要指明大小
		
3. loggin | nologgin 默认是 loggin,一般不写,但你要知道
	(1) loggin: 创建表空间时,同时生成重做日志文件(数据丢失时,可以恢复)
	(2) nologgin: 与 loggin 相反,但表创建速度快

4. autoextend 表空间是否自动扩展
	(1) off: 不自动扩展,默认值
	(2) autoextend on next 10m maxsize 100m
		自动扩展,增量是 10m,最大大小是 100m
		
5. extent management local | dictionary 区管理
	(1) local 本地管理,强烈推荐,从 Oracle 9i 开始,只能用 local,效率高
	(2) dictionary 数据字典管理,以前的管理方式。
		将数据文件中的每一个存储单元做为一条记录,在大量数据管理时,会产生大量的 insertupdate 操作,严重影响性能。
		同时,长时间对表数据的操作,会产生很多磁盘碎片,这就是为什么要做磁盘整理的原因。

6. uniform size n: 指定 extent 的固定大小
    (1) size 8m: 固定为 8m
    (2) 若不填,extent 默认 64k (参考: dba_tablespaces 中的 initial_extent)

7. segment space management auto | manual
	(1) auto: 段空间由系统自动管理,只能使用在本地管理的表空间中(lob 字段的表除外)
	(2) manual: 段空间手动管理,目前已不用,主要是为了向后兼容

After we have created it, we can also query the parameters set above through the data dictionary:

SELECT ddf.file_id,
       ddf.file_name, -- 数据文件名称 datefile 'fileName'
       dt.tablespace_name, -- 表空间名称 tablespace tbs
       ddf.bytes / 1024 / 1024 || 'm' size_n, -- 初始数据文件大小,也就是 size n
       dt.logging, -- 日志属性 loggin
       ddf.autoextensible, -- 自动扩展
       dt.extent_management, -- extent management local | dictionary
       dt.allocation_type, -- system 长度自适应 | uniform 长度固定
       dt.segment_space_management, -- segment space management auto | manual
       dt.next_extent / 1024 / 1024 || 'm' next_n -- 数据文件增量大小,也就是 next n (当 allocata_type = 'uniform' 时,才有值)
  FROM dba_tablespaces dt, -- tablespace_name
       dba_data_files  ddf -- file_name, file_id, tablespace_name 
 WHERE ddf.tablespace_name = dt.tablespace_name
   AND dt.tablespace_name IN ('');

2.2 Modification

  • The built-in table space of the system is not allowed to be modified, such as:system、sysaux、temp、undo、users
  • The corresponding data file, location, and size have not changed after the table space name is modified
  • A table space that is offline cannot modify its name
1. 调整数据文件的大小
alter database datafile 'D:\oracle\tbs01_1.dbf' resize 10m;

2. 调整数据文件为自动扩展
alter database datafile 'D:\oracle\tbs01_1.dbf' autoextend on next 20m maxsize 1g; -- 增量 20m, 最大长度 1g

3. 新增数据文件
alter database datafile 'D:\oracle\tbs02_1.dbf' size 2000m; -- 不自动扩展
alter database datafile 'D:\oracle\tbs02_1.dbf' autoextend on next 20m maxsize 1g; -- 自动扩展

2.3 Delete

  • Any table space of the Oracle database can be deleted (except system、sysaux、temp、undo、users)
  • Once the table space is deleted, it cannot be restored, so it is best to backup before deleting the table space
  • You cannot delete a tablespace containing any active segments, you should make the tablespace offline before being deleted (very simple, do not go offline or close the running PL/SQL, deleting the tablespace will report an error)
  • User to delete the table space must have drop tablespacepermission
1. 删除空的表空间(数据文件还在)
drop tablespace tbs;

2. 删除表空间 和 数据文件
drop tablespace tbs including contents and datafiles;

3. 仅从数据字典中删除表空间信息,但数据文件仍然存在,但已经失去作用
drop tablespace tbs including contents;

2.4 Query

Query table space information: (if there are multiple data files, group summation is required)

SELECT ddf.file_id 文件id,
       ddf.file_name 文件名,
       dt.tablespace_name 表空间名,
       dfs.block_id 块id,
       dt.contents 表空间类型,
       dt.extent_management 表空间管理模式,
       dt.segment_space_management 块空间管理模式,
       dt.allocation_type 分配类型, -- "system 系统随机" or "uniform 固定"
       dt.logging 日志模式,
       dt.force_logging 是否强制日志, -- 优先级 > 日志模式
       ddf.autoextensible 是否自动扩展,
       ddf.bytes / 1024 / 1024 "表空间初始大小(M)", -- 单位由 字节 -> MB
       ddf.maxbytes / 1024 / 1024 "表空间最大扩展到多少(M)", -- 0 表示自动扩展: off
       ddf.bytes / 1024 / 1024 "总大小(M)",
       dfs.bytes / 1024 / 1024 "剩余大小(M)",
       (1 - dfs.bytes / ddf.bytes) * 100 "使用率%"
  FROM dba_tablespaces dt, -- tablespace_name 
       dba_data_files  ddf, -- file_name, file_id, tablespace_name 
       dba_free_space  dfs -- tablespace_name, file_id, block_id 
 WHERE ddf.tablespace_name = dt.tablespace_name
   AND dfs.tablespace_name = ddf.tablespace_name
   AND dfs.file_id = ddf.file_id
   AND ddf.tablespace_name IN ('表空间名');

3 example

3.1 Create an automatic extension table space

Create a 100M database file student.dbf permanent table space tbs_student, automatically expand every 10M, the maximum is 500M

CREATE TABLESPACE tbs_student 
DATAFILE 'D:\table_space\student.dbf'
SIZE 100m
AUTOEXTEND ON NEXT 10M MAXSIZE 500M -- 自动扩展, 每次扩展 10M, 最大扩展到 500M
PERMANENT -- 永久表空间,默认
EXTENT MANAGEMENT LOCAL;

Query the table space through the data dictionary:

SELECT dt.tablespace_name 表空间名,
       dt.contents 表空间类型,
       dt.extent_management 表空间管理模式, -- "LOCAL 默认,本地" or "DICTIONARY 数据字典"
       dt.segment_space_management 段空间管理模式, -- "AUTO 默认,自动管理" or "MANUAL 手动管理"
       dt.logging 默认日志属性, -- "LOGGING 默认" OR "NOLOGGING"
       dt.allocation_type 分配类型, -- "SYSTEM 系统自动分配" or "UNIFORM 统一分配"
       dt.next_extent 默认增量, -- next, 当 allocation_type = UNIFORM 时,才有值
       ddf.file_id 文件id,
       ddf.file_name 文件名,
       ddf.autoextensible 是否自动扩展,
       ddf.bytes / 1024 / 1024 "表空间初始大小 M", --,单位
       ddf.maxbytes / 1024 / 1024 "表空间最大扩展到多少 M"
  FROM dba_tablespaces dt, -- tablespace_name
       dba_data_files  ddf -- file_id  
 WHERE ddf.tablespace_name = dt.tablespace_name
   AND ddf.tablespace_name = 'TBS_STUDENT';

search result:
Insert picture description here

3.2 The table cannot be expanded in the table space

the reason:

  • The table space is full!
    • Table space automatic expansion is not set
    • Or set the table space automatic expansion, but exceeded MAXSIZE

Test user preparation

-- 表空间创建语句
CREATE TABLESPACE "TESTTBS_02" DATAFILE 
'D:\TABLE_SPACE\TESTTBS_02.DBF' SIZE 1m
AUTOEXTEND ON NEXT 1m MAXSIZE 2m
EXTENT MANAGEMENT LOCAL 
SEGMENT SPACE MANAGEMENT AUTO;

-- 测试用户
CREATE USER test_tablespace IDENTIFIED BY 12345 DEFAULT TABLESPACE TESTTBS_02;
GRANT CREATE SESSION TO test_tablespace; -- 允许登录
GRANT RESOURCE TO test_tablespace; -- 不对用户表空间进行限制

Test statement:

CREATE TABLE test_tablespace.stu_info (
  ID   NUMBER(10),
  NAME VARCHAR2(30)
);
DECLARE
   v_sql_insert VARCHAR2(500);
BEGIN
   -- 模拟插入语句
   v_sql_insert := 'INSERT INTO test_tablespace.stu_info (id, NAME) VALUES (:b1, :b2)';

   FOR i IN 1 .. 100000 LOOP
      EXECUTE IMMEDIATE v_sql_insert
         USING i, 'a' || i;
   END LOOP;
END;

Error screenshot:
Insert picture description here

4 extension

Reference sentence:

1. 修改表空间名
ALTER TABLESPACE old_name RENAME TO new_name;

2. 设置数据库的默认表空间
ALTER DATABASE DEFAULT TABLESPACE mytbs;

3. 设置数据库的默认临时表空间
ALTER DATABASE TEMPORARY TABLESPACE temptbs;

4. 查询数据库的默认表空间
SELECT t.username, t.default_tablespace, t.temporary_tablespace FROM dba_users t;

5. 允许数据文件自动扩展
ALTER DATABASE DATAFILE 'D:\oracle\tbs01_1.dbf' AUTOEXTEND ON NEXT 10m MAXSIZE 100m;

6. 使用表空间脱机/联机 (有效/无效)
ALTER TABLESPACE tbs01 ONLINE; -- 联机、有效
ALTER TABLESPACE tbs01 OFFLINE; -- 脱机、无效

7. 使表空间只读(仍可执行 drop 删除对象操作)
ALTER TABLESPACE tbs01 READ ONLY;

8. 使表空间可读写(脱机状态的表空间不能修改为读写状态)
ALTER TABLESPACE tbs01 READ WRITE;

4.1 Frequently Asked Questions

1. It is auto-expanding, why does it prompt that there is insufficient space?

AUTOEXTEND ON NEXT 1m MAXSIZE 2m

2. What is the default next value of autoextend on?

  • When allocation_type = SYSTEMtime, next unified value assigned by the system, is not fixed
  • When allocation_type = UNIFORMtime, next value is fixed, the following
extent management local uniform size 8m -- next = 8m

4.2 Related knowledge points

Guess you like

Origin blog.csdn.net/qq_34745941/article/details/91039626