【mysql元數據庫】使用information_schema.tables查詢數據庫和數據表信息




概述

對於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:表示需要執行的指令:
































免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM