oracle-- table space usage query and maintenance

        An Oracle tablespace is a logical storage structure in an Oracle database for storing tables, indexes, views, and other database objects in the database. Table space is an important concept of Oracle database management, it can help administrators and developers to effectively manage and organize data in the database.

The following is the query script for table space usage:

SELECT
/*+ parallel (8) */
	df.tablespace_name "表空间名",
	totalspace "总空间GB",
	totalspace - freespace "已使用空间GB",
	freespace "剩余空间G",
	round( ( 1 - freespace / totalspace ) * 100, 2 ) "使用率%" 
FROM
	( SELECT tablespace_name, round( sum( bytes ) / 1024 / 1024 / 1024, 3 ) totalspace FROM dba_data_files GROUP BY tablespace_name ) df,
	( SELECT tablespace_name, round( sum( bytes ) / 1024 / 1024 / 1024, 3 ) freespace FROM dba_free_space GROUP BY tablespace_name ) fs 
WHERE
	df.tablespace_name = fs.tablespace_name 
ORDER BY
	5 DESC;

Reduce tablespace usage with the following script:

--通过表空间名称查询表空间数据文件信息
SELECT * FROM dba_data_files t WHERE t.tablespace_name = 'TS_XX';
--拼接表空间数据文件resize脚本(一般只能扩大)
SELECT
	'alter database datafile ''' || t.file_name || ''' resize 34359721984;' 
FROM
	dba_data_files t 
WHERE
	t.tablespace_name = 'TS_XX';
--新增表空间数据文件
ALTER TABLESPACE TS_XX ADD DATAFILE SIZE 10m AUTOEXTEND ON NEXT 100m MAXSIZE UNLIMITED;
--resize表空间数据文件
ALTER database datafile '+DATA/OLAPCDB/CF550B7F99ADC8DBE0532A0709622DB8/DATAFILE/ts_xx.1804.1088175529' resize 34359721984;

Guess you like

Origin blog.csdn.net/m0_38004228/article/details/131454110