Query database space (mysql and oracle)

MySQL version

1. Check the size of all databases

-- 查看所有数据库容量大小
SELECT
    table_schema AS '数据库',
    sum( table_rows ) AS '记录数',
    sum(
    TRUNCATE ( data_length / 1024 / 1024, 2 )) AS '数据容量(MB)',
    sum(
    TRUNCATE ( index_length / 1024 / 1024, 2 )) AS '索引容量(MB)' 
FROM
    information_schema.TABLES 
GROUP BY
    table_schema 
ORDER BY
    sum( data_length ) DESC,
    sum( index_length ) DESC;

2. View the capacity of each table in all databases

SELECT
    table_schema AS '数据库',
    table_name AS '表名',
    table_rows AS '记录数',
    TRUNCATE ( data_length / 1024 / 1024, 2 ) AS '数据容量(MB)',
    TRUNCATE ( index_length / 1024 / 1024, 2 ) AS '索引容量(MB)' 
FROM
    information_schema.TABLES 
ORDER BY
    data_length DESC,
    index_length DESC;

3. View the specified database capacity

SELECT
    table_schema AS '数据库',
    sum( table_rows ) AS '记录数',
    sum(
    TRUNCATE ( data_length / 1024 / 1024, 2 )) AS '数据容量(MB)',
    sum(
    TRUNCATE ( index_length / 1024 / 1024, 2 )) AS '索引容量(MB)' 
FROM
    information_schema.TABLES 
WHERE
    table_schema = '数据库名';

4. View the capacity of each table in the specified database

SELECT
    table_schema AS '数据库',
    table_name AS '表名',
    table_rows AS '记录数',
    TRUNCATE ( data_length / 1024 / 1024, 2 ) AS '数据容量(MB)',
    TRUNCATE ( index_length / 1024 / 1024, 2 ) AS '索引容量(MB)' 
FROM
    information_schema.TABLES 
WHERE
    table_schema = '数据库名' 
ORDER BY
    data_length DESC,
    index_length DESC;

5. View the table information of the specified database

SHOW TABLE STATUS;

oracle version

1. View the space occupied by the table

--  不需要DBA权限
SELECT SEGMENT_NAME TABLENAME,(BYTES/1024/1024) MB
,RANK() OVER (PARTITION BY NULL ORDER BY BYTES DESC) RANK_ID  //根据表大小进行排序
FROM USER_SEGMENTS
WHERE SEGMENT_TYPE='TABLE'

-- 需要DBA权限,一般情况下很少会给这么高的权限,可以说这个权限基本没有,所以一般工作中不是DBA的人不会常用到这个命令
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size 
FROM dba_tablespaces t, dba_data_files d 
WHERE t.tablespace_name = d.tablespace_name 
GROUP BY t.tablespace_name; 
 

2. View the usage of the table space

SELECT a.tablespace_name "表空间名称",
       total / (1024 * 1024) "表空间大小(M)",
       free / (1024 * 1024) "表空间剩余大小(M)",
       (total - free) / (1024 * 1024 ) "表空间使用大小(M)",
       total / (1024 * 1024 * 1024) "表空间大小(G)",
       free / (1024 * 1024 * 1024) "表空间剩余大小(G)",
       (total - free) / (1024 * 1024 * 1024) "表空间使用大小(G)",
       round((total - free) / total, 4) * 100 "使用率 %"
FROM (SELECT tablespace_name, SUM(bytes) free
      FROM dba_free_space
      GROUP BY tablespace_name) a,
     (SELECT tablespace_name, SUM(bytes) total
      FROM dba_data_files
      GROUP BY tablespace_name) b
WHERE a.tablespace_name = b.tablespace_name

3. View the rollback segment name and size

SELECT segment_name, 
tablespace_name, 
r.status, 
(initial_extent / 1024) initialextent, 
(next_extent / 1024) nextextent, 
max_extents, 
v.curext curextent 
FROM dba_rollback_segs r, v$rollstat v 
WHERE r.segment_id = v.usn(+) 
ORDER BY segment_name; 

4. Check the control file

SELECT NAME FROM v$controlfile; 

5. View log files

SELECT MEMBER FROM v$logfile; 

6. View database objects

SELECT owner, object_type, status, COUNT(*) count# 
FROM all_objects 
GROUP BY owner, object_type, status; 

7. View the database version

SELECT version 
FROM product_component_version 
WHERE substr(product, 1, 6) = 'Oracle'; 

8. View the creation date and filing method of the database

SELECT created, log_mode, log_mode FROM v$database; 

9. Check whether the table space has the ability to automatically expand

SELECT T.TABLESPACE_NAME,D.FILE_NAME,
D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS
FROM DBA_TABLESPACES T,DBA_DATA_FILES D
WHERE T.TABLESPACE_NAME =D.TABLESPACE_NAME
 ORDER BY TABLESPACE_NAME,FILE_NAME;

oracle enhanced version

1. Check the table space usage

1. View the database tablespace file:

--查看数据库表空间文件
select * from dba_data_files;

2. View the total capacity of all table spaces:

--查看所有表空间的总容量
select dba.TABLESPACE_NAME, sum(bytes)/1024/1024 as MB  
from dba_data_files dba 
group by dba.TABLESPACE_NAME;

3. View the database table space usage

--查看数据库表空间使用率
select total.tablespace_name,round(total.MB, 2) as Total_MB,round(total.MB - free.MB, 2) as Used_MB,round((1-free.MB / total.MB)* 100, 2) || '%' as Used_Pct 
from (
select tablespace_name, sum(bytes) /1024/1024 as MB 
from dba_free_space group by tablespace_name) free,
(select tablespace_name, sum(bytes) / 1024 / 1024 as MB 
from dba_data_files group by tablespace_name) total     
where free.tablespace_name = total.tablespace_name 
order by used_pct desc;

