1、查看数据库中不为 InnoDB 引擎的表
SELECT TABLE_SCHEMA, TABLE_NAME, ENGINE
FROM information_schema.TABLES
WHERE TABLE_SCHEMA NOT IN
('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
AND ENGINE != 'InnoDB';
2、查看数据库中表的大小及数据量
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS,
CONCAT(round((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024, 2), 'MB') as data
FROM information_schema.TABLES
WHERE TABLE_SCHEMA NOT IN
('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
ORDER BY DATA_LENGTH + INDEX_LENGTH DESC;
3、查找数据库中无显式主键索引的表
SELECT t.TABLE_SCHEMA, t.TABLE_NAME
FROM information_schema.TABLES t
WHERE (t.TABLE_SCHEMA, t.TABLE_NAME) NOT IN
(SELECT DISTINCT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.COLUMNS
WHERE COLUMN_KEY = 'PRI')
AND t.TABLE_SCHEMA NOT IN
('sys', 'mysql', 'performance_schema', 'information_schema', 'test');
4、查找数据库中主键为联合主键的表
SELECT TABLE_SCHEMA, TABLE_NAME,
GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ',') cols,
MAX(SEQ_IN_INDEX) len
FROM information_schema.STATISTICS
WHERE INDEX_NAME = 'PRIMARY'
AND TABLE_SCHEMA NOT IN
('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
GROUP BY TABLE_SCHEMA, TABLE_NAME
HAVING len > 1;
5、查找数据库中不为自增主键的表
SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
AND (TABLE_SCHEMA,TABLE_NAME) NOT IN
(SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
AND IS_NULLABLE = 'NO'
AND COLUMN_TYPE LIKE '%int%'
AND COLUMN_KEY = 'PRI'
AND EXTRA = 'auto_increment');
6、查看数据库中存在外键约束的表
SELECT c.TABLE_SCHEMA,
c.REFERENCED_TABLE_NAME,
c.REFERENCED_COLUMN_NAME,
c.TABLE_NAME,
c.COLUMN_NAME,
c.CONSTRAINT_NAME,
t.TABLE_COMMENT,
r.UPDATE_RULE,
r.DELETE_RULE
FROM information_schema.KEY_COLUMN_USAGE c
JOIN information_schema.TABLES t
ON t.TABLE_NAME = c.TABLE_NAME
JOIN information_schema.REFERENTIAL_CONSTRAINTS r
ON r.TABLE_NAME = c.TABLE_NAME
AND r.CONSTRAINT_NAME = c.CONSTRAINT_NAME
AND r.REFERENCED_TABLE_NAME = c.REFERENCED_TABLE_NAME
WHERE c.REFERENCED_TABLE_NAME IS NOT NULL;
7、查找数据库中低区分度索引(区分度小于0.1)
SELECT p.TABLE_SCHEMA, p.TABLE_NAME, c.INDEX_NAME, c.car, p.car total
FROM (SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME, MAX(CARDINALITY) car
FROM information_schema.STATISTICS
WHERE INDEX_NAME != 'PRIMARY'
AND TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME) c
INNER JOIN (SELECT TABLE_SCHEMA, TABLE_NAME, MAX(CARDINALITY) car
from information_schema.STATISTICS
WHERE INDEX_NAME = 'PRIMARY'
AND TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
GROUP BY TABLE_SCHEMA, TABLE_NAME) p
ON c.TABLE_NAME = p.TABLE_NAME
AND c.TABLE_SCHEMA = p.TABLE_SCHEMA
WHERE p.car > 0
AND c.car / p.car < 0.1;
8、查找数据库中重复索引前缀的索引
SELECT a.TABLE_SCHEMA, a.TABLE_NAME, a.INDEX_NAME, a.cols, b.INDEX_NAME, b.cols
FROM (SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME,
CONCAT('| ', GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ' | '), ' |') AS cols
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
AND INDEX_NAME != 'PRIMARY'
GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME) a
INNER JOIN (SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME,
CONCAT('| ', GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ' | '), ' |') AS cols
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
AND INDEX_NAME != 'PRIMARY'
GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME) b
ON a.TABLE_NAME = b.TABLE_NAME
AND a.TABLE_SCHEMA = b.TABLE_SCHEMA
AND a.cols LIKE CONCAT(b.cols, '%')
AND a.INDEX_NAME != b.INDEX_NAME;
9、查找数据库中包索引重复包含主键列的索引
SELECT a.*, b.pk
FROM (SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME,
CONCAT('| ', GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ' | '), ' |') cols
FROM information_schema.STATISTICS
WHERE INDEX_NAME != 'PRIMARY'
AND TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
GROUP BY TABLE_SCHEMA, TABLE_NAME, INDEX_NAME) a
INNER JOIN (SELECT TABLE_SCHEMA, TABLE_NAME,
CONCAT('| ', GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX SEPARATOR ' | '), ' |') pk
FROM information_schema.STATISTICS
WHERE INDEX_NAME = 'PRIMARY'
AND TABLE_SCHEMA NOT IN ('sys', 'mysql', 'performance_schema', 'information_schema', 'test')
GROUP BY TABLE_SCHEMA, TABLE_NAME) b
ON a.TABLE_NAME = b.TABLE_NAME
AND a.TABLE_SCHEMA = b.TABLE_SCHEMA
AND a.cols LIKE CONCAT('%', b.pk, '%');
10、查找数据库中没有被使用的索引
SELECT a.OBJECT_SCHEMA, a.OBJECT_NAME, a.INDEX_NAME, b.TABLE_ROWS
FROM performance_schema.TABLE_IO_WAITS_SUMMARY_BY_INDEX_USAGE a
INNER JOIN information_schema.TABLES b
ON a.OBJECT_SCHEMA = b.TABLE_SCHEMA
AND a.OBJECT_NAME = b.TABLE_SCHEMA
WHERE a.INDEX_NAME IS NOT NULL
AND a.INDEX_NAME != 'PRIMARY'
AND a.COUNT_STAR = 0
AND OBJECT_SCHEMA NOT IN
('sys', 'mysql', 'performance_schema', 'information_schema')
ORDER BY OBJECT_SCHEMA, OBJECT_NAME;
11、查看数据库中的锁请求信息
SELECT r.TRX_ISOLATION_LEVEL,
r.TRX_ID WAITING_TRX_ID,
r.TRX_MYSQL_THREAD_ID WAITING_TRX_THREAD,
r.TRX_STATE WAITING_TRX_STATE,
lr.LOCK_MODE WAITING_TRX_LOCK_MODE,
lr.LOCK_TYPE WAITING_TRX_LOCK_TYPE,
lr.LOCK_TABLE WAITING_TRX_LOCK_TABLE,
lr.LOCK_INDEX WAITING_TRX_LOCK_INDEX,
r.TRX_QUERY WAITING_TRX_QUERY,
b.TRX_ID BLOCKING_TRX_ID,
b.TRX_MYSQL_THREAD_ID BLOCKING_TRX_THREAD,
b.TRX_STATE BLOCKING_TRX_STATE,
lb.LOCK_MODE BLOCKING_TRX_LOCK_MODE,
lb.LOCK_TYPE BLOCKING_TRX_LOCK_TYPE,
lb.LOCK_TABLE BLOCKING_TRX_LOCK_TABLE,
lb.LOCK_INDEX BLOCKING_TRX_LOCK_INDEX,
b.TRX_QUERY BLOCKING_QUERY
FROM information_schema.INNODB_LOCK_WAITS w
INNER JOIN INFORMATION_SCHEMA.INNODB_TRX b
ON b.TRX_ID = W.BLOCKING_TRX_ID
INNER JOIN information_schema.INNODB_TRX R
ON r.TRX_ID = W.REQUESTING_TRX_ID
INNER JOIN information_schema.INNODB_LOCKS lb
ON lb.LOCK_TRX_ID = W.BLOCKING_TRX_ID
INNER JOIN information_schema.INNODB_LOCKS lr
ON lr.LOCK_TRX_ID = W.REQUESTING_TRX_ID;
- 完整备份和差异备份数据库的SQL脚本
工作中需要创建SQL Job对数据库进行定期备份,现把脚本记录如下. 1. 完整备份: -- FULL declare @filename varchar(1024), @file_dev varch ...
- Mysql数据库导出sql脚本
1. 运行环境Centos mysqldump -h localhost -u root -p etv > ./etv.sql etv 是要导出的数据库名 > 设置导出的路径和文件名
- MSSQL生成整个数据库的SQL脚本的工具 scptxfr.exe
scptxfr.exe的路径要正确declare @cMd varchar(1000)set @cmd = 'master.dbo.xp_cmdshell ' + '''c:\"Micros ...
- [SQL SERVER] - 还原数据库备份(SQL脚本),抛出 "System.OutOfMemoryException" 异常之解决
背景 在 Microsoft SQL Server 2016 的查询窗体中,直接还原备份数据库的 SQL 脚本(99MB),抛出 Cannot execute script 异常: 原因 相关资料说: ...
- 数据库备份及SQL脚本导入
数据库备份及SQL脚本导入 数据导出 su - oracle exp 数据库用户名/数据库密码@ORCL file=20190905.dmp full=y SQL脚本导入 首先导入前查看Oracle用 ...
- 如何执行一个mysql的sql脚本文件
sql脚本是包含一到多个sql命令的sql语句,我们可以将这些sql脚本放在一个文本文件中(我们称之为“sql脚本文件”),然后通过相关的命令执行这个sql脚本文件.基本步骤如下:一.创建包含sql命 ...
- mysql执行sql脚本
最近用mysql执行sql脚本,遇到一些问题,顺便记录一下笔记. 首先,先开启mysql服务,创建一个空数据库(脚本里没有创建数据库) 执行脚本有两个方法 1.未连接数据库:在Windows下使用cm ...
- mysql执行sql脚本文件
mysql执行sql脚本文件 方法一:使用cmd命令执行(windows下,unix或Linux在的其控制台下) [MySQL的bin目录]\mysql –u用户名 –p密码 –D数据库<[sq ...
- 使用命令执行 sql 脚本文件
使用命令执行 sql 脚本文件 方法: 在 Windows 下使用 cmd 命令执行(或 Unix 或 Linux 控制台下)[Mysql的bin目录]\mysql –u用户名 –p密码 –D数据库名 ...
随机推荐
- Spring中事务的XML方式[声明方式]
事务管理: 管理事务,管理数据,数据完整性和一致性 事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务], 当我们动作当中有一个错了,全错~ ACID 原子 ...
- affix附加导航插件
<style> a:focus { outline: none; } .nav-pills { width: 150px; } .nav-pills.affix { top : 10px; ...
- Cisco安全防护读书笔记之一Cisco系统设备协议漏洞
650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...
- Spring源码分析专题——目录
Spring源码分析专题 -- 阅读指引 IOC容器 Spring源码分析专题 -- IOC容器启动过程(上篇) Spring源码分析专题 -- IOC容器启动过程(中篇) Spring源码分析专题 ...
- Docker+SVN
原文:Docker+SVN mkdir /cnex/svndocker pull garethflowers/svn-server docker run -d --name svn-server - ...
- 20款PHP版WebMail开源项目
20款PHP版WebMail开源项目 如今互联网巨头提供的企业应用套件中邮件托管是必备服务,而且还始终秉承免费的优良光荣传统,最为让人熟识的恐怕非"瘟多死里屋管理中心"和" ...
- ie为什么那么垃圾(不是ie垃圾,是ie用的人太多了,很多在用低版本)
ie为什么那么垃圾(不是ie垃圾,是ie用的人太多了,很多在用低版本) 一.总结 1.我们觉得ie差的原因:我们拿老的ie和最新的其它浏览器做比较了,两者相差了很多年.比较微软几十年才发布了11个ie ...
- 20.SpringBoot 之 读取环境变量和绑定属性对象
转自:https://blog.csdn.net/catoop/article/details/50548009
- Linux 解压缩命令整理
一.tar命令 参数 参数 详解 参数 详解 -c 可以使用绝对路径来压缩 -x 解开一个压缩文件的参数指令 -t 查看内容 -r 向压缩归档文件末尾追加文件 -u 更新原压缩包中的文件 -z 有gz ...
- VUE笔记 - 插值表达式 v-on: / @ 事件绑定 定时器运用
<body> <!-- 2. 创建一个要控制的区域 --> <div id="app"> <input type="button ...