MySQL INFORMATION_SCHEMA 使用
一、INFORMATION_SCHEMA介紹
第一個查詢看看庫里有多少個表,表名等
select * from INFORMATION_SCHEMA.TABLES
information_schema這張數據表保存了MySQL服務器所有數據庫的信息。如數據庫名,數據庫的表,表欄的數據類型與訪問權限等。再簡單點,這臺mysql服務器上,到底有哪些數據庫、各個數據庫有哪些表,每張表的字段類型是什么,各個數據庫要什么權限才能訪問,等等信息都保存在information_schema表里面。
Mysql的INFORMATION_SCHEMA數據庫包含了一些表和視圖,提供了訪問數據庫元數據的方式。
元數據是關于數據的數據,如數據庫名或表名,列的數據類型,或訪問權限等。有些時候用于表述該信息的其他術語包括“數據詞典”和“系統目錄”。
?
下面對一些重要的數據字典表做一些說明:
SCHEMATA表:提供了關于數據庫的信息。
TABLES表:給出了關于數據庫中的表的信息。
COLUMNS表:給出了表中的列信息。
STATISTICS表:給出了關于表索引的信息。
USER_PRIVILEGES表:給出了關于全程權限的信息。該信息源自mysql.user授權表。
SCHEMA_PRIVILEGES表:給出了關于方案(數據庫)權限的信息。該信息來自mysql.db授權表。
TABLE_PRIVILEGES表:給出了關于表權限的信息。該信息源自mysql.tables_priv授權表。
COLUMN_PRIVILEGES表:給出了關于列權限的信息。該信息源自mysql.columns_priv授權表。
CHARACTER_SETS表:提供了關于可用字符集的信息。
COLLATIONS表:提供了關于各字符集的對照信息。
COLLATION_CHARACTER_SET_APPLICABILITY表:指明了可用于校對的字符集。
TABLE_CONSTRAINTS表:描述了存在約束的表。
KEY_COLUMN_USAGE表:描述了具有約束的鍵列。
ROUTINES表:提供了關于存儲子程序(存儲程序和函數)的信息。此時,ROUTINES表不包含自定義函數(UDF)。
VIEWS表:給出了關于數據庫中的視圖的信息。
TRIGGERS表:提供了關于觸發程序的信息。
二、使用示例
--查看創建的索引的CARDINALITY比率
--通常cardinality達到表數據的10%左右建索引會有意義--如果是一個組合索引,索引第一位的cardinality表示第一個列的cardinality大小,第二列表示第一列和第二列共同的cardinality值
SELECT T1.TABLE_SCHEMA,T1.TABLE_NAME,T2.INDEX_NAME,ROUND(T2.CARDINALITY / T1.TABLE_ROWS * 100, 2) AS RATE FROMINFORMATION_SCHEMA.TABLES T1,INFORMATION_SCHEMA.STATISTICS T2 WHERET1.TABLE_SCHEMA = T2.TABLE_SCHEMAAND T1.TABLE_NAME = T2.TABLE_NAMEAND T2.SEQ_IN_INDEX = (SELECT MIN(T3.SEQ_IN_INDEX)FROMINFORMATION_SCHEMA.STATISTICS T3WHERET2.TABLE_NAME = T3.TABLE_NAMEAND T2.TABLE_SCHEMA = T3.TABLE_SCHEMAAND T2.INDEX_NAME = T3.INDEX_NAME) AND T1.TABLE_SCHEMA NOT IN ('MYSQL','PERFORMANCE_SCHEMA','INFORMATION_SCHEMA','SYS') AND T1.TABLE_ROWS >=100 ORDER BY RATE;
--查看鎖阻塞
-- 查看鎖的SQL SELECT t3.trx_id waiting_trx_id,t3.trx_mysql_thread_id waiting_thread,t3.trx_query waiting_query,t2.trx_id blocking_trx_id,t2.trx_mysql_thread_id blocking_thread,t2.trx_query blocking_query FROMinformation_schema.innodb_lock_waits t1,information_schema.innodb_trx t2,information_schema.innodb_trx t3 WHEREt1.blocking_trx_id = t2.trx_idAND t1.requesting_trx_id = t3.trx_id;--查詢出哪些表不是InnoDB引擎的
SELECT TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,CREATE_TIME,UPDATE_TIME,TABLE_COLLATION FROMINFORMATION_SCHEMA.TABLES WHERETABLE_SCHEMA NOT IN ('information_schema' , 'mysql', 'performance_schema', 'sys')AND ENGINE <> 'InnoDB';--生成修改存儲引擎的語句
SELECT -- TABLE_SCHEMA,-- TABLE_NAME,-- TABLE_TYPE,-- ENGINE,-- CREATE_TIME,-- UPDATE_TIME,-- TABLE_COLLATION,CONCAT('alter table ', TABLE_SCHEMA,'.',TABLE_NAME, ' engine=InnoDB;') AS alter_sqlFROM INFORMATION_SCHEMA.TABLESWHERE TABLE_SCHEMA NOT IN('information_schema', 'mysql', 'performance_schema', 'sys')AND ENGINE <> 'InnoDB';-- ?查看指定數據庫的表信息
SET @table_schema='employees'; SELECT table_name,table_type,engine,table_rows,avg_row_length,data_length,index_length,table_collation,create_time FROMinformation_schema.tables WHEREtable_schema = @table_schema ORDER BY table_name;-- 查看會話連接信息
SELECT THREAD_ID,name,type,PROCESSLIST_ID,PROCESSLIST_USER AS user,PROCESSLIST_HOST AS host,PROCESSLIST_DB AS db,PROCESSLIST_COMMAND AS cmd,PROCESSLIST_TIME AS time,PROCESSLIST_STATE AS state,PROCESSLIST_INFO AS info,CONNECTION_TYPE AS type,THREAD_OS_ID AS os_id FROMperformance_schema.threads WHEREtype = 'FOREGROUND' ORDER BY THREAD_ID;-- CHARACTER_SETS 查看數據庫支持的字符集
SELECT * FROM INFORMATION_SCHEMA.CHARACTER_SETS WHERE CHARACTER_SET_NAME LIKE 'utf%';SHOW CHARACTER SET LIKE 'utf%';-- COLLATIONS ?字符序
-- 用于指定數據集如何排序,以及字符串的比對規則 SELECT * FROM INFORMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME LIKE 'utf%';SHOW COLLATION LIKE 'utf%';-- 查看表結構定義信息
SELECT table_name,COLUMN_NAME,ordinal_position,DATA_TYPE,IS_NULLABLE,COLUMN_DEFAULT,column_type,column_key,character_set_name,collation_name FROMINFORMATION_SCHEMA.COLUMNS WHEREtable_name = 'employees'AND table_schema = 'employees';show columns from employees from employees;
desc employeees.employees;
-- 查看支持的引擎
SELECT * FROM INFORMATION_SCHEMA.ENGINES; show ENGINES;-- 查看數據庫的數據文件信息
SELECT FILE_ID,FILE_NAME,FILE_TYPE,TABLESPACE_NAME,FREE_EXTENTS,TOTAL_EXTENTS,((TOTAL_EXTENTS - FREE_EXTENTS) * EXTENT_SIZE) / 1024 / 1024 AS MB_used,EXTENT_SIZE,INITIAL_SIZE,MAXIMUM_SIZE,AUTOEXTEND_SIZE,DATA_FREE,STATUS,ENGINE FROMINFORMATION_SCHEMA.FILES;-- 查看指定表的約束
SELECT constraint_schema,table_name,constraint_name,column_name,ordinal_position,CONCAT(table_name,'.',column_name,' -> ',referenced_table_name,'.',referenced_column_name) AS list_of_fks FROMinformation_schema.KEY_COLUMN_USAGE WHEREREFERENCED_TABLE_SCHEMA = 'employees'AND REFERENCED_TABLE_NAME IS NOT NULL ORDER BY TABLE_NAME , COLUMN_NAME;-- 查看指定分區表信息
SELECT TABLE_SCHEMA,table_name,partition_name,subpartition_name sub_par,partition_ordinal_position par_position,partition_method method,partition_expression expression,partition_description description,table_rows FROMinformation_schema.PARTITIONS WHEREtable_schema = 'test'AND table_name = 't';-- 查看支持的插件
SELECTPLUGIN_NAME, PLUGIN_STATUS, PLUGIN_TYPE,PLUGIN_LIBRARY, PLUGIN_LICENSE FROM INFORMATION_SCHEMA.PLUGINS;SHOW PLUGINS;-- 查看數據庫連接信息
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST;SHOW FULL PROCESSLIST;-- 查看數據庫中的存儲過程、函數等
SELECT ROUTINE_SCHEMA,routine_name,ROUTINE_TYPE,data_type,routine_body,routine_definition,routine_comment FROMINFORMATION_SCHEMA.ROUTINES WHEREROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_SCHEMA="employees";-- 查看存在的數據庫及字符集信息
SELECT SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME,DEFAULT_COLLATION_NAME FROMINFORMATION_SCHEMA.SCHEMATA;SHOW DATABASES;-- 查看索引信息
SELECT table_schema,table_name,index_name,COLUMN_NAME,COLLATION,CARDINALITY,index_type FROMINFORMATION_SCHEMA.STATISTICS WHEREtable_name = 'employees'AND table_schema = 'employees';SHOW INDEX FROM employees FROM employees;
-- 查看數據庫大小
SELECT table_schema 'database',CONCAT(ROUND(SUM(data_length + index_length) / (1024 * 1024),2),'M') size FROMinformation_schema.TABLES WHEREENGINE in ('MyISAM','InnoDB') GROUP BY table_schema;-- 查看表大小
SELECT?
CONCAT(table_schema, '.', table_name) table_name,
CONCAT(ROUND(data_length / (1024 * 1024), 2),
'M') data_length,
CONCAT(ROUND(index_length / (1024 * 1024), 2),
'M') index_length,
CONCAT(ROUND(ROUND(data_length + index_length) / (1024 * 1024),
2),
'M') total_size,
engine
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema' , 'performance_schema', 'sys', 'mysql')
ORDER BY data_length DESC;
轉自:http://www.cnblogs.com/zhenxing/p/5340592.html
轉載于:https://www.cnblogs.com/duanxz/p/3757511.html
總結
以上是生活随笔為你收集整理的MySQL INFORMATION_SCHEMA 使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: *[topcoder]Increment
- 下一篇: 安卓手机关闭底部键盘灯的方法(htc G