[精讚] [會員登入]
2379

[Mysql/MariaDB] 查看資料庫所占空間

查看資料庫在磁碟中所占空間

分享此文連結 //n.sfs.tw/10274

分享連結 [Mysql/MariaDB] 查看資料庫所占空間@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 05:50:14 最後編修
2016-11-23 01:11:04 By 張○○
 

自動目錄

法一

到mysql的目錄,以Centos為例:

# cd /var/lib/mysql

由於mysql是一個資料庫占一個目錄,所以下指令:

# du -Sh dbname
3.1M    dbname

這個方法簡單但有時得到的結果是錯的,因為如果資料庫的ENGINE 是Memory或是InnoDB,資料並非存於此目錄中。

法二

進到你的mysql console,選擇你的資料庫:
mysql> use dbname;
mysql> SHOW TABLE STATUS;

或直接下指令

# mysqlshow --status note

列出的

Data_length  +  Index_length  就是大小,單位是bytes.

這個方法的好處是可以列出每一個table所占的大小,但比較麻煩是沒有加總,您得自己加總


吃飯前禱告一下, 你先別跑...

法三

在[1]下方有kevingo網友提供一個複雜的語法來查看,我略為修改:

SELECT concat(table_schema,'.',table_name), 
concat(round(table_rows/1000,2),'K') rows, 
concat(round(data_length/(1024*1024),2),'M') DATA, 
concat(round(index_length/(1024*1024),2),'M') idx, 
concat(round((data_length+index_length)/(1024*1024),2),'M') total_size, 
round(index_length/data_length,2) idxfrac 
FROM information_schema.TABLES 
WHERE table_schema='note' 
ORDER BY data_length+index_length ;

請修改第8行的"note"為您的資料庫名

執行結果:(idxfrac:索引/資料的比值)

mysql> SELECT concat(table_schema,'.',table_name),
    -> concat(round(table_rows/1000,2),'K') rows,
    -> concat(round(data_length/(1024*1024),2),'M') DATA,
    -> concat(round(index_length/(1024*1024),2),'M') idx,
    -> concat(round((data_length+index_length)/(1024*1024),2),'M') total_size,
    -> round(index_length/data_length,2) idxfrac
    -> FROM information_schema.TABLES where table_schema='note'
    -> ORDER BY data_length+index_length ;
+-------------------------------------+-------+-------+-------+------------+---------+
| concat(table_schema,'.',table_name) | rows  | DATA  | idx   | total_size | idxfrac |
+-------------------------------------+-------+-------+-------+------------+---------+
| note.contact                        | 0.00K | 0.00M | 0.00M | 0.00M      |    2.23 |
| note.month_user                     | 0.04K | 0.00M | 0.00M | 0.00M      |    2.12 |
| note.log_nobody                     | 0.02K | 0.00M | 0.00M | 0.00M      |    2.04 |
| note.media_extra                    | 0.01K | 0.00M | 0.00M | 0.00M      |   13.13 |

... 略 ...


| note.test                           | 0.00K | 1.60M | 0.00M | 1.60M      |    0.00 |
| note.temp                           | 5.81K | 2.86M | 0.34M | 3.20M      |    0.12 |
+-------------------------------------+-------+-------+-------+------------+---------+
25 rows in set (0.00 sec)

上面的缺點還是沒有算加總,所以再改一下

SELECT round(sum(data_length)/(1024*1024),2) DATA_SUM, 
round(sum(index_length)/(1024*1024),2) INDEX_SUM,
round(sum(data_length+index_length)/(1024*1024),2) TOTAL 
FROM information_schema.TABLES 
where table_schema='note';

執行結果(單位是M)

mysql> SELECT round(sum(data_length)/(1024*1024),2) DATA_SUM,  
    -> round(sum(index_length)/(1024*1024),2) INDEX_SUM,
    -> round(sum(data_length)+sum(index_length),2) TOTAL
    -> FROM information_schema.TABLES
    -> where table_schema='note';
+----------+-----------+-------+
| DATA_SUM | INDEX_SUM | TOTAL |
+----------+-----------+-------+
|     6.35 |      0.64 |  6.99 |
+----------+-----------+-------+
1 row in set (0.00 sec)

參考資料

[1] Tsung's blog http://blog.longwin.com.tw/2009/02/view-mysql-table-size-2009/


原文 2013-09-10 14:42:11

 

 

 

 

 

END

你可能感興趣的文章

[Mysql/Mariadb] 密碼設定強度修改 新的mysql對於密碼預設也把原來的規則改得更嚴格,此篇教你修改密碼的設定政策

[MySQL/Mariadb] console 將查詢結果輸出到檔案 在mysql 的console中該如何將查詢結果輸出到檔案?

[MYSQL] 設定字串欄位的預設值 新增表格的時候,字串欄位給予預設空值;數字欄位給預設數值;日期欄位給空值。

[MySQL] FIND_IN_SET函數,把多筆條件判斷弄成字串 SQL 我們如果要查找某個欄位是否為某個值的時候,如果要判斷的條件很多,可以用這個函數

[Docker] Mariadb-Galera出現Incorrect definition of table mysql.column_stats:'hist_type'及 'histogram' 使用docker的Mariadb-galera出現'hist_type'及 'histogram'型別錯誤的解決方法。

[mysqldump] Mysqldump時出現記憶體錯誤Out of memory 當進行 mysqldump 時出現 Out of memory (Needed xxxxx bytes) 的錯誤,該如何處理?

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[HP DL380G7] 生效啟動第3,4片網卡/開啟或關閉內建的網卡 HP DL380G7 預設第3,4片網卡裝完系統後找不到,難道是壞了?要怎麼辦?

安裝SPHINX支援中文 新版本的 sphinx 和舊版不同,網路上很多範例和教學是不能用的。此文是安裝和設定方法分享

[Windows7] 移除IE10及移除IE11 Windows7 不得已的情況要移除IE11或IE10怎麼做?

[CodeIgniter3] 解決無法上傳特定檔案(.sb2)的問題 上傳時出現The filetype you are attempting to upload is not allowed,要怎麼解決?

一個邏輯的錯誤刪了全部檔案的經驗 今天本來想做一件很簡單的事,但卻足足浪費我多一倍的時間,再加上刪掉我全部的檔案,原因只是因為我自己的邏輯錯誤。