当我们在Oracle数据库中创建了一个临时表时,临时表空间就会被分配给该表。但是,有时候临时表空间的使用率会很高,这就需要我们查询临时表空间的实际使用率并作出相应的调整。下面是查询临时表空间实际使用率的SQL语句:

SELECT tablespace_name, round((bytes_used / bytes_total)100,2) as usage_pct

FROM

(SELECT tablespace_name, SUM(bytes_used) as bytes_used, SUM(bytes_total) as bytes_total

FROM (SELECT b.tablespace_name, b.segtype, COUNT() * c.block_size AS bytes_total,

DECODE(b.segtype, 'TEMPORARY', COUNT() * c.block_size, 0) AS bytes_used

FROM v$sort_usage a, dba_extents b, dba_tablespaces c

WHERE b.file_id = a.file_id AND b.block_id <= a.block_max AND b.block_id + b.blocks - 1 >= a.block_min

AND b.owner = c.contents(+) AND (b.tablespace_name like 'TEMP%' OR b.tablespace_name like 'TOOLS%')

GROUP BY b.tablespace_name, b.segtype)

GROUP BY tablespace_name)

WHERE round((bytes_used / bytes_total)100,2) >= 80;