4.1. View the total size, usage, and remaining space of the table space

--查看表空间总大小、使用率、剩余空间
select a.tablespace_name, total, free, total-free as used, substr(free/total * 100, 1, 5) as "FREE%", substr((total - free)/total * 100, 1, 5) as "USED%"
from
(select tablespace_name, sum(bytes)/1024/1024 as total from dba_data_files group by tablespace_name) a,
(select tablespace_name, sum(bytes)/1024/1024 as free from dba_free_space group by tablespace_name) b
where a.tablespace_name = b.tablespace_name
order by a.tablespace_name

4.2. View table space usage (including temp temporary table space)

--查看表空间使用率(包含临时表空间)
select * from (
Select a.tablespace_name,
(a.bytes- b.bytes) "表空间使用大小(BYTE)",
a.bytes/(1024*1024*1024) "表空间大小(GB)",
b.bytes/(1024*1024*1024) "表空间剩余大小(GB)",
(a.bytes- b.bytes)/(1024*1024*1024) "表空间使用大小(GB)",
to_char((1 - b.bytes/a.bytes)*100,'99.99999') || '%' "使用率"
from (select tablespace_name,
sum(bytes) bytes
from dba_data_files
group by tablespace_name) a,
(select tablespace_name,
sum(bytes) bytes
from dba_free_space
group by tablespace_name) b
where a.tablespace_name = b.tablespace_name
union all
select c.tablespace_name,
d.bytes_used "表空间使用大小(BYTE)",
c.bytes/(1024*1024*1024) "表空间大小(GB)",
(c.bytes-d.bytes_used)/(1024*1024*1024) "表空间剩余大小(GB)",
d.bytes_used/(1024*1024*1024) "表空间使用大小(GB)",
to_char(d.bytes_used*100/c.bytes,'99.99999') || '%' "使用率"
from
(select tablespace_name,sum(bytes) bytes
from dba_temp_files group by tablespace_name) c,
(select tablespace_name,sum(bytes_cached) bytes_used
from v$temp_extent_pool group by tablespace_name) d
where c.tablespace_name = d.tablespace_name
)
order by tablespace_name

5. Check the space occupied by the specific table

--查看具体表的占用空间大小
select * from (
select t.tablespace_name,t.owner, t.segment_name, t.segment_type, sum(t.bytes / 1024 / 1024) mb
from dba_segments t
where t.segment_type='TABLE'
group by t.tablespace_name,t.OWNER, t.segment_name, t.segment_type
) t
order by t.mb desc

Second, expand the size or increase the table space file

1. Change the dbf data file allocation space size of the table space

alter database datafile ‘...\system_01.dbf' autoextend on;
alter database datafile ‘...\system_01.dbf' resize 1024M;

2. Add a new data file for the table space (the table space is full of 32G and cannot be expanded, and the table space file is added)

alter tablespace SYSTEM add datafile '/****' size 1000m autoextend on next 100m;

3. If it is a temp temporary table to add a new table space, an error will be reported:

0RA-03217: Invalid option to change TEMPORARY TABLESPACE
Solution: change datafile to tempfile

alter tablespace TEMP01 add tempfile'/****' size 1000m autoextend on next 100m maxsize 10000m

Aiming at the full usage rate of temp temporary table space,
the main purpose of temporary table space is to provide temporary computing space when the database performs operations such as sorting operations, index management, and accessing views. When the operation is completed, the system will automatically clean up, but sometimes we encounter The temporary segment has not been released, and the TEMP table space is almost full;
the increase in the temporary table space is mainly used in the following situations:
1. order by or group by (disc sort accounts for the main part);
2. Index creation
3. Distinct operation; 4.
Union & intersect & minus sort-merge joins;
5. Analyze operation;
6. Some abnormalities can also cause TEMP to skyrocket.
Solution 1: Use the above method to add tablespace files to temp
Solution 2: In the case of limited server resource space, re-create a new temporary tablespace to replace the current tablespace

--1.查看当前的数据库默认表空间:
select * from database_properties
where property_name='DEFAULT_TEMP_TABLESPACE';
 
--2.创建新的临时表空间
create temporary tablespace TEMP01 tempfile 
'/home/temp01.dbf' size 31G;
 
--3.更改默认临时表空间
alter database default temporary tablespace TEMP01;
 
--4.删除原来的临时表空间
drop tablespace TEMP02 including contents and datafiles;
 
--如果删除原来临时表空间报错ORA-60100:由于排序段,已阻止删除表空间...
--(说明有语句正在使用原来的临时表空间,需要将其kill掉再删除,此语句多为排序的语句)
--查询语句
Select se.username,se.sid,se.serial#,su.extents,su.blocks*to_number(rtrim(p.value))as Space,
tablespace,segtype,sql_text
from v$sort_usage su,v$parameter p,v$session se,v$sql s
where p.name='db_block_size' and su.session_addr=se.saddr and s.hash_value=su.sqlhash
and s.address=su.sqladdr
order by se.username,se.sid;
 
--删除对应的'sid,serial#'
alter system kill session 'sid,serial#'

Attachment: Check whether the table space has the ability to automatically expand

--查看表空间是否具有自动扩展的能力     
SELECT T.TABLESPACE_NAME,D.FILE_NAME,     
D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS     
FROM DBA_TABLESPACES T,DBA_DATA_FILES D     
WHERE T.TABLESPACE_NAME =D.TABLESPACE_NAME     
 ORDER BY TABLESPACE_NAME,FILE_NAME;

Guess you like

Origin blog.csdn.net/qq_40520912/article/details/130142013