
概述
對於mysql和Infobright等數據庫,information_schema數據庫中的表都是只讀的,不能進行更新、刪除和插入等操作,也不能加觸發器,因為它們實際只是一個視圖,不是基本表,沒有關聯的文件。
information_schema.tables存儲了數據表的元數據信息,下面對常用的字段進行介紹:
- table_schema: 記錄數據庫名;
- table_name: 記錄數據表名;
- engine : 存儲引擎;
- table_rows: 關於表的粗略行估計;
- data_length : 記錄表的大小(單位字節);
- index_length : 記錄表的索引的大小;
- row_format: 可以查看數據表是否壓縮過;
下面介紹幾種常見的用法;
information_schema.tables信息;
use information_schema;
show create table tables;

desc tables;

查詢所有的數據庫信息
select distinct TABLE_SCHEMA from tables ;

查詢數據庫和數據表信息
顯示mysql數據庫下面的所有表信息:(共對比使用)
use mysql;
show tables;

通過information_schema.table獲取數據庫和數據表信息:
use information_schema;
select TABLE_SCHEMA ,table_name from tables where table_schema like 'mysql';

數據表大小以及索引大小
示例1:mysql.time_zone相關表

獲取time_zone相關表的大小:
select (sum(DATA_LENGTH) + sum(INDEX_LENGTH)) as size from tables where table_schema='mysql' and table_name like 'time_%';

示例2: 獲取指定數據庫的大小;
select (sum(DATA_LENGTH) + sum(INDEX_LENGTH)) as size from tables where table_schema='mysql';

判斷myisam數據表是否已壓縮
select distinct row_format,engine from information_schema.tables where engine='myisam';

- Fixed: 表示已壓縮;
- Dynamic:表示未壓縮;
select row_format,engine,table_name from information_schema.tables where engine='myisam';

通過Linux指令直接獲取數據庫和數據表信息:
mysql -uroot -pxxxx -D information_schema -e "select TABLE_SCHEMA ,table_name from tables where table_schema like 'hsm_syslog_%'"

參數說明:
- -D:表示數據庫名稱
;
- -e:表示需要執行的指令:
;