Tablespace

Tablespace

From Oracle FAQ

Jump to: navigation , search

<!-- start content -->

A tablespace is a container for segments (tables, indexes, etc). A database consists of one or more tablespaces, each made up of one or more data files . Tables and indexes are created within a particular tablespace.

Oracle has a limit of 64,000 data files per database.

Contents

[hide ]

<script type="text/javascript"> if (window.showTocToggle) { var tocShowText = &quot;show&quot;; var tocHideText = &quot;hide&quot;; showTocToggle(); } </script>

Default tablespaces

When a new database is created , it will have the following tablespaces (as created by the Database Configuration Assistant ):

  • SYSTEM (the data dictionary )
  • SYSAUX (optional database components)
  • TEMP (temporary tablespace, see tablespace types below)
  • UNDOTBS1 (undo tablespace, see tablespace types below)
  • USERS (default users tablespace created)

Tablespace types

Different tablespace types can be created for different purposes:

Permanent tablespaces

Permanent tablespaces are used to store user data and user created objects like tables, indexes and materialized views. Sample create statements:

CREATE TABLESPACE tools DATAFILE '/u01/oradata/orcl/tools/file_1.dbf' SIZE 100M;
CREATE TABLESPACE tools DATAFILE 'C:\ORA\tools01.dbf' SIZE 100M AUTOEXTEND ON MAXSIZE 500M;

  Temp tablespaces

Temp or temporary tablespaces are used to store data with short lifespan (transient data), for example: global temporarily tables or sort results.

CREATE TEMPORARY

 TABLESPACE temp TEMPFILE '/u01/oradata/orcl/temp/file_1.dbf' SIZE 100M;

With a single temp tablespace, the database will only write to one temp file at a time. However, Temporary tablespace groups , an Oracle 10g feature, can be created to allow Oracle to write to multiple temp files simultaneously.

Undo tablespaces

Undo tablespaces are used to store "before image" data that can be used to undo transactions. See ROLLBACK .

CREATE UNDO

 TABLESPACE undots DATAFILE '/u01/oradata/orcl/undo/file_1.dbf' SIZE 20M;

Assign tablespaces to users

Users cannot create objects in a tablespace (even it's their default tablespace) unless they have a quota on it (or UNLIMITED TABLESPACE privilege). Some examples:

Grant user scott access to use all space in the tools tablespace:

ALTER USER scott QUOTA UNLIMITED ON tools;

Prevent user scott from using space in the system tablespace:

ALTER USER scott QUOTA 0 ON system;

Check free/used space per tablespace

Example query to check free and used space per tablespace:

SELECT /* + RULE */  df.tablespace_name "Tablespace",
       df.bytes / (1024 * 1024) "Size (MB)",
       SUM(fs.bytes) / (1024 * 1024) "Free (MB)",
       Nvl(Round(SUM(fs.bytes) * 100 / df.bytes),1) "% Free",
       Round((df.bytes - SUM(fs.bytes)) * 100 / df.bytes) "% Used"
  FROM dba_free_space fs,
       (SELECT tablespace_name,SUM(bytes) bytes
          FROM dba_data_files
         GROUP BY tablespace_name) df
 WHERE fs.tablespace_name (+)  = df.tablespace_name
 GROUP BY df.tablespace_name,df.bytes
UNION ALL
SELECT /* + RULE */ df.tablespace_name tspace,
       fs.bytes / (1024 * 1024),
       SUM(df.bytes_free) / (1024 * 1024),
       Nvl(Round((SUM(fs.bytes) - df.bytes_used) * 100 / fs.bytes), 1),
       Round((SUM(fs.bytes) - df.bytes_free) * 100 / fs.bytes)
  FROM dba_temp_files fs,
       (SELECT tablespace_name,bytes_free,bytes_used
          FROM v$temp_space_header
         GROUP BY tablespace_name,bytes_free,bytes_used) df
 WHERE fs.tablespace_name (+)  = df.tablespace_name
 GROUP BY df.tablespace_name,fs.bytes,df.bytes_free,df.bytes_used
 ORDER BY 4 DESC;

Sample output:

Tablespace                      Size (MB)  Free (MB)     % Free     % Used
------------------------------ ---------- ---------- ---------- ----------
UNDOTBS1                               65    17.8125         27         73
EXAMPLE                               100     22.625         23         77
USERS                                   5     1.0625         21         79
TEMP                                   20          2         10         90
SYSAUX                            625.125       54.5          9         91
SYSTEM                                700     9.0625          1         99

 Best practices

  • Do not create objects in the SYSTEM tablespace. The system tablespace is reserved for the data dictionary .
  • Don't create tablespaces, with hundreds of small datafiles, these files needs to be checkpointed , resulting is unnecessary processing.
### 创建 Oracle 表空间的方法和示例 在 Oracle 数据库中创建表空间涉及定义表空间名称、指定数据文件的位置以及设置初始大小和其他参数。以下是详细的说明和示例。 #### 定义表空间名称 表空间的名称应具有唯一性和描述性,以便于管理和识别。例如: ```sql CREATE TABLESPACE my_tablespace; ``` #### 指定数据文件位置 需提供完整的路径来保存数据文件,并设定其初始大小。这可以通过 `DATAFILE` 子句完成。例如,在 Windows 系统上可以这样操作[^2]: ```sql CREATE TABLESPACE my_tablespace DATAFILE 'd:\oracle\oradata\dbname\my_tablespace.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED; ``` 此命令将在指定目录下创建名为 `my_tablespace.dbf` 的新数据文件,初始大小为 100MB 并允许自动扩展至无限大。 #### 设置其他属性 还可以配置更多选项如段管理方式(LOCAL 或 DICTIONARY)、日志记录模式等。对于大多数情况,默认值已足够适用: ```sql CREATE TABLESPACE my_tablespace DATAFILE 'd:\oracle\oradata\dbname\my_tablespace.dbf' SIZE 100M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED SEGMENT SPACE MANAGEMENT AUTO LOGGING; ``` 上述语句不仅设置了基本的数据文件信息,还启用了本地化段管理和日志功能以提高性能并简化维护工作。 #### 授权用户访问新的表空间 一旦成功建立了所需的表空间,则需要将其分配给特定数据库账户使用,并授予必要的权限才能让这些账号能够在此区域内创建对象。比如向某位用户赋予默认表空间及相应权利可如下执行: ```sql ALTER USER username DEFAULT TABLESPACE my_tablespace; GRANT CREATE SESSION, CREATE TABLE TO username WITH ADMIN OPTION; ``` 通过以上步骤即可顺利完成一个自定义表空间及其关联用户的建立过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值