MySQL INFORMATION_SCHEMA 使用
--查看创建的索引的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
FROM
INFORMATION_SCHEMA.TABLES T1,
INFORMATION_SCHEMA.STATISTICS T2
WHERE
T1.TABLE_SCHEMA = T2.TABLE_SCHEMA
AND T1.TABLE_NAME = T2.TABLE_NAME
AND T2.SEQ_IN_INDEX = (SELECT
MIN(T3.SEQ_IN_INDEX)
FROM
INFORMATION_SCHEMA.STATISTICS T3
WHERE
T2.TABLE_NAME = T3.TABLE_NAME
AND T2.TABLE_SCHEMA = T3.TABLE_SCHEMA
AND 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
FROM
information_schema.innodb_lock_waits t1,
information_schema.innodb_trx t2,
information_schema.innodb_trx t3
WHERE
t1.blocking_trx_id = t2.trx_id
AND t1.requesting_trx_id = t3.trx_id;
--查询出哪些表不是InnoDB引擎的
SELECT
TABLE_SCHEMA,
TABLE_NAME,
TABLE_TYPE,
ENGINE,
CREATE_TIME,
UPDATE_TIME,
TABLE_COLLATION
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_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_sql
FROM INFORMATION_SCHEMA.TABLES
WHERE 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
FROM
information_schema.tables
WHERE
table_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
FROM
performance_schema.threads
WHERE
type = '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
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_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
FROM
INFORMATION_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
FROM
information_schema.KEY_COLUMN_USAGE
WHERE
REFERENCED_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
FROM
information_schema.PARTITIONS
WHERE
table_schema = 'test'
AND table_name = 't';
-- 查看支持的插件
SELECT
PLUGIN_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
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_TYPE = 'PROCEDURE'
AND ROUTINE_SCHEMA="employees";
-- 查看存在的数据库及字符集信息
SELECT
SCHEMA_NAME,
DEFAULT_CHARACTER_SET_NAME,
DEFAULT_COLLATION_NAME
FROM
INFORMATION_SCHEMA.SCHEMATA; SHOW DATABASES;
-- 查看索引信息
SELECT
table_schema,
table_name,
index_name,
COLUMN_NAME,
COLLATION,
CARDINALITY,
index_type
FROM
INFORMATION_SCHEMA.STATISTICS
WHERE
table_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
FROM
information_schema.TABLES
WHERE
ENGINE 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;
MySQL INFORMATION_SCHEMA 使用的更多相关文章
- (转)十分钟了结MySQL information_schema
十分钟了结MySQL information_schema 原文:http://www.cnblogs.com/shengdimaya/p/6920677.html information_sche ...
- mysql information_schema介绍
mysql information_schema介绍 一.information_schema是什么 information_schema是MySQL自带的一个信息数据库,其保存着关于MySQL服务器 ...
- MySQL information_schema表查询导致内存暴涨
case:下面的一条sql语句,导致mysql实例内存暴涨: select * from tables where table_name not in(select table_name from p ...
- 十分钟了结MySQL information_schema
information_schema数据库是MySQL系统自带的数据库,它提供了数据库元数据的访问方式.感觉information_schema就像是MySQL实例的一个百科全书,记录了数据库当中大部 ...
- mysql—information_schema数据库
一.介绍 MySQL中有一个默认数据库名为information_schema,在MySQL中我们把 information_schema 看作是一个数据库,确切说是信息数据库.其中保存着关于MySQ ...
- mysql information_schema 数据库简介:
.CHARACTER_SETS 表 CREATE TEMPORARY TABLE `CHARACTER_SETS` ( `CHARACTER_SET_NAME` varchar() NOT NULL ...
- mysql information_schema
SELECT TABLE_NAME,COLUMN_NAME,CHARACTER_MAXIMUM_LENGTH,COLUMN_COMMENT FROM COLUMNS WHERE TABLE_SCHEM ...
- [MySQL] INFORMATION_SCHEMA 数据库包含所有表的字段
sql注入后可以通过该数据库获取所有表的字段信息 1. COLLATIONS表 提供有关每个字符集的排序规则的信息. COLLATIONS表包含以下列: COLLATION_NAME 排序规则名称. ...
- mysql performance_schema 和information_schema.tables了解
这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql information_schema 信息相关 performance_schema 性能相关 ...
随机推荐
- ASP.NET.Identity 加密算法
public static string HashPassword(string password) { if (password == null) { throw new ArgumentNullE ...
- mac下搭建基于vue-cli 3.0的Element UI 项目
1.安装yarn管理工具(包含node.js); 2.安装全局vue-cli全家桶: yarn global add @vue/cli 3.创建.测试一个vue-cli项目: vue create a ...
- 百度云如何免费扩容至2055G?
百度云如何免费扩容至2055G? 上篇说到整一个新的百度账号,那么5G的百度云内存肯定满足不了我们收集癖的需求.那么就来了解一下怎么扩容吧. 主要是在手机端实现的 用这个新的百度账号在手机APP上登录 ...
- mybatis的操作数据库基础
1.domain类 package com.xiaostudy.mybatis.domain; /** * @desc domain类 * @author xiaostudy * */ public ...
- php入门(一)
一,在HTML中嵌入php代码 先看html的代码: <form action="processorder.php" method="post"> ...
- oracle in语句的坑
oracle 的in语句最多只能有1000条数据,超出,sql报错.
- vc++获取系统网卡列表及IP地址信息
#include "stdafx.h" #include <Windows.h> #include <IPHlpApi.h> #include <io ...
- Java Applet基础
applet是一种Java程序.它一般运行在支持Java的Web浏览器内.因为它有完整的Java API支持,所以applet是一个全功能的Java应用程序. 如下所示是独立的Java应用程序和app ...
- Android 版本升级涉及到的数据库数据迁移问题
最近做老版本向新版本升级,新版本增加了几张表,有的表经过了增加字段.那么如何把老的数据迁移到新的版本里呢? 我写了一段伪代码,是关于我们项目里的 用户登录信息 Users表, 历史表histor ...
- 设计模式--命令模式C++实现
命令模式C++实现 1定义 将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求队列或者记录请求日志,可以提供命令的撤销和恢复功能 2类图 角色描述: Receiver接受者角色,就 ...