MYSQL常用SQL彙總
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.idG
7、最近一次死鎖、未提交事物、CHECKPIONT、BUFFER POOL等
show engine innodb statusG
8、過濾無用執行緒資訊可用pager
pager grep -v Sleep;show processlist;
9、檢視當前執行的詳細SQL
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE info is not nullG
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 1G
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 1G
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 1G
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 1G
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 1G
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 2G
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;