首頁 > 軟體

MySQL資料庫如何檢視表佔用空間大小

2022-06-10 22:05:12

前言

在mysql中有一個預設的資料表information_schema,information_schema這張資料表儲存了MySQL伺服器所有資料庫的資訊。如資料庫名,資料庫的表,表欄的資料型別與存取許可權等。再簡單點,這臺MySQL伺服器上,到底有哪些資料庫、各個資料庫有哪些表,每張表的欄位型別是什麼,各個資料庫要什麼許可權才能存取,等等資訊都儲存在information_schema表裡面,所以請勿刪改此表。

1、切換資料庫

use information_schema;

2、檢視所有資料庫容量大小

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;

3、檢視指定資料庫使用大小

short_video庫名 video_info 表名

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='short_video';

4、檢視表使用大小

video_info 表名

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='short_video' and table_name='video_info';

5、檢視所有資料庫容量大小

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;

6、檢視所有資料庫各表容量大小

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;

7、檢視指定資料庫容量大小

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='short_video';

8、檢視指定資料庫各表容量大小

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='short_video'
order by data_length desc, index_length desc;

總結

到此這篇關於MySQL資料庫如何檢視表佔用空間大小的文章就介紹到這了,更多相關MySQL檢視表佔用空間大小內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com