1、查看当前应用连接,连接数突增排查

select user,SUBSTRING_INDEX(host,':',1) as ip , count(*) as count,db from information_schema.processlist where host not in ('localhost') and user not in ('replicater') group by ip order by count;

2、查看表所属及大概行数,一般加字段索引时做参考

select TABLE_SCHEMA,TABLE_NAME,TABLE_ROWS from information_schema.tables where TABLE_NAME='表名';

3、查看表碎片,是否需要整理表释放物理空间

select table_name,table_rows,concat(round(DATA_LENGTH/1024/1024, 2), 'MB') as size,DATA_FREE/1024/1024 AS data_free_MB from information_schema.TABLES where table_schema='库名' order by DATA_LENGTH desc;

4、当前有没有锁

select * from information_schema.innodb_locks;

5、当前锁堵塞情况

select * from information_schema.innodb_lock_waits;

6、当前锁等待详细信息

select it.trx_mysql_thread_id, il.lock_id, il.lock_table, il.lock_mode, il.lock_type, it.trx_state, pl.USER||'@'||pl.HOST as user_host, pl.db, pl.command, pl.info, it.trx_started, it.trx_wait_started, now()-trx_wait_started as wait_seconds, il.lock_index, it.trx_weight, it.trx_rows_locked, it.trx_rows_modified from information_schema.INNODB_TRX it,information_schema.innodb_locks il,information_schema.processlist pl where it.trx_id = il.lock_trx_id and it.trx_mysql_thread_id = pl.id\G

7、最近一次死锁、未提交事物、CHECKPIONT、BUFFER POOL等

show engine innodb status\G

8、过滤无用线程信息可用pager

pager grep -v Sleep;show processlist;

9、查看当前运行的详细SQL

SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE info is not null\G

10、查看某条sql各阶段执行时间,可开启profiling功能

set global profiling=on;

11、查看用户信息

select user,host,password from mysql.user group by user;

12、分表时批量生成sql语句

select concat("select IP as ",TABLE_NAME," from ",TABLE_SCHEMA,".",TABLE_NAME," group by id;") from information_schema.TABLES where TABLE_NAME like 'table_%';

(注意:PERFORMANCE_SCHEMA是在5.5开始新增一个数据库,MySQL5.5默认是关闭的,需要手动开启,5.6默认开启)

[mysqld]

performance_schema=ON

13、查看哪些sql执行最多

SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY COUNT_STAR desc LIMIT 1;

14、哪个SQL平均响应时间最多

SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,AVG_TIMER_WAIT,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY AVG_TIMER_WAIT desc LIMIT 1\G

15、哪个SQL扫描的行数最多(IO消耗)

SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,AVG_TIMER_WAIT,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_ROWS_EXAMINED desc LIMIT 1\G

16、哪个SQL使用的临时表最多

SELECT SCHEMA_NAME,DIGEST_TEXT,SUM_CREATED_TMP_DISK_TABLES,SUM_CREATED_TMP_TABLES,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_CREATED_TMP_DISK_TABLES desc LIMIT 1\G

17、哪个SQL返回的结果集最多(net消耗)

SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_ROWS_SENT,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_ROWS_SENT desc LIMIT 1\G

18、哪个SQL排序数最多(CPU消耗)

SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_SORT_ROWS,FIRST_SEEN,LAST_SEEN FROM performance_schema.events_statements_summary_by_digest where SCHEMA_NAME is not null and SCHEMA_NAME !='information_schema' ORDER BY SUM_SORT_ROWS desc LIMIT 1\G

19、哪个表、文件逻辑IO最多(热数据)

SELECT FILE_NAME,EVENT_NAME,COUNT_READ,SUM_NUMBER_OF_BYTES_READ,COUNT_WRITE,SUM_NUMBER_OF_BYTES_WRITE FROM performance_schema.file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ+SUM_NUMBER_OF_BYTES_WRITE DESC LIMIT 2\G

20、哪个索引使用最多

SELECT OBJECT_NAME, INDEX_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE FROM performance_schema.table_io_waits_summary_by_index_usage ORDER BY SUM_TIMER_WAIT DESC limit 1;

21、哪个索引没有使用过

SELECT OBJECT_SCHEMA, OBJECT_NAME, INDEX_NAME FROM performance_schema.table_io_waits_summary_by_index_usage WHERE INDEX_NAME IS NOT NULL AND COUNT_STAR = 0 AND OBJECT_SCHEMA <> 'mysql' ORDER BY OBJECT_SCHEMA,OBJECT_NAME;

22、哪个等待事件消耗的时间最多

SELECT EVENT_NAME, COUNT_STAR, SUM_TIMER_WAIT, AVG_TIMER_WAIT FROM events_waits_summary_global_by_event_name WHERE event_name != 'idle' ORDER BY SUM_TIMER_WAIT DESC LIMIT 1;

23、通过performance_schema库得到数据库运行的统计信息,更好分析定位问题和完善监控信息

打开标准的innodb监控:

CREATE TABLE innodb_monitor (a INT) ENGINE=INNODB;

打开innodb的锁监控:

CREATE TABLE innodb_lock_monitor (a INT) ENGINE=INNODB;

打开innodb表空间监控:

CREATE TABLE innodb_tablespace_monitor (a INT) ENGINE=INNODB;

打开innodb表监控:

