持续更新…
辅助阅读1. https://www.yuque.com/infuq/others/ivmss0
辅助阅读2. https://www.yuque.com/infuq/others/ohn4yf
辅助阅读3. https://www.yuque.com/infuq/others/niv9ee
-- 线程缓存内的线程数量
show global status like 'Threads_cached';
-- 当前打开的连接数量
show global status like 'Threads_connected';
-- 创建用来处理连接的线程数
show global status like 'Threads_created';
-- 激活的(非睡眠状态)线程数
show global status like 'Threads_running';
-- 内部提交语句
show global status like 'Handler_commit';
-- 内部 rollback语句数量
show global status like 'Handler_rollback';
-- 索引第一条记录被读的次数,如果高,则它表明服务器正执行大量全索引扫描
show global status like 'Handler_read_first';
-- 根据索引读一行的请求数,如果较高,说明查询和表的索引正确
show global status like 'Handler_read_key';
-- 查询读索引最后一个索引键请求数
show global status like 'Handler_read_last';
-- 按照索引顺序读下一行的请求数
show global status like 'Handler_read_next';
-- 按照索引顺序读前一行的请求数
show global status like 'Handler_read_prev';
-- 根据固定位置读一行的请求数,如果值较高,说明可能使用了大量需要MySQL扫整个表的查询或没有正确使用索引
show global status like 'Handler_read_rnd';
-- 在数据文件中读下一行的请求数,如果你正进行大量的表扫,该值会较高
show global status like 'Handler_read_rnd_next';
-- 被缓存的.frm文件数量
show global status like 'Open_table_definitions';
-- 已经打开的表的数量,如果较大,table_open_cache值可能太小
show global status like 'Opened_tables';
-- 当前打开的表的数量
show global status like 'Open_tables';
-- 已经发送给服务器的查询个数
show global status like 'Queries';
-- 没有使用索引的联接的数量,如果该值不为0,你应该仔细检查表的所有
show global status like 'Select_full_join';
-- 对第一个表进行完全扫的联接的数量
show global status like 'Select_scan';
-- 查询时间超过long_query_time秒的查询个数
show global status like 'Slow_queries';
-- 排序算法已经执行的合并的数量,如果值较大,增加sort_buffer_size大小
show global status like 'Sort_merge_passes';
-- 服务器执行语句时,在硬盘上自动创建的临时表的数量,是指在排序时,内存不够用(tmp_table_size小于需要排序的结果集),所以需要创建基于磁盘的临时表进行排序
show global status like 'Created_tmp_disk_tables';
-- 服务器执行语句时自动创建的内存中的临时表的数量
show global status like 'Created_tmp_files';
-- 试图连接到(不管成不成功)MySQL服务器的连接数
show global status like 'Connections';
-- 使用临时二进制日志缓存但超过 binlog_cache_size 值,需要使用临时文件存储事务中的语句的事务数
binlog_cache_disk_use;
-- 使用二进制日志缓存的事务数
binlog_cache_use;
-- 使用二进制日志语句缓存但超过 binlog_stmt_cache_size 的值,需要使用临时文件存储这些语句的非事务语句的数量
binlog_stmt_cache_disk_use;
-- 使用二进制日志语句缓存的非事务性语句的数量
binglog_cache_disk_use;
-- 表锁
show global status like 'Table_locks_waited';
show global status like 'Table_locks_immediate';
-- 行锁
-- 当前等待锁的行锁数量
show global status like 'Innodb_row_lock_current_waits';
-- 请求行锁总耗时
show global status like 'Innodb_row_lock_time';
-- 请求行锁平均耗时
show global status like 'Innodb_row_lock_time_avg';
-- 请求行锁最久耗时
show global status like 'Innodb_row_lock_time_max';
-- 行锁发生次数
show global status like 'Innodb_row_lock_waits';
-- 还可以定时收集INFORMATION_SCHEMA里面的信息:
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
-- MySQL 8.0 中已经不再使用,建议观测 sys 库
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
-- 临时表/临时文件
show global status like 'Created_tmp_disk_tables';
show global status like 'Created_tmp_files';
-- 打开表/文件数
show global status like 'Open_files';
show global status like 'Open_table_definitions';
show global status like 'Open_tables';
-- 并发连接数
show global status like 'Threads_running';
show global status like 'Threads_created';
show global status like 'Threads_cached';
show global status like 'Aborted_clients';
-- 客户端没有正确关闭连接导致客户端终止而中断的连接数
show global status like 'Aborted_connects';
show global status like 'Innodb_buffer_pool_wait_free';
show global status like 'Innodb_log_waits';
show full processlist;
show engine innodb status\G;
show slave hosts;
select @@transaction_isolation
设置全局隔离级别
set global transaction isolation level READ UNCOMMITTED
set global transaction isolation level READ COMMITTED
set global transaction isolation level REPEATABLE READ
set global transaction isolation level SERIALIZABLE
设置会话隔离级别
set session transaction isolation level READ UNCOMMITTED
set session transaction isolation level READ COMMITTED
set session transaction isolation level REPEATABLE READ
set session transaction isolation level SERIALIZABLE
设置锁等待超时时长
set innodb_lock_wait_timeout=60
set global innodb_lock_wait_timeout=60
查看事务执行情况
SELECT * FROM information_schema.innodb_trx
trx_state包含RUNNING 和 LOCK WAIT
如果LOCK WAIT时间超过设定的时长, 会出现超时异常. https://www.yuque.com/infuq/others/tmmgsq#c5RPR
SELECT * FROM information_schema.innodb_locks
(8.0版本之后)
SELECT * FROM performance_schema.data_locks
查看锁的等待情况 (8.0版本之前)
SELECT * FROM information_schema.innodb_lock_waits
(8.0版本之后)
SELECT * FROM performance_schema.data_lock_waits
pager less 或 pager more
show engine innodb status
SHOW INDEX FROM store_goods
1.cardinality 预估值
2.根据cardinality可以判断此列是否适合创建索引
3.cardinality值越大越适合创建索引
若重新评估store_goods表的cardinality值, 采用 analyze table store_goods
若查看表的状态, 采用 show table status
-- 方式一
SELECT
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
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
-- 方式二
SELECT * FROM sys.innodb_lock_waits ;
方式一的SQL结果如下图
事务515590阻塞了515591, 即线程8阻塞了线程9, 线程ID与show processlist 命令显示的ID对应.
waiting_query表示被阻塞的SQL.
blocking_query一般情况都是NULL, 因为此SQL大概率已经在事务515590中执行过了.
以上两种方式不一定能查看到blocking_query值, 可以通过以下方式查看到blocking_query值
SELECT
tmp.*,
c.SQL_Text blocking_sql_text,
p.HOST blocking_host
FROM
(
SELECT
r.trx_state wating_trx_state,
r.trx_id waiting_trx_id,
r.trx_mysql_thread_Id waiting_thread,
r.trx_query waiting_query,
b.trx_state blocking_trx_state,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
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
) tmp,
information_schema.PROCESSLIST p,
PERFORMANCE_SCHEMA.events_statements_current c,
PERFORMANCE_SCHEMA.threads t
WHERE
tmp.blocking_thread = p.id
AND t.thread_id = c.THREAD_ID
AND t.PROCESSLIST_ID = p.id;
查看事务执行时长
-- #1
SELECT trx.trx_id AS `事务ID`
,trx.trx_mysql_thread_id AS `线程ID`
,l.user AS `用户`
,l.host AS `主机`
,l.db AS `数据库名`
,to_seconds(now())- to_seconds(trx.trx_started) AS `事务持续时间(秒)`
,trx.trx_state AS `事务状态`
,trx.trx_requested_lock_id AS `事务需要等待的资源`
,trx.trx_wait_started AS `事务开始等待时间`
,trx.trx_tables_in_use AS `事务使用表`
,trx.trx_tables_locked AS `事务拥有锁`
,trx.trx_rows_locked AS `事务锁定行`
,trx.trx_rows_modified AS `事务更改行`
,trx.trx_query AS `语句`
FROM information_schema.innodb_trx trx
LEFT JOIN information_schema.PROCESSLIST l ON trx.trx_mysql_thread_id = l.id;
或者
SELECT t.trx_id AS `事务ID`
,to_seconds(now())- to_seconds(t.trx_started) AS `事务持续时间(秒)`
,t.*
FROM information_schema.innodb_trx t;
https://hdm.console.aliyun.com/#/diagnosis/instance/rm-bp1349lz8ic76gru5/detail/session
事务执行的所有SQL
-- #2
SELECT now() AS `当前时间`
,( UNIX_TIMESTAMP( now()) - UNIX_TIMESTAMP( a.trx_started )) AS `事务持续时间(秒)`
,b.id AS `MySQL线程ID`
,b.user
,b.host
,b.db
,d.SQL_TEXT AS `事务最后执行的SQL`
FROM information_schema.innodb_trx a
INNER JOIN information_schema.PROCESSLIST b ON a.TRX_MYSQL_THREAD_ID = b.id AND b.command = 'Sleep'
INNER JOIN performance_schema.threads c ON b.id = c.PROCESSLIST_ID
INNER JOIN performance_schema.events_statements_current d ON d.THREAD_ID = c.THREAD_ID;
-- #3 726137即#2中的MySQL线程ID,一个事务对应一个MySQL线程ID
SELECT ps.id 'PROCESS ID'
,ps.USER
,ps.HOST
,esh.EVENT_ID
,trx.trx_started
,esh.event_name 'EVENT NAME'
,esh.sql_text 'SQL'
,ps.time
FROM PERFORMANCE_SCHEMA.events_statements_history esh
JOIN PERFORMANCE_SCHEMA.threads th ON esh.thread_id = th.thread_id
JOIN information_schema.PROCESSLIST ps ON ps.id = th.processlist_id AND ps.id = 726137
LEFT JOIN information_schema.innodb_trx trx ON trx.trx_mysql_thread_id = ps.id
WHERE trx.trx_id IS NOT NULL
AND ps.USER != 'SYSTEM_USER'
ORDER BY esh.EVENT_ID;
--
SELECT lock_id AS `锁ID`
,lock_trx_id AS `拥有锁的事务ID`
,lock_mode AS `锁模式`
,lock_type AS `锁类型`
,lock_table AS `被锁的表`
,lock_index AS `被锁的索引`
,lock_space AS `被锁的表空间号`
,lock_page AS `被锁的页号`
,lock_rec AS `被锁的记录号`
,lock_data AS `被锁的数据`
FROM information_schema.innodb_locks;
--
SELECT requesting_trx_id AS `请求锁的事务ID`
,requested_lock_id AS `请求锁的锁ID`
,blocking_trx_id AS `当前拥有锁的事务ID`
,blocking_lock_id AS `当前拥有锁的锁ID`
FROM innodb_lock_waits;
-- 不严谨
SELECT t.table_schema
,t.table_name
FROM information_schema.tables t
INNER JOIN information_schema.columns c ON t.table_schema = c.table_schema AND t.table_name = c.table_name
WHERE t.table_schema NOT IN ('sys', 'mysql', 'information_schema', 'performance_schema', 'test')
GROUP BY t.table_schema
,t.table_name
HAVING SUM(if(column_key IN ('PRI', 'UNI'), 1, 0)) = 0 ;
--
SELECT
t.table_schema,
t.table_name,
t.table_type
FROM
information_schema.TABLES t
LEFT JOIN information_schema.TABLE_CONSTRAINTS c ON c.TABLE_SCHEMA = t.TABLE_SCHEMA
AND t.TABLE_NAME = c.TABLE_NAME
AND c.CONSTRAINT_NAME IN ( 'PRIMARY' )
WHERE
c.TABLE_NAME IS NULL
AND t.TABLE_SCHEMA NOT IN ( 'information_schema', 'performance_schema', 'test', 'mysql', 'sys' )
AND t.TABLE_TYPE = "BASE TABLE" ;
--
SELECT
t.table_schema,
t.table_name,
t.table_type
FROM
information_schema.TABLES t
LEFT JOIN information_schema.STATISTICS c ON c.TABLE_SCHEMA = t.TABLE_SCHEMA
AND t.TABLE_NAME = c.TABLE_NAME
AND c.INDEX_NAME IN ( 'PRIMARY' )
WHERE
c.TABLE_NAME IS NULL
AND t.TABLE_SCHEMA NOT IN ( 'information_schema', 'performance_schema', 'test', 'mysql', 'sys' )
AND t.TABLE_TYPE = "BASE TABLE" ;
-- information_schema
SELECT a.table_schema AS '库'
,a.table_name AS '表名'
,a.index_name AS '索引'
,a.index_columns AS '索引列'
,b.index_name AS '冗余索引'
,b.index_columns AS '冗余索引列'
,CONCAT('ALTER TABLE ',a.table_schema,'.',a.table_name,' DROP INDEX ',a.index_name,'') AS '建议删除索引语句'
FROM
( (
SELECT information_schema.STATISTICS.TABLE_SCHEMA AS table_schema
,information_schema.STATISTICS.TABLE_NAME AS table_name
,information_schema.STATISTICS.INDEX_NAME AS index_name
,MAX(information_schema.STATISTICS.NON_UNIQUE) AS non_unique
,MAX(IF(ISNULL(information_schema.STATISTICS.SUB_PART),0,1)) AS subpart_exists
,GROUP_CONCAT(information_schema.STATISTICS.COLUMN_NAME ORDER BY information_schema.STATISTICS.SEQ_IN_INDEX ASC separator ',') AS index_columns
FROM information_schema.STATISTICS
WHERE information_schema.STATISTICS.INDEX_TYPE = 'BTREE' AND information_schema.STATISTICS.TABLE_SCHEMA NOT IN ('mysql', 'sys', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA')
GROUP BY information_schema.STATISTICS.TABLE_SCHEMA
,information_schema.STATISTICS.TABLE_NAME
,information_schema.STATISTICS.INDEX_NAME
) a
JOIN
(
SELECT information_schema.STATISTICS.TABLE_SCHEMA AS table_schema
,information_schema.STATISTICS.TABLE_NAME AS table_name
,information_schema.STATISTICS.INDEX_NAME AS index_name
,MAX(information_schema.STATISTICS.NON_UNIQUE) AS non_unique
,MAX(IF(ISNULL(information_schema.STATISTICS.SUB_PART),0,1)) AS subpart_exists
,GROUP_CONCAT(information_schema.STATISTICS.COLUMN_NAME ORDER BY information_schema.STATISTICS.SEQ_IN_INDEX ASC separator ',') AS index_columns
FROM information_schema.STATISTICS
WHERE information_schema.STATISTICS.INDEX_TYPE = 'BTREE' AND information_schema.STATISTICS.TABLE_SCHEMA NOT IN ('mysql', 'sys', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA')
GROUP BY information_schema.STATISTICS.TABLE_SCHEMA
,information_schema.STATISTICS.TABLE_NAME
,information_schema.STATISTICS.INDEX_NAME
) b ON a.table_schema = b.table_schema AND a.table_name = b.table_name
)
WHERE a.index_name <> b.index_name
AND (((a.index_columns = b.index_columns) AND ((a.non_unique > b.non_unique) OR ((a.non_unique = b.non_unique) AND (IF((a.index_name = 'PRIMARY'), '', a.index_name) > IF((b.index_name = 'PRIMARY'), '', b.index_name))))) OR ((LOCATE(CONCAT(a.index_columns, ','), b.index_columns) = 1) AND (a.non_unique = 1)) OR ((LOCATE(CONCAT(b.index_columns, ','), a.index_columns) = 1) AND (b.non_unique = 0)));
-- sys
SELECT
table_schema AS '库',
table_name AS '表名',
dominant_index_name AS '索引',
dominant_index_columns AS '索引列',
dominant_index_non_unique,
redundant_index_name AS '冗余索引',
redundant_index_columns AS '冗余索引列',
redundant_index_non_unique,
subpart_exists,
sql_drop_index AS '建议删除索引语句'
FROM
sys.schema_redundant_indexes
WHERE
table_schema NOT IN ('mysql', 'sys', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA');
-- performance_schema information_schema
SELECT
t.OBJECT_SCHEMA AS object_schema,
t.OBJECT_NAME AS object_name,
t.INDEX_NAME AS index_name
FROM
performance_schema.table_io_waits_summary_by_index_usage t
JOIN information_schema.STATISTICS s ON t.OBJECT_SCHEMA = s.TABLE_SCHEMA
AND t.OBJECT_NAME = s.TABLE_NAME
AND t.INDEX_NAME = s.INDEX_NAME
WHERE
t.INDEX_NAME IS NOT NULL
AND t.COUNT_STAR = 0
AND t.OBJECT_SCHEMA NOT IN ('mysql', 'sys', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA')
AND t.INDEX_NAME <> 'PRIMARY'
AND s.NON_UNIQUE = 1
AND s.SEQ_IN_INDEX = 1
ORDER BY t.OBJECT_SCHEMA, t.OBJECT_NAME;
-- sys
SELECT * FROM sys.schema_unused_indexes WHERE object_schema NOT IN ('mysql', 'sys', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA');
select
TABLE_SCHEMA,
TABLE_NAME,
TABLE_ROWS,
DATA_LENGTH,
INDEX_LENGTH,
DATA_FREE
from
information_schema.tables
where
DATA_FREE is not null
ORDER BY
DATA_FREE DESC
LIMIT 30;
--
SELECT
TABLE_SCHEMA,
TABLE_NAME,
ENGINE
FROM
INFORMATION_SCHEMA.TABLES
WHERE
ENGINE != 'InnoDB'
AND TABLE_SCHEMA NOT IN ( "INFORMATION_SCHEMA", "PERFORMANCE_SCHEMA", "MYSQL", "SYS" ) ;
SELECT
TABLE_SCHEMA,
TABLE_NAME,
TABLE_ROWS
FROM
`information_schema`.`tables`
WHERE
TABLE_SCHEMA NOT IN ( 'information_schema', 'sys', 'mysql', 'performance_schema' )
ORDER BY
table_rows DESC
LIMIT 30;
--
SELECT
concat( round( sum( DATA_LENGTH / 1024 / 1024 ), 2 ), 'M' ) AS "Size data(MB)",
concat( round( sum( INDEX_LENGTH / 1024 / 1024 ), 2 ), 'M' ) AS "Size index(MB)"
FROM
information_schema.tables
WHERE
table_schema = 'wisp_test'
AND table_name = 'store_customer_order';
--
SELECT
table_schema "Database name",
sum( table_rows ) "No. of rows",
sum( data_length ) / 1024 / 1024 "Size data (MB)",
sum( index_length )/ 1024 / 1024 "Size index (MB)"
FROM
information_schema.TABLES
GROUP BY
table_schema;
show table status like '表名'\G;
抽样计算每行字节数
SELECT
ROUND(AVG(row), 2)
FROM
(SELECT
(4 + 4 + LENGTH(field)) AS row
FROM
table
LIMIT 150000) t
https://dev.mysql.com/doc/refman/8.0/en/storage-requirements.html
SELECT
table_schema,
table_name,
ENGINE,
auto_increment
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA NOT IN ( "INFORMATION_SCHEMA", "PERFORMANCE_SCHEMA", "MYSQL", "SYS" )
AND auto_increment IS NOT NULL
ORDER BY
auto_increment DESC
limit 30;
SELECT *
FROM information_schema.tables
WHERE table_name = 'order_relation'
-- 主从状态
show slave status\G;
-- 主从是否延迟
Master_Log_File == Relay_Master_Log_File && Read_Master_Log_Pos == Exec_Master_Log_Pos