create user tsmonitor identified by I11m8cb default tablespace tsmonitor;
4.为了tbsmonitor用户赋权用来查找表空间使用情况。
grant resource to tbsmonitor;
grant create session to tbsmonitor;
grant create table to tbsmonitor;
grant select on dba_data_files to tbsmonitor;
grant select on dba_free_space to tbsmonitor;
create database link TO_DATABASE1
connect to TSMONITOR identified by I11m08cb
using 'DATABASE1';
create database link TO_DATABASE2
connect to TSMONITOR identified by I11m08cb
using 'DATABASE2';
create database link TO_DATABASE3
connect to TSMONITOR identified by I11m08cb
using 'DATABASE3';
insert into tsmonitor.tbsmonitor SELECT utl_inaddr.get_host_address('DATABASE1') ipaddress,
(select instance_name from v$instance) instancename,
df.tablespace_name,
COUNT(*) datafile_count,
ROUND(SUM(df.BYTES) / 1048576) size_mb,
ROUND(SUM(free.BYTES) / 1048576, 2) free_mb,
ROUND(SUM(df.BYTES) / 1048576 - SUM(free.BYTES) / 1048576, 2) used_mb,
ROUND(MAX(free.maxbytes) / 1048576, 2) maxfree,
100 - ROUND(100.0 * SUM(free.BYTES) / SUM(df.BYTES), 2) pct_used,
ROUND(100.0 * SUM(free.BYTES) / SUM(df.BYTES), 2) pct_free,sysdate time
FROM dba_data_files@TO_DATABASE1 df,
(SELECT tablespace_name,
file_id,
SUM(BYTES) BYTES,
MAX(BYTES) maxbytes
FROM dba_free_space@TO_DATABASE1
GROUP BY tablespace_name, file_id) free
WHERE df.tablespace_name = free.tablespace_name(+)
AND df.file_id = free.file_id(+)
GROUP BY df.tablespace_name
ORDER BY 6;
12.查看表空间使用占比可以使用如下语句(如果要查看某台机器可以带上条件where ipaddress='xxxx' and instance='xxxxx' and to_char(time,'yyyy-mm-dd')='xxxx-xx-xx')
SELECT IPADDRESS ,
Instancename,
tablespace_name,
datafile_count,
size_mb "表空间大小(M)",
used_mb "已使用空间(M)",
TO_CHAR(ROUND((used_mb) / size_mb * 100,
2),
'990.99') "使用比",
free_mb "空闲空间(M)"
FROM tbsmonitor. tbsmonitor order by "使用比" desc
13.查看每日增量可以使用如下脚本。(下面显示的是4-8日10.1.21.2表空间增长的情况)
select a.tablespace_name,(b.used_mb-a.used_mb) increase,a.ipaddress from
(select * from tsmonitor.tbs_usage where to_char(time,'yyyy-mm-dd')='2016-01-04') a,
(select * from tsmonitor.tbs_usage where to_char(time,'yyyy-mm-dd')='2016-01-08') b
where a.tablespace_name=b.tablespace_name and a.IPADDRESS=b.IPADDRESS order by increase desc
select * from tbsmonitor. tbsmonitor where ipaddress='10.1.21.2' and to_char(time,'yyyy-mm-dd')='2016-01-08'