Oracle Query 查找总大小、消耗量和可用大小,包括各种模式 11g
Posted
技术标签:
【中文标题】Oracle Query 查找总大小、消耗量和可用大小,包括各种模式 11g【英文标题】:Oracle Query to find total size vs consumed vs available size including various schemas 11g 【发布时间】:2021-02-12 10:58:16 【问题描述】:我需要对以下内容进行分解
整体 DWH 大小
已用空间
空闲空间
按模式划分空间(例如 STAGE、EDW/CORE、MART....)(必须涵盖第 1、2、3 点)
4.1 - 我有兴趣具体查看所有模式按总表大小与使用大小划分 (表),我尝试了以下查询(1 到 5)
查看各种帖子后,有点混乱
一些建议检查以下表格
DATA_SIZE(检查数据文件) TEMP_SIZE(检查临时文件) SYS.V_$LOG(检查重做日志文件) V$CONTROLFILE(检查重做日志文件)DBA_SEGMENTS我已经测试了以下查询
查询 1 - 数据库的实际大小
SELECT SUM (bytes) / 1024 / 1024 / 1024 AS GB FROM dba_data_files;
结果 - GB
GB 900 - 仅示例
查询 2 - 给出此数据库中数据占用的大小或数据库使用详情
SELECT SUM (bytes)/1024/1024/1024 AS GB FROM dba_segments;
结果-GB
GB 900 - 仅示例
查询 3 - 以 TB 为单位的整体数据库大小
SELECT
( SELECT SUM(BYTES)/1024/1024/1024/1024 DATA_SIZE FROM DBA_DATA_FILES ) +
( SELECT NVL(SUM(BYTES),0)/1024/1024/1024/1024 TEMP_SIZE FROM DBA_TEMP_FILES ) +
( SELECT SUM(BYTES)/1024/1024/1024/1024 REDO_SIZE FROM SYS.V_$LOG ) +
( SELECT SUM(BLOCK_SIZE*FILE_SIZE_BLKS)/1024/1024/1024/1024 CONTROLFILE_SIZE FROM V$CONTROLFILE) "SIZE IN TB"
FROM
DUAL
结果-结核病
大小(TB) 100 - 仅示例
查询 4 - 以 TB 为单位的数据库大小,包含使用空间和可用空间 Oracle DB
select round(sum(used.bytes) / 1024 / 1024 / 1024/1024 ) || 'TB' "Database Size"
, round(sum(used.bytes) / 1024 / 1024 / 1024/1024 ) -
round(free.p / 1024 / 1024 / 1024/1024) || 'TB' "Used space"
, round(free.p / 1024 / 1024 / 1024/1024) || 'TB' "Free space"
from (select bytes
from v$datafile
union all
select bytes
from v$tempfile
union all
select bytes
from v$log) used
, (select sum(bytes) as p
from dba_free_space) free
group by free.p
/
结果-结核病
数据库大小 已用空间 可用空间 100 90 10
查询 5 - 查询以获取 ORACLE 数据库架构中所有表的大小
SELECT * FROM
(
SELECT
OWNER,
OBJECT_NAME,
OBJECT_TYPE,
TABLE_NAME,
--ROUND(BYTES)/1024/1024 AS MB,
ROUND(BYTES) / 1024 / 1024 / 1024 AS GB,
--ROUND(100*RATIO_TO_REPORT(ROUND(BYTES) / 1024 / 1024 / 1024) OVER(),2) AS GB_PERCENT,
ROUND(100*RATIO_TO_REPORT(BYTES) OVER (), 2) PERCENTAGE,
TABLESPACE_NAME,
EXTENTS,
INITIAL_EXTENT,
ROUND(SUM(BYTES/1024/1024/1024) OVER (PARTITION BY TABLE_NAME)) AS TOTAL_TABLE_GB
--ROUND(SUM(BYTES)/1024/1024/1024) OVER (PARTITION BY TABLE_NAME)) AS TOTAL_TABLE_GB
FROM
(
--TABLES
SELECT OWNER, SEGMENT_NAME AS OBJECT_NAME, 'TABLE' AS OBJECT_TYPE,
SEGMENT_NAME AS TABLE_NAME, BYTES,
TABLESPACE_NAME, EXTENTS, INITIAL_EXTENT
FROM DBA_SEGMENTS /*DBA_SEGMENTS*/
WHERE SEGMENT_TYPE IN ('TABLE', 'TABLE PARTITION', 'TABLE SUBPARTITION')
UNION ALL
--INDEXES
SELECT I.OWNER, I.INDEX_NAME AS OBJECT_NAME, 'INDEX' AS OBJECT_TYPE,
I.TABLE_NAME, S.BYTES,
S.TABLESPACE_NAME, S.EXTENTS, S.INITIAL_EXTENT
FROM DBA_INDEXES I /*DBA_INDEXES*/
, DBA_SEGMENTS S /*DBA_SEGMENTS*/
WHERE S.SEGMENT_NAME = I.INDEX_NAME
AND S.OWNER = I.OWNER
AND S.SEGMENT_TYPE IN ('INDEX', 'INDEX PARTITION', 'INDEX SUBPARTITION')
--LOB SEGMENTS
UNION ALL
SELECT L.OWNER, L.COLUMN_NAME AS OBJECT_NAME, 'LOB_COLUMN' AS OBJECT_TYPE,
L.TABLE_NAME, S.BYTES,
S.TABLESPACE_NAME, S.EXTENTS, S.INITIAL_EXTENT
FROM DBA_LOBS L, /*DBA_LOBS*/
DBA_SEGMENTS S /*DBA_SEGMENTS*/
WHERE S.SEGMENT_NAME = L.SEGMENT_NAME
AND S.OWNER = L.OWNER
AND S.SEGMENT_TYPE = 'LOBSEGMENT'
--LOB INDEXES
UNION ALL
SELECT L.OWNER, L.COLUMN_NAME AS OBJECT_NAME, 'LOB_INDEX' AS OBJECT_TYPE,
L.TABLE_NAME, S.BYTES,
S.TABLESPACE_NAME, S.EXTENTS, S.INITIAL_EXTENT
FROM DBA_LOBS L, /*DBA_LOBS*/
DBA_SEGMENTS S /*DBA_SEGMENTS*/
WHERE S.SEGMENT_NAME = L.INDEX_NAME
AND S.OWNER = L.OWNER
AND S.SEGMENT_TYPE = 'LOBINDEX'
)
WHERE OWNER IN UPPER('&SCHEMA_NAME')
)
--WHERE TOTAL_TABLE_MB > 10
ORDER BY TOTAL_TABLE_GB DESC, GB DESC
/
预期结果
OWNER OBJECT_TYPE TOTAL_SPACE_GB TOTAL_SPACE_USED_GB PERCENTAGE_GB
DWH_STAGE TABLE 400 200 50
DWH_EDW TABLE 800 400 50
DWH_MART TABLE 1600 800 50
您能否建议如何实现这一目标?
【问题讨论】:
你想要完成什么? 【参考方案1】:如果这是您想要的,这会显示表段内的空间,但我认为您可能需要按表空间的空闲空间。
select t.owner,'TABLE' OBJECT_TYPE ,t.TOTAL_SPACE_GB, u.TOTAL_SPACE_USED_GB,(u.TOTAL_SPACE_USED_GB/t.TOTAL_SPACE_GB)*100 PERCENTAGE_USED
from
(select owner,sum(bytes)/(1024*1024*1024) TOTAL_SPACE_GB
from dba_segments
where segment_type like 'TABLE%'
group by owner) t,
(select owner, sum(NUM_ROWS*AVG_ROW_LEN)/(1024*1024*1024) TOTAL_SPACE_USED_GB
from dba_tables
group by owner) u
where
t.owner=u.owner
order by owner;
鲍比
【讨论】:
@Boby Durrett - 是否可以在 TB 中检查相同的内容,我添加了额外的 1024 但导致除数错误,任何建议 将 1024*1024*1024 替换为 1024*1024*1024*1024 以获得 TB。 @Boby Durrett - 谢谢,但新的要求是 1) 单个模式每天消耗的空间 2) 四舍五入到小数点后两位,这可能是第 1 点和第 2 点吗? 我建议您重写您的问题,准确说明您想要回答的问题。请包括您的 Oracle 数据库的确切版本(如 11.2.0.4)以及您已获得许可的软件。 IE。它是企业版吗?您有可用的诊断和调整包吗?如果您提供该信息,我将尽力改写我的答案以适合您的问题。以上是关于Oracle Query 查找总大小、消耗量和可用大小,包括各种模式 11g的主要内容,如果未能解决你的问题,请参考以下文章