MySQL 數(shù)據(jù)庫空間使用大小查詢的方法實現(xiàn)
以下是 MySQL 數(shù)據(jù)庫空間大小查詢與管理的常用方法,基于最新實踐整理:
一、查詢數(shù)據(jù)庫空間大小
1. 查看所有數(shù)據(jù)庫空間
SELECT table_schema AS '數(shù)據(jù)庫', SUM(table_rows) AS '記錄數(shù)', SUM(TRUNCATE(data_length/1024/1024,2)) AS '數(shù)據(jù)容量(MB)', SUM(TRUNCATE(index_length/1024/1024,2)) AS '索引容量(MB)', SUM(TRUNCATE((data_length+index_length)/1024/1024,2)) AS '總大小(MB)' FROM information_schema.tables GROUP BY table_schema ORDER BY SUM(data_length) DESC;
此語句統(tǒng)計所有數(shù)據(jù)庫的總數(shù)據(jù)量、索引量及碎片空間,結(jié)果按數(shù)據(jù)容量降序排列。
使用
SELECT table_schema AS '數(shù)據(jù)庫', SUM(table_rows) AS '記錄數(shù)', SUM(TRUNCATE(data_length/1024/1024,2)) AS '數(shù)據(jù)容量(MB)', SUM(TRUNCATE(index_length/1024/1024,2)) AS '索引容量(MB)', SUM(TRUNCATE((data_length+index_length)/1024/1024,2)) AS '總大小(MB)' FROM information_schema.tables WHERE table_schema='sftzhzx_jy-241220' GROUP BY table_schema ORDER BY SUM(data_length) DESC; SELECT table_schema AS '數(shù)據(jù)庫', SUM(table_rows) AS '記錄數(shù)', SUM(TRUNCATE(data_length/1024/1024,2)) AS '數(shù)據(jù)容量(MB)', SUM(TRUNCATE(index_length/1024/1024,2)) AS '索引容量(MB)', SUM(TRUNCATE((data_length+index_length)/1024/1024,2)) AS '總大小(MB)' FROM information_schema.tables WHERE table_schema='sftzhzx_jd' GROUP BY table_schema ORDER BY SUM(data_length) DESC;
2. 查看指定數(shù)據(jù)庫空間
SELECT table_schema AS '數(shù)據(jù)庫', SUM(data_length + index_length)/1024/1024 AS '總大小(MB)' FROM information_schema.tables WHERE table_schema = 'your_database_name' GROUP BY table_schema;
替換 your_database_name 后,可獲取特定數(shù)據(jù)庫的總空間占用。
使用
SELECT table_schema AS '數(shù)據(jù)庫', SUM(data_length + index_length)/1024/1024 AS '總大小(MB)' FROM information_schema.tables WHERE table_schema = 'sftzhzx_jy-241220' GROUP BY table_schema; SELECT table_schema AS '數(shù)據(jù)庫', SUM(data_length + index_length)/1024/1024 AS '總大小(MB)' FROM information_schema.tables WHERE table_schema = 'sftzhzx_jd' GROUP BY table_schema;
二、查詢表級空間占用
1. 查看數(shù)據(jù)庫中所有表空間
SELECT table_name AS '表名', TRUNCATE(data_length/1024/1024,2) AS '數(shù)據(jù)容量(MB)', TRUNCATE(index_length/1024/1024,2) AS '索引容量(MB)', TRUNCATE((data_length+index_length)/1024/1024,2) AS '總大小(MB)' FROM information_schema.tables WHERE table_schema = 'your_database_name' ORDER BY (data_length + index_length) DESC;
用于分析指定數(shù)據(jù)庫內(nèi)各表的空間分布,識別大表。
2. 精確查詢單表空間
SELECT table_name AS '表名', TRUNCATE((data_length + index_length)/1024/1024,2) AS '總大小(MB)' FROM information_schema.tables WHERE table_schema = 'your_database_name' AND table_name = 'your_table_name';
適用于定位具體表的空間占用情況。
三、空間管理建議
1、自動擴展配置
在 my.cnf 配置文件中設(shè)置自動擴展參數(shù),避免空間不足:
[mysqld] innodb_data_file_path = ibdata1:10M:autoextend
2、獨立表空間優(yōu)化
啟用獨立表空間可提升管理靈活性:
SET GLOBAL innodb_file_per_table = 1;
3、定期清理碎片
對頻繁更新的表執(zhí)行優(yōu)化命令:
OPTIMIZE TABLE your_table_name;
以上方法結(jié)合系統(tǒng)表查詢與配置優(yōu)化,可有效管理和監(jiān)控數(shù)據(jù)庫空間。
到此這篇關(guān)于MySQL 數(shù)據(jù)庫空間使用大小查詢的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL 空間使用大小查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql處理海量數(shù)據(jù)時的一些優(yōu)化查詢速度方法
最近一段時間由于工作需要,開始關(guān)注針對Mysql數(shù)據(jù)庫的select查詢語句的相關(guān)優(yōu)化方法,需要的朋友可以參考下2017-04-04
MySQL復(fù)制出錯 Last_SQL_Errno:1146的解決方法
這篇文章主要介紹了MySQL復(fù)制出錯 Last_SQL_Errno:1146的解決方法,需要的朋友可以參考下2016-07-07

