oracle table 使用的获取 space 的查询太慢

Query to get space used by oracle table is too slow

我正在使用下面的查询来获取 table 占用的内存。

SELECT SUM(bytes), SUM(bytes)/1024/1024 MB
    FROM dba_extents
    WHERE owner = :Owner
    AND segment_name = :table_name;

打印 2 MB 文件大小需要 986 秒(16 分钟)。

这个查询有什么问题?

有没有更好的查询可以非常快速地提供相同的数据?

SELECT SUM(bytes), SUM(bytes)/1024/1024 MB
FROM DBA_SEGMENTS
WHERE owner = :Owner
AND segment_name = :table_name;

段数远远少于范围,但很奇怪。您的 table 有多少个范围?

仅供参考,这只是未分区的 table 数据。如果 table 有索引、LOB 或对象类型,它不是它所需要的全部 space。

访问速度慢 DBA_EXTENTS 好像是common issue。这是引述:

The main performance problem here however is that the values of the BYTES, BLOCKS & EXTENTS columns of the DBA/USER/ALL_SEGMENTS and DBA/USER/ALL_EXTENTS views are not stored in the Data Dictionary for segments that reside in Locally Managed Tablespaces. Instead they must be obtained by accessing the Segment Header Blocks for all segments processed in the query. These extra block accesses cause the slow performance. So with increasing number of database files and segments the query performance in this case can potentially degrade. Furthermore Oracle in certain circumstance does not cache properly those segment header blocks, thus forcing reading them from disk even at the next query run – so no caching happening. Do not be surprised if I say that a query against dba_segments can trigger even at the background a few more procedures from DBMS_SPACE_ADMIN package to get correct segment sizing information.

Link 也包含一个解决方案,我无法应用和测试,因为我现在没有自己的数据库。但我相信你已经明白了。