MYSQL PERFORMANCE_SCHEMA HINTS
ACCOUNTS NOT PROPERLY CLOSING CONNECTIONS [ 1 ]
Works since 5.6
- SELECT ess.user, ess.host
- , (a.total_connections - a.current_connections) - ess.count_star as not_closed
- , ((a.total_connections - a.current_connections) - ess.count_star) * 100 /
- (a.total_connections - a.current_connections) as pct_not_closed
- FROM performance_schema.events_statements_summary_by_account_by_event_name ess
- JOIN performance_schema.accounts a on (ess.user = a.user and ess.host = a.host)
- WHERE ess.event_name = 'statement/com/quit'
- AND (a.total_connections - a.current_connections) > ess.count_star
- ;
UNUSED INDEXES [ 2 ]
Works since 5.6
- 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
- ORDER BY object_schema, object_name
- ;
WHO CREATED TEMPORARY (DISK) TABLES
Works since 5.6
- SELECT user, host, event_name, count_star AS cnt, sum_created_tmp_disk_tables AS tmp_disk_tables, sum_created_tmp_tables AS tmp_tables
- FROM performance_schema.events_statements_summary_by_account_by_event_name
- WHERE sum_created_tmp_disk_tables > 0
- OR sum_created_tmp_tables > 0
- ;
- SELECT schema_name, substr(digest_text, 1, 40) AS statement, count_star AS cnt, sum_created_tmp_disk_tables AS tmp_disk_tables, sum_created_tmp_tables AS tmp_tables
- FROM performance_schema.events_statements_summary_by_digest
- WHERE sum_created_tmp_disk_tables > 0
- OR sum_created_tmp_tables > 0
- ;
ACCOUNTS WHICH NEVER CONNECTED SINCE LAST START-UP [ 3 ]
Works since 5.6
- SELECT DISTINCT m_u.user, m_u.host
- FROM mysql.user m_u
- LEFT JOIN performance_schema.accounts ps_a ON m_u.user = ps_a.user AND m_u.host = ps_a.host
- WHERE ps_a.user IS NULL
- ORDER BY m_u.user
- ;
USERS WHICH NEVER CONNECTED SINCE LAST START-UP
Works since 5.6
- SELECT DISTINCT m_u.user
- FROM mysql.user m_u
- LEFT JOIN performance_schema.users ps_u ON m_u.user = ps_u.user
- WHERE ps_u.user IS NULL
- ORDER BY m_u.user
- ;
TOTALLY UNUSED ACCOUNTS (NEVER CONNECTED SINCE LAST RESTART AND NOT USED TO CHECK STORED PROGRAM OR VIEW PRIVILEGES) SINCE LAST START-UP
Works since 5.6
- SELECT DISTINCT m_u.user, m_u.host
- FROM mysql.user m_u
- LEFT JOIN performance_schema.accounts ps_a ON m_u.user = ps_a.user AND ps_a.host = m_u.host
- LEFT JOIN information_schema.views is_v ON is_v.DEFINER = CONCAT(m_u.User, '@', m_u.Host) AND is_v.security_type = 'DEFINER'
- LEFT JOIN information_schema.routines is_r ON is_r.DEFINER = CONCAT(m_u.User, '@', m_u.Host) AND is_r.security_type = 'DEFINER'
- LEFT JOIN information_schema.events is_e ON is_e.definer = CONCAT(m_u.user, '@', m_u.host)
- LEFT JOIN information_schema.triggers is_t ON is_t.definer = CONCAT(m_u.user, '@', m_u.host)
- WHERE ps_a.user IS NULL
- AND is_v.definer IS NULL
- AND is_r.definer IS NULL
- AND is_e.definer IS NULL
- AND is_t.definer IS NULL
- ORDER BY m_u.user, m_u.host
- ;
SHOW FULL PROCESSLIST
Works since 5.5 (5.1?)
But with filter on Sleep and sorting by time to find the evil query...
- SELECT id, user, host, db, command, time, state, LEFT(info, 80) AS info
- FROM information_schema.processlist
- WHERE command NOT IN ('Sleep', 'Binlog Dump')
- ORDER BY time ASC
- ;
Non blocking version, since 5.6:
- SELECT PROCESSLIST_ID AS id, PROCESSLIST_USER AS user, PROCESSLIST_HOST AS host, PROCESSLIST_DB AS db
- , PROCESSLIST_COMMAND AS command, PROCESSLIST_TIME AS time, PROCESSLIST_STATE AS state, LEFT(PROCESSLIST_INFO, 80) AS info
- FROM performance_schema.threads
- WHERE PROCESSLIST_ID IS NOT NULL
- AND PROCESSLIST_COMMAND NOT IN ('Sleep', 'Binlog Dump')
- ORDER BY PROCESSLIST_TIME ASC
- ;
STORAGE ENGINES PER SCHEMA
For defining backup strategy, preparing migration to InnoDB or Galera Cluster for MySQL, etc.
Works since 5.5 (5.1?)
- SELECT table_schema AS `schema`, engine, COUNT(*) AS `tables`
- , ROUND(SUM(data_length)/1024/1024, 0) AS data_mb, ROUND(SUM(index_length)/1024/1024, 0) index_mb
- FROM information_schema.tables
- WHERE table_schema NOT IN ('mysql', 'information_schema', 'performance_schema')
- AND engine IS NOT NULL
- GROUP BY table_schema, engine
- ;
- +---------------------+--------+--------+---------+----------+
- | schema | engine | tables | data_mb | index_mb |
- +---------------------+--------+--------+---------+----------+
- | mantis | MyISAM | 31 | 0 | 0 |
- | mpm | InnoDB | 3 | 0 | 0 |
- | mysql_sequences | InnoDB | 2 | 0 | 0 |
- | mysql_sequences | MEMORY | 1 | 0 | 0 |
- | otrs | InnoDB | 73 | 13 | 4 |
- | quartz | InnoDB | 12 | 0 | 0 |
- | tracking | MyISAM | 1 | 0 | 0 |
- +---------------------+--------+--------+---------+----------+
TABLES WITHOUT A PRIMARY KEY
Galera Cluster, InnoDB, M/S replication with row based replication does not work well with tables without a Primary Key. To find those the following query helps:
Works since 5.5 (5.1?)
- SELECT DISTINCT t.table_schema, t.table_name
- FROM information_schema.tables AS t
- LEFT JOIN information_schema.columns AS c ON t.table_schema = c.table_schema AND t.table_name = c.table_name AND c.column_key = "PRI"
- WHERE t.table_schema NOT IN ('information_schema', 'mysql', 'performance_schema')
- AND c.table_name IS NULL AND t.table_type != 'VIEW'
- ;
- +--------------+--------------------+
- | table_schema | table_name |
- +--------------+--------------------+
- | test | t_wo_pk |
- | test | t_wo_pk_with_Index |
- +--------------+--------------------+
BAD SQL QUERIES OF USERS
Sometimes it could be interesting to find users who do evil SQL Queries which do for examplecreated_tmp_disk_tables
, select_full_join
, select_range_check
or sort_merge_passes
. Those can be found as follows:
- SELECT user, host, event_name
- , sum_created_tmp_disk_tables AS tmp_disk_tables
- , sum_select_full_join AS full_join
- , sum_select_range_check AS range_check
- , sum_sort_merge_passes AS sort_merge
- FROM performance_schema.events_statements_summary_by_account_by_event_name
- WHERE sum_created_tmp_disk_tables > 0
- OR sum_select_full_join > 0
- OR sum_select_range_check > 0
- OR sum_sort_merge_passes > 0
- ORDER BY sum_sort_merge_passes DESC
- LIMIT 10
- ;
- +-------+-------------+---------------+-----------------+-----------+-------------+------------+
- | user | host | event_name | tmp_disk_tables | full_join | range_check | sort_merge |
- +-------+-------------+---------------+-----------------+-----------+-------------+------------+
- | user1 | 192.168.0.3 | insert_select | 0 | 7033 | 0 | 10947 |
- | user2 | 192.168.0.4 | insert_select | 0 | 6837 | 0 | 10792 |
- | user1 | 192.168.0.1 | select | 10742308 | 2095 | 23061 | 16 |
- | user2 | 192.168.0.2 | select | 10958067 | 2639 | 23162 | 14 |
- +-------+-------------+---------------+-----------------+-----------+-------------+------------+
SHOW PROFILE IN PERFORMANCE_SCHEMA
On systems with heavy traffic the PERFORMANCE_SCHEMA tables might be too small.
- mysql> SHOW GLOBAL VARIABLES LIKE 'perf%events%stage%hist%long%';
- +----------------------------------------------------+-------+
- | Variable_name | Value |
- +----------------------------------------------------+-------+
- | performance_schema_events_stages_history_long_size | 10000 |
- +----------------------------------------------------+-------+
- mysql> pager grep history_long
- mysql> SHOW ENGINE PERFORMANCE_SCHEMA STATUS;
- | performance_schema | events_waits_history_long.row_size | 184 |
- | performance_schema | events_waits_history_long.row_count | 1000 |
- | performance_schema | events_waits_history_long.memory | 184000 |
- | performance_schema | events_stages_history_long.row_size | 88 |
- | performance_schema | events_stages_history_long.row_count | 1000 |
- | performance_schema | events_stages_history_long.memory | 88000 |
- | performance_schema | events_statements_history_long.row_size | 3024 |
- | performance_schema | events_statements_history_long.row_count | 1000 |
- | performance_schema | events_statements_history_long.memory | 3024000 |
- mysql> UPDATE performance_schema.setup_instruments
- SET ENABLED = 'YES', TIMED = 'YES'
- WHERE NAME LIKE '%statement/%';
- mysql> UPDATE performance_schema.setup_instruments
- SET ENABLED = 'YES', TIMED = 'YES'
- WHERE NAME LIKE '%stage/%';
- mysql> UPDATE performance_schema.setup_consumers
- SET ENABLED = 'YES'
- WHERE NAME LIKE '%events_statements_%';
- mysql> UPDATE performance_schema.setup_consumers
- SET ENABLED = 'YES'
- WHERE NAME LIKE '%events_stages_%';
- mysql> SELECT ;
- mysql> SELECT eshl.event_id AS Query_ID, TRUNCATE(eshl.timer_wait/1000000000000, 6) as Duration
- , LEFT(eshl.sql_text, 120) AS Query
- FROM performance_schema.events_statements_history_long AS eshl
- JOIN performance_schema.threads AS t ON t.thread_id = eshl.thread_id
- WHERE t.processlist_id = CONNECTION_ID();
- +----------+-----------+-------------------------+
- | Query_ID | Duration | Query |
- +----------+-----------+-------------------------+
- | 12 | 13.560737 | select * from test.test |
- +----------+-----------+-------------------------+
- mysql> SELECT event_name AS Stage, TRUNCATE(timer_wait/1000000000000,6) AS Duration
- FROM performance_schema.events_stages_history_long
- WHERE nesting_event_id = 12;
- +--------------------------------+-----------+
- | Stage | Duration |
- +--------------------------------+-----------+
- | stage/sql/starting | 0.000043 |
- | stage/sql/checking permissions | 0.000004 |
- | stage/sql/Opening tables | 0.002700 |
- | stage/sql/init | 0.000025 |
- | stage/sql/System lock | 0.000009 |
- | stage/sql/optimizing | 0.000002 |
- | stage/sql/statistics | 0.000014 |
- | stage/sql/preparing | 0.000013 |
- | stage/sql/executing | 0.000000 |
- | stage/sql/Sending data | 13.557683 |
- | stage/sql/end | 0.000002 |
- | stage/sql/query end | 0.000008 |
- | stage/sql/closing tables | 0.000006 |
- | stage/sql/freeing items | 0.000215 |
- | stage/sql/cleaning up | 0.000001 |
- +--------------------------------+-----------+
SELECT
, INSERT
, UPDATE
AND DELETE
PER TABLE
Sometimes it is interesting to know how many SELECT
, INSERT
, UPDATE
or DELETE
(DML) statementes have been exectuted against a specifict table (for example for OPTIMZE TABLE
). This can be found as follows:
Works since MySQL 5.6
- SELECT object_type, object_schema, object_name
- , count_star, count_read, count_write, count_fetch
- , count_insert, count_update, count_delete
- FROM performance_schema.table_io_waits_summary_by_table
- WHERE count_star > 0
- ;
- SELECT object_type, object_schema, object_name, index_name
- , count_star, count_read, count_write, count_fetch
- , count_insert, count_update, count_delete
- FROM performance_schema.table_io_waits_summary_by_index_usage
- WHERE count_star > 0
- ;
TOP LONG RUNNING QUERIES
Works since MySQL 5.6
- UPDATE setup_consumers SET enabled = 1 WHERE name = 'events_statements_history_long';
- SELECT left(digest_text, 64)
- , ROUND(SUM(timer_end-timer_start)/1000000000, 1) AS tot_exec_ms
- , ROUND(SUM(timer_end-timer_start)/1000000000/COUNT(*), 1) AS avg_exec_ms
- , ROUND(MIN(timer_end-timer_start)/1000000000, 1) AS min_exec_ms
- , ROUND(MAX(timer_end-timer_start)/1000000000, 1) AS max_exec_ms
- , ROUND(SUM(timer_wait)/1000000000, 1) AS tot_wait_ms
- , ROUND(SUM(timer_wait)/1000000000/COUNT(*), 1) AS avg_wait_ms
- , ROUND(MIN(timer_wait)/1000000000, 1) AS min_wait_ms
- , ROUND(MAX(timer_wait)/1000000000, 1) AS max_wait_ms
- , ROUND(SUM(lock_time)/1000000000, 1) AS tot_lock_ms
- , ROUND(SUM(lock_time)/1000000000/COUNT(*), 1) AS avglock_ms
- , ROUND(MIN(lock_time)/1000000000, 1) AS min_lock_ms
- , ROUND(MAX(lock_time)/1000000000, 1) AS max_lock_ms
- , MIN(LEFT(DATE_SUB(NOW(), INTERVAL (isgs.VARIABLE_VALUE - TIMER_START*10e-13) second), 19)) AS first_seen
- , MAX(LEFT(DATE_SUB(NOW(), INTERVAL (isgs.VARIABLE_VALUE - TIMER_START*10e-13) second), 19)) AS last_seen
- , COUNT(*) as cnt
- FROM events_statements_history_long
- JOIN information_schema.global_status AS isgs
- WHERE isgs.variable_name = 'UPTIME'
- GROUP BY LEFT(digest_text,64)
- ORDER BY tot_exec_ms DESC
- ;
- +------------------------------------------------------------------+-------------+-------------+-------------+---------------------+---------------------+-----+
- | left(digest_text, 64) | tot_exec_ms | tot_wait_ms | tot_lock_ms | first_seen | last_seen | cnt |
- +------------------------------------------------------------------+-------------+-------------+-------------+---------------------+---------------------+-----+
- | INSERT INTO `test` SELECT ? , DATA , ? FROM `test` | 50493.5 | 50493.5 | 26.3 | 2015-11-12 16:41:35 | 2015-11-12 16:42:04 | 20 |
- | SELECT LEFT ( `digest_text` , ? ) , `ROUND` ( SUM ( `timer_end` | 14434.6 | 14434.6 | 25.8 | 2015-11-12 16:48:44 | 2015-11-12 17:07:15 | 6 |
- | SELECT * FROM `test` | 7483.0 | 7483.0 | 0.2 | 2015-11-12 16:41:16 | 2015-11-12 16:42:34 | 2 |
- | SHOW ENGINE INNODB STATUS | 1912.4 | 1912.4 | 0.0 | 2015-11-12 16:37:19 | 2015-11-12 17:07:36 | 687 |
- | SHOW GLOBAL VARIABLES | 1091.1 | 1091.1 | 68.8 | 2015-11-12 16:37:19 | 2015-11-12 17:07:36 | 687 |
- | SHOW GLOBAL STATUS | 638.7 | 638.7 | 40.8 | 2015-11-12 16:37:19 | 2015-11-12 17:07:36 | 687 |
- | SELECT LEFT ( `digest_text` , ? ) , SUM ( `timer_end` - `timer_s | 356.2 | 356.2 | 42.4 | 2015-11-12 16:42:38 | 2015-11-12 16:45:00 | 6 |
- | SELECT `digest_text` , SUM ( `timer_end` - `timer_start` ) / ? A | 325.3 | 325.3 | 0.4 | 2015-11-12 16:40:44 | 2015-11-12 16:42:18 | 3 |
- | SELECT `DIGEST_TEXT` , ( `TIMER_END` - `TIMER_START` ) / ? AS `e | 163.2 | 163.2 | 1.0 | 2015-11-12 16:37:44 | 2015-11-12 16:39:22 | 9 |
- | SELECT LOWER ( REPLACE ( trx_state , ?, ... ) ) AS state , COUNT | 133.9 | 133.9 | 80.2 | 2015-11-12 16:37:19 | 2015-11-12 17:07:36 | 687 |
- +------------------------------------------------------------------+-------------+-------------+-------------+---------------------+---------------------+-----+
TABLES NEVER WRITTEN TO
If you want to find tables which it was never written to (or read from) since last instance restart you can use the following query. Works since MySQL 5.6
Caution:
count_read
can only be taken as argument if a physical backup method (NOTmysqldump
) is used.- If
count_write
is 0 it does not necessarily mean that there was no write statement (no matching write)! - If tables are empty
SELECT
statements are not counted (count_read
= 0).
- SELECT t.table_schema, t.table_name, t.table_rows, tio.count_read, tio.count_write
- FROM information_schema.tables AS t
- JOIN performance_schema.table_io_waits_summary_by_table AS tio
- ON tio.object_schema = t.table_schema AND tio.object_name = t.table_name
- WHERE t.table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
- AND tio.count_write = 0
- ORDER BY t.table_schema, t.table_name
- ;
http://www.fromdual.com/mysql-performance-schema-hints
MYSQL PERFORMANCE_SCHEMA HINTS的更多相关文章
- mysql performance_schema 初探
mysql performance_schema 初探: mysql 5.5 版本 新增了一个性能优化的引擎: PERFORMANCE_SCHEMA 这个功能默认是关闭的: 需要设置参数: perf ...
- mysql performance_schema 和information_schema.tables了解
这个是关于mysql的系统表,性能表,核心表操作的一些介绍,深入算不上 我们一般很少去动 mysql information_schema 信息相关 performance_schema 性能相关 ...
- mysql performance_schema/information_schema授权问题
mysql> grant all on performance_schema.* to 'testuser'@'%';ERROR 1044 (42000): Access denied for ...
- docker配置mysql主从与django实现读写分离
一.搭建主从mysql环境 1 下载mysql镜像 docker pull mysql:5.7 2 运行刚下载的mysql镜像文件 # 运行该命令之前可以使用`docker images`是否下载成功 ...
- 基于MySQL MEB的备份恢复
MEB(MySQL Enterprise Backup)是MySQL商业版中提供的备份工具,属于物理备份. 同XtraBackup一样,mysqlbackup的使用过程同样包含如下三个步骤: 备份(- ...
- 如何在删除ibdata1和ib_logfile的情况下恢复MySQL数据库
昨天,有个朋友对公司内部使用的一个MySQL实例开启binlog,但是在启动的过程中失败了(他也没提,为何会失败),在启动失败后,他删除了ibdata1和ib_logfile,后来,能正常启动了,但所 ...
- 读书笔记--SQL必知必会--常用MySQL(MariaDB)命令
DBMS信息 显示DBMS的版本 select version(); 显示DBMS状态 status; 显示DBMS资源状态 show status; 显示DBMS支持的权限 show privile ...
- 4.MySQL 主主(m-m) 同步生产库标准同步操作实施流程
通过MySQL参数配置使用主主前提: 1.表的主键自增. ################################################################# #m1-m ...
- 二进制包安装MySQL数据库
1.1二进制包安装MySQL数据库 1.1.1 安装前准备(规范) [root@Mysql_server ~]# mkdir -p /home/zhurui/tools ##创建指定工具包存放路径 [ ...
随机推荐
- c51
ORG 0000HMOV R7,#08HMOV 83H,#01HMOV R4,#00HAA1:CLR P3.6 CLR P3.4 SETB P3.6 DJNZ R7,AA1AA2:JB P3.0,AA ...
- python学习历程之split()方法获取cmd mysql 结果集
if __name__=='__main__': FServerId = raw_input("Please input source id:") GetFileKey(FServ ...
- metagenome 简介
宏基因组 ( Metagenome)(也称微生物环境基因组 Microbial Environmental Genome, 或元基因组) .是由 Handelsman 等 1998 年提出的新名词, ...
- junit单元测试(keeps the bar green to keeps the code clean)
error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...
- cocoapods:安装/更新Ruby环境教程
简介 有时候在安装cocoapods时会产生如下错误 ERROR: Error installing cocoapods: activesupport requires Ruby version &g ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
- 【转载】App.config/Web.config 中特殊字符的处理
写一个网站,遇到一个问题,发布以后,提示错误,但是即使打开错误提示(在web.config中打开),还是只提示错误,没提示什么地方错误,这让我知道了:是webconfig本身的错误,经过排除,是链接字 ...
- 自动化测试工具QTP的使用实例 分类: 软件测试 2015-06-17 00:23 185人阅读 评论(0) 收藏
1. QTP简介 1.1QTP功能与特点 QTP是QuickTest Professional的简称,是一种自动化软件测试工具.在软件的测试过程中,QTP主要来用来通过已有的测试脚本执行重复的手动测试 ...
- redis之(二十一)redis之深入理解Spring Redis的使用
关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...
- 启用https协议的方法
提醒:启用https协议会降低服务器性能,如非必要不必启用 一.用openssl生成密钥.证书: 1.生成RSA密钥的方法 openssl genrsa -out privkey.pem 2048 建 ...