丹陽(yáng)做公司網(wǎng)站的蒙牛牛奶推廣軟文
mysql 數(shù)據(jù)庫(kù)空間統(tǒng)計(jì)
文章目錄
- mysql 數(shù)據(jù)庫(kù)空間統(tǒng)計(jì)
- 說明
- 一、數(shù)據(jù)庫(kù)存儲(chǔ)代碼
- 二、查詢某個(gè)數(shù)據(jù)庫(kù)的所有表的 代碼
- 總結(jié)
說明
INFORMATION_SCHEMA Table Reference 表參考
information_schema是?MySQL中的一個(gè)特殊數(shù)據(jù)庫(kù),它存儲(chǔ)了關(guān)于所有其他數(shù)據(jù)庫(kù)的元數(shù)據(jù)信息。 這些元數(shù)據(jù)包括數(shù)據(jù)庫(kù)名、表名、列的數(shù)據(jù)類型、訪問權(quán)限等。通過查詢information_schema,用戶可以獲取到關(guān)于數(shù)據(jù)庫(kù)結(jié)構(gòu)的詳細(xì)信息,這對(duì)于數(shù)據(jù)庫(kù)管理和優(yōu)化非常有幫助。例如,可以通過查詢information_schema來查看表的索引信息、視圖定義、存儲(chǔ)過程和函數(shù)的信息等。此外,由于information_schema中的表都是只讀的,它們實(shí)際上可以被視為視圖,因此用戶無(wú)法直接修改這些數(shù)據(jù),保證了元數(shù)據(jù)的完整性。?
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、數(shù)據(jù)庫(kù)存儲(chǔ)代碼
請(qǐng)注意
如果啟用了innodb_read_only系統(tǒng)變量,ANALYZE TABLE可能會(huì)失敗,因?yàn)樗鼰o(wú)法更新使用InnoDB的數(shù)據(jù)字典中的統(tǒng)計(jì)表。對(duì)于更新鍵分布的ANALYZE TABLE操作,即使操作更新表本身(例如,如果它是一個(gè)MyISAM表),也可能發(fā)生失敗。要獲取更新的分布統(tǒng)計(jì)信息,可以設(shè)置information_schema_stats_expiry=0。
代碼如下(GB)(示例):
select coalesce(table_schema, '合計(jì)') as table_schema,
concat(round(sum(data_length/1024/1024/1024),2),'GB') as data_length_GB,
concat(round(sum(index_length/1024/1024/1024),2),'GB') as index_length_GB ,
concat(round(sum(index_length/1024/1024/1024),2)+round(sum(data_length/1024/1024/1024),2),'GB') as tal_GB
from information_schema.tables t where table_Type='BASE TABLE'
and table_schema not in ('document','mysql','performance_schema','sys')
group by table_schema
代碼如下(MB)(示例):
select coalesce(table_schema, '合計(jì)') as table_schema,
concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB,
concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB ,
concat(round(sum(index_length/1024/1024),2)+round(sum(data_length/1024/1024),2),'MB') as tal_MB
from information_schema.tables t where table_Type='BASE TABLE'
and table_schema not in ('document','mysql','performance_schema','sys')
group by table_schema WITH ROLLUP order by round(sum(data_length/1024/1024),2) desc
二、查詢某個(gè)數(shù)據(jù)庫(kù)的所有表的 代碼
SELECTTABLE_NAME, ENGINE, VERSION, ROW_FORMAT, TABLE_ROWS, AVG_ROW_LENGTH,DATA_LENGTH, MAX_DATA_LENGTH, INDEX_LENGTH, DATA_FREE, AUTO_INCREMENT,CREATE_TIME, UPDATE_TIME, CHECK_TIME, TABLE_COLLATION, CHECKSUM,CREATE_OPTIONS, TABLE_COMMENTFROM INFORMATION_SCHEMA.TABLESWHERE table_schema = 'db_name'[AND table_name LIKE 'wild']SHOW TABLE STATUSFROM db_name[LIKE 'wild']
The following statements are equivalent:
SELECTTABLE_NAME, TABLE_TYPEFROM INFORMATION_SCHEMA.TABLESWHERE table_schema = 'db_name'[AND table_name LIKE 'wild']SHOW FULL TABLESFROM db_name[LIKE 'wild']
具體可以查看 mysql 幫助
https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
總結(jié)
information_schema用于存儲(chǔ)數(shù)據(jù)庫(kù)元數(shù)據(jù),本文sql 主要是 MySQL系統(tǒng)庫(kù)之information_schema的實(shí)現(xiàn),
查詢數(shù)據(jù)庫(kù)結(jié)構(gòu):information_schema 可用于查詢數(shù)據(jù)庫(kù)、表、列、索引、外鍵、觸發(fā)器等對(duì)象的結(jié)構(gòu)信息。
權(quán)限管理:可以使用 information_schema 查詢用戶和權(quán)限信息,以確保正確的訪問控制和權(quán)限設(shè)置。
性能優(yōu)化:information_schema 提供有關(guān)索引、表大小、表引擎等性能相關(guān)信息,這對(duì)于性能優(yōu)化很有幫助。
查詢執(zhí)行計(jì)劃:可以查詢 information_schema 獲取查詢執(zhí)行計(jì)劃,以了解查詢?nèi)绾伪粓?zhí)行。