CREATE TABLE innodb_table_monitor (a INT) ENGINE=INNODB;

24、添加主键

alter table xxx add constraint primary key(id);

25、删除外键

alter table test drop foreign key FK_XXX;

26、QPS

SHOW GLOBAL STATUS LIKE 'Questions';
SHOW GLOBAL STATUS LIKE 'Uptime';
Questions/Uptime

27、TPS

SHOW GLOBAL STATUS LIKE 'Com_commit';
SHOW GLOBAL STATUS LIKE 'Com_rollback';
SHOW GLOBAL STATUS LIKE 'Uptime';
(Com_commit + Com_rollback)/Uptime

27、导出

select * from MacConfig into outfile '/tmp/MacConfig.sql' fields terminated by ',' lines terminated by '\n';

28、导入

load data infile '/tmp/MacConfig.sql' into table MacConfig fields terminated by ',' lines terminated by '\n';

29、清理binlog

PURGE BINARY LOGS TO 'XXX';

PURGE BINARY LOGS BEFORE '2017-11-09 00:00:00';

30、外键隔离级别等信息

select @@FOREIGN_KEY_CHECKS;
select @@global.tx_isolation,@@tx_isolation;
select @@character_set_database;
select @@GLOBAL.sql_mode;

MySQL 常用SQL 汇总的更多相关文章

  1. mysql常用sql汇总

    给一张表新增一个字段 ALTER table student add zz INT() DEFAULT COMMENT '0是授权 1未授权' 给表student 新增一个zz的字段 默认是0 后面是 ...

  2. MySQL常用SQL整理

    MySQL常用SQL整理 一.DDL #创建数据库 CREATE DATABASE IF NOT EXISTS product DEFAULT CHARSET utf8 COLLATE utf8_ge ...

  3. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  4. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  5. 【SQL实践】其他常用SQL汇总

    [SQL实践]其他常用SQL汇总 1.联表更新 update students stu inner join course on course.STUDENT_ID=stu.id set stu.na ...

  6. mysql 常用 sql 语句 - 快速查询

    Mysql 常用 sql 语句 - 快速查询 1.mysql 基础 1.1 mysql 交互         1.1.1 mysql 连接             mysql.exe -hPup    ...

  7. php面试专题---MySQL常用SQL语句优化

    php面试专题---MySQL常用SQL语句优化 一.总结 一句话总结: 原理,万变不离其宗:其实SQL语句优化的过程中,无非就是对mysql的执行计划理解,以及B+树索引的理解,其实只要我们理解执行 ...

  8. Mysql常用sql语句(一)- 操作数据库

    21篇测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html ...

  9. Mysql常用sql语句(二)- 操作数据表

    21篇测试必备的Mysql常用sql语句,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1683347.html ...

随机推荐

  1. 「ZJOI2008」树的统计

    树剖模板题啊! 这道题的话,最通(jian)俗(dan)易(cu)懂(bao)的解法应该就是树剖了. 加上线段树维护树上路径的最大权值(\(Max\))和路径和(\(sum\)). 至于\(LCT\) ...

  2. Java入门笔记 08-注解

    1. 注解:通过 @interface 关键字进行定义2. 元注解:是一种基本注解,但是它能够应用到其它的注解上面.@Retention:解释说明了这个注解的的存活时间.取值如下: Retention ...

  3. 人工智能、大数据、物联网、区块链,四大新科技PK,你更看好谁?

    最近行业中备受关注并且非常火热的产业有哪些呢?小编这边总结了一下,一共有4个,分别是人工智能.大数据.物联网和区块链,这四种新科技也一直是蓄势待发,未来将引领新一代的科技成长,也会带给人类很多更方便快 ...

  4. Kubernetes的service资源介绍

    service 三种工作模式:userspace.iptables.ipvs 删除手动创建的service [root@master ~]# kubectl delete svc redis serv ...

  5. pip源、搭建虚拟环境、git

    一.pip源 1.1 介绍 1.采用国内源,加速下载模块的速度2.常用pip源:-- 豆瓣:https://pypi.douban.com/simple-- 阿里:https://mirrors.al ...

  6. PXE无人值守实现批量化自动安装Linux系统

    设想一个场景:假如让你给1000台服务器装系统,你会怎么做?跑去每一台服务器给它安装系统吗?显然不会.. 一.概括 通过网络引导系统的做法可以不必从硬盘.软盘或CD-ROM硬盘,而是完全通过网络来引导 ...

  7. sftp和FTP

    sftp 是一个交互式安全文件传输协议的传输程式.它类似于 ftp也叫internet网络文件协议, 但它进行加密传输,比FTP有更高的安全性.下边就简单介绍一下如何远程连接主机,进行文件的上传和下 ...

  8. linux操作系统及内核

    自己的一点总结,把知识重新编码~ 一.操作系统概述 在Richard Stevens的unix环境高级编程中这样定义“它控制计算机硬件资源,提供程序运行环境.一般而言我们称这种软件为内核(kernel ...

  9. 质因数分解(0)<P2012_1>

    质因数分解 (prime.cpp/c/pas) [问题描述] 已知正整数n是两个不同的质数的乘积,试求出较大的那个质数. [输入] 输入文件名为prime.in. 输入只有一行,包含一个正整数n. [ ...

  10. MVC 拦截器

    https://www.cnblogs.com/blosaa/archive/2011/06/02/2067632.html