Oracle extent、segment、datafile、tablespace及存储关系

1、创建测试表空间

create tablespace efmis datafile '/u01/app/oracle/oradata/orcl/efmis_01.dbf' size 80m;

alter tablespace efmis add datafile '/u01/app/oracle/oradata/orcl/efmis_02.dbf' size 80m;

alter tablespace efmis add datafile '/u01/app/oracle/oradata/orcl/efmis_03.dbf' size 80m;

SQL> select t.tablespace_name,t.file_id,t.file_name,t.bytes/1024/1024/1024||'G' from dba_data_files t where t.tablespace_name='EFMIS';

TABLESPACE_NAME FILE_ID FILE_NAME T.BYTES/1024/1024/1024||'G'


EFMIS 5 /u01/app/oracle/oradata/orcl/efmis_01.dbf .078125G

EFMIS 6 /u01/app/oracle/oradata/orcl/efmis_02.dbf .078125G

EFMIS 7 /u01/app/oracle/oradata/orcl/efmis_03.dbf .078125G

2、创建测试用户

create user fasp identified by 1 default tablespace efmis;

grant connect,resource to fasp;

3、创建测试表并插入数据

create table fasp.tab1 as select * from dba_objects;

4、查看dba_extents及dba_segments数据相关操作

SQL> select t.tablespace_name,t.segment_name,t.bytes/1024/1024 from dba_segments t where t.owner='FASP';

TABLESPACE_NAME SEGMENT_NAME T.BYTES/1024/1024


EFMIS TAB1 10

SQL> select file_id,sum(bytes)/1024/1024 from dba_extents t where t.owner='FASP' group by file_id;

FILE_ID SUM(BYTES)/1024/1024


6 4

5 3

7 3

SQL> select count(*) from fasp.tab1;

COUNT(*)


86268

SQL> insert into fasp.tab1 select * from fasp.tab1;

86268 rows inserted

SQL> insert into fasp.tab1 select * from fasp.tab1;

172536 rows inserted

SQL> select t.tablespace_name,t.segment_name,t.bytes/1024/1024 from dba_segments t where t.owner='FASP';

TABLESPACE_NAME SEGMENT_NAME T.BYTES/1024/1024


EFMIS TAB1 40

SQL> select file_id,sum(bytes)/1024/1024 from dba_extents t where t.owner='FASP' group by file_id;

FILE_ID SUM(BYTES)/1024/1024


6 14

5 13

7 13

SQL> delete from fasp.tab1;

345072 rows deleted

SQL> commit;

Commit complete

SQL> select count(*) from fasp.tab1;

COUNT(*)


0

SQL> select t.tablespace_name,t.segment_name,t.bytes/1024/1024 from dba_segments t where t.owner='FASP';

TABLESPACE_NAME SEGMENT_NAME T.BYTES/1024/1024


EFMIS TAB1 40

SQL> select file_id,sum(bytes)/1024/1024 from dba_extents t where t.owner='FASP' group by file_id;

FILE_ID SUM(BYTES)/1024/1024


6 14

5 13

7 13

5、结论:

1)tab1表在dba_segments中共1个段,占据10m存储空间。

2)tab1表在dba_extents中分成多个区,总共占10m存储空间。

3)oracle数据库中segment由extent逻辑构成。

4)tab1插入的数据按照hash算法均为分布在所属表空间数据文件下,数据文件创建后不可随意删除,否则将导致部分数据丢失。

5)段相关数据字典:dba_segments,user_segments

区相关数据字典:dba_extents,user_extents

6)dba_segments及dba_extents存储至高水位线空间大小,delete删除数据并不会降低高水位线值。

7)可以通过dba_segments视图查看table及index等数据库对象占用存储空间大小,此时值为起点至高水位线值大小。

6、查看表空间使用率

select a.tablespace_name,

round((a.maxbytes / 1024 / 1024 / 1024), 2) "sum G",

round((a.bytes / 1024 / 1024 / 1024), 2) "datafile G",

round(((a.bytes - b.bytes) / 1024 / 1024 / 1024), 2) "used G",

round(((a.maxbytes - a.bytes + b.bytes) / 1024 / 1024 / 1024), 2) "free G",

round(((a.bytes - b.bytes) / a.maxbytes) * 100, 2) "percent_used(%)"

from (select tablespace_name, sum(sumnow) bytes, sum(summax) maxbytes

from (select t1.tablespace_name,

sum(t1.bytes) sumnow,

sum(t1.maxbytes) summax

from dba_data_files t1

where t1.maxbytes <> 0

group by t1.tablespace_name

union all

select t2.tablespace_name,

sum(t2.bytes) sumnow,

sum(t2.bytes) summax

from dba_data_files t2

where t2.maxbytes = 0

group by t2.tablespace_name)

group by tablespace_name) a,

(select tablespace_name, sum(bytes) bytes

from dba_free_space

group by tablespace_name) b

where a.tablespace_name = b.tablespace_name

order by ((a.bytes - b.bytes) / a.maxbytes) desc;

TABLESPACE_NAME sum G datafile G used G free G percent_used(%)


EFMIS 0.23 0.23 0.04 0.19 17.92

SYSTEM 32 0.82 0.81 31.19 2.52

SYSAUX 32 0.54 0.51 31.49 1.59

UNDOTBS1 32 0.35 0.13 31.87 0.41

USERS 32 0 0 32 0

SQL> select t.tablespace_name,t.file_name,t.file_id,t.bytes/1024/1024,t.autoextensible,t.maxbytes from dba_data_files t where t.tablespace_name='EFMIS';

TABLESPACE_NAME FILE_NAME FILE_ID T.BYTES/1024/1024 AUTOEXTENSIBLE MAXBYTES


EFMIS /u01/app/oracle/oradata/orcl/efmis_01.dbf 5 80 NO 0

EFMIS /u01/app/oracle/oradata/orcl/efmis_02.dbf 6 80 NO 0

EFMIS /u01/app/oracle/oradata/orcl/efmis_03.dbf 7 80 NO 0

7、结论

1)表空间数据文件未设置自动增长,则数据文件最大值(maxbytes)为0,此时数据文件最大值为bytes值大小。

2)表空间由数据文件物理构成。

相关推荐
Elastic 中国社区官方博客17 分钟前
让大模型思考,让小模型执行:在 Elastic Workflows 中拆分 LLM 成本
大数据·运维·数据库·人工智能·elasticsearch·ai
Faith_xzc31 分钟前
一条 SQL 顶一条 Flink 链路?Doris Streaming Job 持续导入全景解析
大数据·数据库·sql·flink
这个DBA有点耶3 小时前
多模数据库深度解读:从“多库拼装”到“一库多能”的架构演进
数据库·mysql·dba
程序员夏洛4 小时前
MySQL 默认的事务隔离级别是什么?为什么选择这个级别?
数据库·mysql
艺术留白4 小时前
MokaTest使用篇-SQL接口
数据库·sql
( •̀∀•́ )9205 小时前
MySQL 自动备份与恢复方案
数据库·mysql·oracle
这个DBA有点耶5 小时前
大事务的“事前预防”:监控、拦截、Kill,四层防线一次讲透
数据库·mysql·代码规范
tachibana25 小时前
RAGAS 指标解读
数据库·人工智能·算法·机器学习·架构·大模型·llm
mlidongfeng6 小时前
[AI][昇腾950]TA 学习
数据库·学习
bestsun9996 小时前
rac集群环境下配置JDBC
数据库