一个查看Oracle表和表空间存储分配的示例
Posted dingdingfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个查看Oracle表和表空间存储分配的示例相关的知识,希望对你有一定的参考价值。
从以下输出可知,在当前schema中,空间占用约43GB,使用的是users表空间。
SQL> show parameter db_block_size
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_block_size integer 8192
col segment_name for a20
col tablespace_name for a16
select segment_name, tablespace_name, bytes/1024/1024, blocks*8/1024 from user_segments;
SEGMENT_NAME BYTES/1024/1024 BLOCKS*8/1024
-------------------- --------------- -------------
CUSTOMER 30 30
DATE_DIM .375 .375
LINEORDER 42795 42795
PART 104 104
SUPPLIER 2 2
SQL> select sum(bytes)/1024/1024, sum(blocks)*8/1024 from user_segments;
SUM(BYTES)/1024/1024 SUM(BLOCKS)*8/1024
-------------------- ------------------
42931.375 42931.375
再来看下extent的情况,可知在当前schema有5个table segment:
set pages 9999
select segment_type, count(1) from dba_segments group by segment_type order by segment_type;
SEGMENT_TYPE COUNT(1)
------------------ ----------
CLUSTER 10
INDEX 774
INDEX PARTITION 29
LOB PARTITION 28
LOBINDEX 400
LOBSEGMENT 400
NESTED TABLE 11
ROLLBACK 1
SYSTEM STATISTICS 1
TABLE 590
TABLE PARTITION 42
TYPE2 UNDO 10
12 rows selected.
SQL> select segment_type, count(1) from user_segments group by segment_type order by segment_type;
SEGMENT_TYPE COUNT(1)
------------------ ----------
TABLE 5
看一下分布详情,注意这里用user_extents是不行的:
select tablespace_name, file_id, extent_id, block_id, blocks, bytes from dba_extents where owner='SSB' and segment_name='LINEORDER';
TABLESPACE_NAME FILE_ID EXTENT_ID BLOCK_ID BLOCKS BYTES
---------------- ---------- ---------- ---------- ---------- ----------
USERS 12 0 3923248 8 65536
USERS 12 1 3923256 8 65536
USERS 12 2 3923264 8 65536
USERS 12 3 3923272 8 65536
...
USERS 12 850 721408 8192 67108864
USERS 18 851 1368576 8192 67108864
USERS 17 852 1368576 8192 67108864
USERS 16 853 1368576 8192 67108864
854 rows selected.
输出太多,我们简化一下,可知lineorder表均匀分布在users表空间的4个数据文件上:
SQL> select sum(blocks)*8/1024, sum(bytes)/1024/1024 from dba_extents where owner='SSB' and segment_name='LINEORDER';
SUM(BLOCKS)*8/1024 SUM(BYTES)/1024/1024
------------------ --------------------
42795 42795
SQL> select tablespace_name, file_id, sum(blocks)*8/1024, sum(bytes)/1024/1024 from dba_extents where owner='SSB' and segment_name='LINEORDER' group by tablespace_name, file_id;
TABLESPACE_NAME FILE_ID SUM(BLOCKS)*8/1024 SUM(BYTES)/1024/1024
---------------- ---------- ------------------ --------------------
USERS 12 10621.625 10621.625
USERS 16 10746 10746
USERS 18 10737.375 10737.375
USERS 17 10690 10690
再来看一下表空间情况:
set lines 140
col file_name for a100
select tablespace_name,file_name from dba_data_files where file_id in
(select distinct(file_id) from dba_extents where owner='SSB' and segment_name='LINEORDER');
TABLESPACE_NAME FILE_NAME
---------------- ----------------------------------------------------------------------------------------------------
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.274.1115978849
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.286.1116135705
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.287.1116135705
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.285.1116135699
users表空间每个数据文件多大?数据文件的大小并不表示数据的大小。因为users表空间的4个数据文件合计105GB,但当前schema的5张表使用空间为43GB。
col tsname for a16
col filename for a100
select t.name tsname, d.name filename, d.bytes/1024/1024 from v$tablespace t join v$datafile d using(ts#) union all select t.name,d.name, d.bytes from v$tablespace t join v$tempfile d using(ts#);
TSNAME FILENAME D.BYTES/1024/1024
---------------- ---------------------------------------------------------------------------------------------------- -----------------
SYSTEM +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/system.273.1115978715 400
SYSAUX +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/sysaux.270.1115978723 520
UNDOTBS1 +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/undotbs1.275.1115978855 1485
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.274.1115978849 32767.5
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.285.1116135699 24700
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.286.1116135705 23600
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.287.1116135705 24700
TEMP +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/TEMPFILE/temp.272.1115978713 4293918720
8 rows selected.
SQL> select t.name tsname, d.name filename, d.bytes/1024/1024 from v$tablespace t join v$datafile d using(ts#);
TSNAME FILENAME D.BYTES/1024/1024
---------------- ---------------------------------------------------------------------------------------------------- -----------------
SYSTEM +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/system.273.1115978715 400
SYSAUX +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/sysaux.270.1115978723 520
UNDOTBS1 +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/undotbs1.275.1115978855 1485
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.274.1115978849 32767.5
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.285.1116135699 24700
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.286.1116135705 23600
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.287.1116135705 24700
7 rows selected.
SQL> select t.name tsname, d.name filename, d.bytes/1024/1024 from v$tablespace t join v$datafile d using(ts#) where t.name = 'USERS';
TSNAME FILENAME D.BYTES/1024/1024
---------------- ---------------------------------------------------------------------------------------------------- -----------------
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.274.1115978849 32767.5
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.285.1116135699 24700
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.286.1116135705 23600
USERS +DATA/DB0921_NRT1MZ/E92E139443C62209E0539B00000AFD03/DATAFILE/users.287.1116135705 24700
SQL> select t.name tsname, sum(d.bytes)/1024/1024 from v$tablespace t join v$datafile d using(ts#) where t.name = 'USERS' group by t.name;
TSNAME SUM(D.BYTES)/1024/1024
---------------- ----------------------
USERS 105767.5
从以下输出也可以印证,MAXBYTES表示数据文件最大为32GB,BYTES表示当前数据文件的大小,也就是高水位线,但并不表示数据量。
SQL> select file_id, tablespace_name, bytes/1024/1024, maxbytes/1024/1024 from dba_data_files where tablespace_name = 'USERS';
FILE_ID TABLESPACE_NAME BYTES/1024/1024 MAXBYTES/1024/1024
---------- ------------------------------ --------------- ------------------
12 USERS 32767.5 32767.9844
16 USERS 24700 32767.9844
17 USERS 23600 32767.9844
18 USERS 24700 32767.9844
从以下输出可知,users表空间为smallfile表空间。
SQL> select tablespace_name, bigfile, max_extents, max_size, segment_space_management from dba_tablespaces where tablespace_name = 'USERS';
TABLESPACE_NAME BIG MAX_EXTENTS MAX_SIZE SEGMEN
---------------- --- ----------- ---------- ------
USERS NO 2147483645 2147483645 AUTO
SQL> select 2147483645*8/1024/1024 from dual;
2147483645*8/1024/1024
----------------------
16384
以上的MAX_SIZE指的是“Default maximum size of segments (in Oracle blocks)”。也就是16GB。
按照这里的描述:
A smallfile tablespace is a traditional Oracle tablespace, which can contain 1022 data files or temp files, each of which can contain up to approximately 4 million (2^22) blocks.
即每个表空间最多1022个数据文件,每个数据文件32GB。即smallfile表空间最大约32GB。bigfile表空间在block大小为8K时最大是32TB,但表空间只能有1个数据文件,因此管理会简便些。
如何看表空间的剩余空间?可以用DBA_FREE_SPACE。
select tablespace_name,
sum(bytes)/1024/1024/1024 tot_free, sum(blocks),
count(*) chunks
from dba_free_space
group by tablespace_name ;
TABLESPACE_NAME TOT_FREE SUM(BLOCKS) CHUNKS
---------------- ---------- ----------- ----------
SYSTEM .002502441 328 2
SYSAUX .032958984 4320 2
UNDOTBS1 .630065918 82584 635
USERS 61.3594971 8042512 30
select tablespace_name, file_id,
bytes/1024/1024 free_mb, blocks
from dba_free_space where tablespace_name = 'USERS';
TABLESPACE_NAME FILE_ID FREE_MB BLOCKS
---------------- ---------- ---------- ----------
USERS 12 3 384
USERS 12 3 384
USERS 12 2237 286336
USERS 12 3968 507904
USERS 12 3968 507904
USERS 12 3968 507904
USERS 12 3968 507904
USERS 12 3968 507904
USERS 12 1 128
USERS 12 3 384
USERS 12 .5 64
USERS 16 3 384
USERS 16 1149 147072
USERS 16 3968 507904
USERS 16 3968 507904
USERS 16 3968 507904
USERS 16 891 114048
USERS 17 3 384
USERS 17 3 384
USERS 17 1149 147072
USERS 17 3968 507904
USERS 17 3968 507904
USERS 17 3759 481152
USERS 18 .625 80
USERS 18 3 384
USERS 18 1149 147072
USERS 18 3968 507904
USERS 18 3968 507904
USERS 18 3968 507904
USERS 18 891 114048
30 rows selected.
select file_id,
sum(bytes)/1024/1024 free_mb, sum(blocks)
from dba_free_space where tablespace_name = 'USERS' group by file_id;
FILE_ID FREE_MB SUM(BLOCKS)
---------- ---------- -----------
12 22087.5 2827200
17 12850 1644800
18 13947.625 1785296
16 13947 1785216
现在整个数据就可以说得通了:
- 当前schema5张表合计使用空间 42931.375 MB = 41.92517 GB
- users表空间的4个数据文件合计105767.5 MB = 103.28857GB
- users表空间剩余空间 61.3594971GB
前2项之差为61.363404GB。
参考
- https://connor-mcdonald.com/2020/08/27/finding-free-space-on-your-database-taking-a-long-time/
- OCA/OCP Oracle 数据库12c考试指南读书笔记:第15章: Oracle Storage
以上是关于一个查看Oracle表和表空间存储分配的示例的主要内容,如果未能解决你的问题,请参考以下文章