(轉)【mysql元數據庫】使用information_schema.tables查詢數據庫和數據表信息 ---數據記錄大小統計

轉:https://www.cnblogs.com/ssslinppp/p/6178636.html https://segmentfault.com/q/1010000007268994?_ea=1290889

若是想要知道Mysql數據庫中每一個表佔用的空間、表記錄的行數的話,能夠打開mysql的information_schema數據庫。在該庫中有個Tables表,這個表主要字段分別是:
TABLE_SCHEMA:數據庫名
TABLE_NAME:表名
ENGINE:所使用的存儲引擎
TABLES_ROWS:記錄數
DATA_LENGTH:數據大小
INDEX_LENGTH:索引大小
因此要知道一個表佔用空間的大小,那就至關因而 數據大小 + 索引大小便可。html

概述

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

 

information_schema.tables信息;

  1. use information_schema;
  2. show create table tables;
  1. desc tables;

查詢全部的數據庫信息

  1. select distinct TABLE_SCHEMA from tables ;

查詢數據庫和數據表信息

顯示mysql數據庫下面的全部表信息:(共對比使用)
  1. use mysql;
  2. show tables;
經過information_schema.table獲取數據庫和數據表信息:
  1. use information_schema;
  2. select TABLE_SCHEMA ,table_name from tables where table_schema like 'mysql';

數據表大小以及索引大小

示例1:mysql.time_zone相關表
獲取time_zone相關表的大小:
  1. select (sum(DATA_LENGTH) + sum(INDEX_LENGTH)) as size from tables where table_schema='mysql' and table_name like 'time_%';
 
示例2: 獲取指定數據庫的大小;
  1. select (sum(DATA_LENGTH) + sum(INDEX_LENGTH)) as size from tables where table_schema='mysql';

判斷myisam數據表是否已壓縮

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

經過Linux指令直接獲取數據庫和數據表信息:

  1. mysql -uroot -pxxxx -D information_schema -e "select TABLE_SCHEMA ,table_name from tables where table_schema like 'hsm_syslog_%'"
參數說明:
  • -D:表示數據庫名稱 
  • -e:表示須要執行的指令:
相關文章
相關標籤/搜索