久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么查詢mysql和oracle數據庫空間

153次閱讀
沒有評論

共計 7330 個字符,預計需要花費 19 分鐘才能閱讀完成。

本篇內容主要講解“怎么查詢 mysql 和 oracle 數據庫空間”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓丸趣 TV 小編來帶大家學習“怎么查詢 mysql 和 oracle 數據庫空間”吧!

Mysql 版 1、查看所有數據庫容量大小

--  查看所有數據庫容量大小
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)  
 information_schema.TABLES 
GROUP BY
 table_schema 
ORDER BY
 sum( data_length ) DESC,
 sum( index_length ) DESC;

2、查看所有數據庫各表容量大小

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)  
 information_schema.TABLES 
ORDER BY
 data_length DESC,
 index_length DESC;

3、查看指定數據庫容量大小

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)  
 information_schema.TABLES 
WHERE
 table_schema =  數據庫名

4. 查看指定數據庫各表容量大小

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)  
 information_schema.TABLES 
WHERE
 table_schema =  數據庫名  
ORDER BY
 data_length DESC,
 index_length DESC;

5. 查看指定數據庫各表信息

SHOW TABLE STATUS;

oracle 版 1、查看表所占的空間大小

--  不需要 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、查看表空間的使用情況

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、查看回滾段名稱及大小

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、查看控制文件

SELECT NAME FROM v$controlfile;

5、查看日志文件

SELECT MEMBER FROM v$logfile;

6、查看數據庫對象

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

7、查看數據庫版本

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

8、查看數據庫的創建日期和歸檔方式

SELECT created, log_mode, log_mode FROM v$database;

9、查看表空間是否具有自動擴展的能力

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 加強版一、查看表空間使用率 1. 查看數據庫表空間文件:

-- 查看數據庫表空間文件
select * from dba_data_files;

2. 查看所有表空間的總容量:

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

3. 查看數據庫表空間使用率

-- 查看數據庫表空間使用率
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. 查看表空間總大小、使用率、剩余空間

-- 查看表空間總大小、使用率、剩余空間
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% 
(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. 查看表空間使用率(包含 temp 臨時表空間)

-- 查看表空間使用率(包含臨時表空間)
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) ||  %   使用率 
(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. 查看具體表的占用空間大小

-- 查看具體表的占用空間大小
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

二、擴展大小或增加表空間文件 1. 更改表空間的 dbf 數據文件分配空間大小

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

2. 為表空間新增一個數據文件(表空間滿 32G 不能擴展則增加表空間文件)

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

3. 如果是 temp 臨時表新增表空間會報錯:

0RA-03217: 變更 TEMPORARY TABLESPACE 無效的選項
解決方法: datafile 改為 tempfile

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

針對 temp 臨時表空間使用率爆滿問題
臨時表空間主要用途是在數據庫進行排序運算、管理索引、訪問視圖等操作時提供臨時的運算空間,當運算完成之后系統會自動清理,但有些時候我們會遇到臨時段沒有被釋放,TEMP 表空間幾乎滿使用率情況;
引起臨時表空間增大主要使用在以下幾種情況:
1、order by or group by (disc sort 占主要部分);
2、索引的創建和重創建;
3、distinct 操作;
4、union intersect minus sort-merge joins;
5、Analyze 操作;
6、有些異常也會引起 TEMP 的暴漲。
解決方法一:用上述方法給 temp 增加表空間文件
解決方法二:在服務器資源空間有限的情況下,重新建立新的臨時表空間替換當前的表空間

--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#

附:查看表空間是否具有自動擴展的能力

-- 查看表空間是否具有自動擴展的能力  
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;

到此,相信大家對“怎么查詢 mysql 和 oracle 數據庫空間”有了更深的了解,不妨來實際操作一番吧!這里是丸趣 TV 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-13發表,共計7330字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 九江市| 文安县| 泾川县| 雷州市| 象山县| 濮阳市| 寿阳县| 白城市| 黄山市| 岫岩| 通辽市| 和静县| 丹寨县| 白城市| 芷江| 依安县| 上虞市| 舞钢市| 广德县| 沁阳市| 武威市| 太原市| 左贡县| 邢台县| 陆川县| 富宁县| 五寨县| 泊头市| 淮阳县| 金乡县| 江孜县| 达孜县| 台南县| 秀山| 平凉市| 兴业县| 浮梁县| 兴安盟| 镇安县| 潢川县| 红桥区|