在上篇ORACLE查看表空間對象中,我介紹了如何查詢一個表空間有那些數據庫對象,那麼我們是否可以查看某個數據文件包含那些數據庫對象呢?如下所示
SELECT E.SEGMENT_TYPE AS SEGMENT_TYPE
, E.SEGMENT_NAME AS SEGMENT_NAME
, F.FILE_NAME AS FILE_NAME
, SUM(E.BYTES)/1024/1024 AS SEGMENT_SIZE
FROM DBA_EXTENTS E
INNER JOIN DBA_DATA_FILES F ON E.FILE_ID= F.FILE_ID
WHERE F.FILE_ID =109
GROUP BY E.SEGMENT_TYPE,E.SEGMENT_NAME,F.FILE_NAME
ORDER BY 4 DESC;
SELECT E.SEGMENT_TYPE AS SEGMENT_TYPE
, E.SEGMENT_NAME AS SEGMENT_NAME
, F.FILE_NAME AS FILE_NAME
, SUM(E.BYTES)/1024/1024 AS SEGMENT_SIZE
FROM DBA_EXTENTS E
INNER JOIN DBA_DATA_FILES F ON E.FILE_ID= F.FILE_ID
WHERE F.FILE_NAME =&FILE_NAME
GROUP BY E.SEGMENT_TYPE,E.SEGMENT_NAME,F.FILE_NAME
ORDER BY 4 DESC;
錯誤的寫法,如下所示,DBA_SEGMENTS的HEADER_FILE表示ID of the file containing the segment header,主要是因為僅僅包含段對象的段頭所在的文件。
SELECT ds.OWNER AS OWNER
,df.FILE_NAME AS FILE_NAME
,ds.SEGMENT_NAME AS SEGMENT_NAME
,ds.SEGMENT_TYPE AS SEGMENT_TYPE
,SUM(ds.BYTES)/1024/1024 AS SEGMENT_SIZE
FROM DBA_SEGMENTS ds
LEFT JOIN DBA_DATA_FILES df ON ds.HEADER_FILE = df.FILE_ID
WHERE ds.TABLESPACE_NAME=&TABLESPACE_NAME
AND df.FILE_ID=&FILE_ID
GROUP BY OWNER,FILE_NAME,SEGMENT_NAME,SEGMENT_TYPE
ORDER BY 4;