MySQL默认数据库介绍
大家在学习MySQL时,安装后都会发现里边已经自带了几个默认的数据库,我装的MySQL5.5里边自带六个数据库
网上查了一些资料对这几个数据库的功能做一下学习。
1.information_schema
这个库中有很多的表
information_schema数据库表说明:
SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。
TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的 结果取之此表。
COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。
STATISTICS表:提供了关于表索引的信息。是show index from schemaname.tablename的结果取之此表。
USER_PRIVILEGES(用户权限)表:给出了关于全程权限的信息。该信息源自mysql.user授权表。是非标准表。
SCHEMA_PRIVILEGES(方案权限)表:给出了关于方案(数据库)权限的信息。该信息来自mysql.db授权表。是非标准表。
TABLE_PRIVILEGES(表权限)表:给出了关于表权限的信息。该信息源自mysql.tables_priv授权表。是非标准表。
COLUMN_PRIVILEGES(列权限)表:给出了关于列权限的信息。该信息源自mysql.columns_priv授权表。是非标准表。
CHARACTER_SETS(字符集)表:提供了mysql实例可用字符集的信息。是SHOW CHARACTER SET结果集取之此表。
COLLATIONS表:提供了关于各字符集的对照信息。
COLLATION_CHARACTER_SET_APPLICABILITY表:指明了可用于校对的字符集。这些列等效于SHOW COLLATION的前两个显示字段。
TABLE_CONSTRAINTS表:描述了存在约束的表。以及表的约束类型。
KEY_COLUMN_USAGE表:描述了具有约束的键列。
ROUTINES表:提供了关于存储子程序(存储程序和函数)的信息。此时,ROUTINES表不包含自定义函数(UDF)。名为“mysql.proc name”的列指明了对应于 INFORMATION_SCHEMA.ROUTINES表的mysql.proc表列。
VIEWS表:给出了关于数据库中的视图的信息。需要有show views权限,否则无法查看视图信息。
TRIGGERS表:提供了关于触发程序的信息。必须有super权限才能查看该表
2.mysql
3.performance_schema
需要设置参数: performance_schema 才可以启动该功能
按照相关的标准对进行的事件统计表, 表也是只读的,只能turcate
events_waits_summary_by_instance
events_waits_summary_by_thread_by_event_name
events_waits_summary_global_by_event_name
file_summary_by_event_name
file_summary_by_instance
setup_consumers 描述各种事件
setup_instruments 描述这个数据库下的表名以及是否开启监控。
setup_timers 描述 监控选项已经采样频率的时间间隔
events_waits_current 记录当前正在发生的等待事件,这个表是只读的表,不能update ,delete ,但是可以truncate
性能历史表 :events_waits_history 只保留每个线程(thread) 的最近的10个事件
性能历史表 :events_waits_history_long 记录最近的10000个事件 标准的先进先出(FIFO) 这俩表也是只读表,只能truncate
4.sakila
这是一个MySQL的一个样本数据库,里边都是一些例子表。
5.test
这是一个空表,测试表,可以删除。
6.world
这个表也没有什么大用吧,里边这三个表里有很多数据。以后有进步还会继续补充的。。
mysql中的默认“INFORMATION_SCHEMA”数据库介绍
INFORMATION_SCHEMA提供了访问数据库元数据的方式。
元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。
有些时候用于表述该信息的其他术语包括“数据词典”和“系统目录”。
下面举例子来说明用法,详细用法参看手册。
1,查看数据库服务器上的数据库
SELECT SCHEMA_NAME AS 'database'
FROM INFORMATION_SCHEMA.SCHEMATA LIMIT 0 , 30
2,查看某个数据库里面的数据表
SELECT table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'mylibrary' LIMIT 0 , 30
3,查看某个数据表里面的字段
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'authors' AND table_schema = 'mylibrary' LIMIT 0 , 30
4,查看某个表下面的索引信息
SELECT *
FROM INFORMATION_SCHEMA.STATISTICSW
WHERE table_name = 'authors'AND table_schema = 'mylibrary'LIMIT 0 , 30
5,查看某个数据库里面数据和索引的大小(M)
SELECT SUM( DATA_LENGTH ) /1024 /1024, SUM( INDEX_LENGTH ) /1024 /1024
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'stat'
MySQL5.6 PERFORMANCE_SCHEMA 说明
背景:
MySQL 5.5开始新增一个数据库:PERFORMANCE_SCHEMA,主要用于收集数据库服务器性能参数。并且库里表的存储引擎均为PERFORMANCE_SCHEMA,而用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表。MySQL5.5默认是关闭的,需要手动开启,在配置文件里添加:
[mysqld]
performance_schema=ON
查看是否开启:
mysql>show variables like 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
从MySQL5.6开始,默认打开,本文就从MySQL5.6来说明,在数据库使用当中PERFORMANCE_SCHEMA的一些比较常用的功能。具体的信息可以查看官方文档。
相关表信息:
一:配置(setup)表:
zjy@performance_schema 10:16:56>show tables like '%setup%';
+----------------------------------------+
| Tables_in_performance_schema (%setup%) |
+----------------------------------------+
| setup_actors |
| setup_consumers |
| setup_instruments |
| setup_objects |
| setup_timers |
+----------------------------------------+
1,setup_actors:配置用户纬度的监控,默认监控所有用户。
zjy@performance_schema 10:19:11>select * from setup_actors;
+------+------+------+
| HOST | USER | ROLE |
+------+------+------+
| % | % | % |
+------+------+------+
2,setup_consumers:配置events的消费者类型,即收集的events写入到哪些统计表中。
zjy@: performance_schema 10:23:35>select * from setup_consumers;
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_stages_current | NO |
| events_stages_history | NO |
| events_stages_history_long | NO |
| events_statements_current | YES |
| events_statements_history | NO |
| events_statements_history_long | NO |
| events_waits_current | NO |
| events_waits_history | NO |
| events_waits_history_long | NO |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+--------------------------------+---------+
这里需要说明的是需要查看哪个就更新其ENABLED列为YES。如:
zjy@performance_schema 10:25:02>update setup_consumers set ENABLED='YES' where NAME in ('events_stages_current','events_waits_current');
Query OK, 2 rows affected (0.00 sec)
更新完后立即生效,但是服务器重启之后又会变回默认值,要永久生效需要在配置文件里添加:
[mysqld]
#performance_schema
performance_schema_consumer_events_waits_current=on
performance_schema_consumer_events_stages_current=on
performance_schema_consumer_events_statements_current=on
performance_schema_consumer_events_waits_history=on
performance_schema_consumer_events_stages_history=on
performance_schema_consumer_events_statements_history=on
即在这些表的前面加上:performance_schema_consumer_xxx。表setup_consumers里面的值有个层级关系:
global_instrumentation > thread_instrumentation = statements_digest > events_stages_current = events_statements_current = events_waits_current > events_stages_history = events_statements_history = events_waits_history > events_stages_history_long = events_statements_history_long = events_waits_history_long
只有上一层次的为YES,才会继续检查该本层为YES or NO。global_instrumentation是最高级别consumer,如果它设置为NO,则所有的consumer都会忽略。其中history和history_long存的是current表的历史记录条数,history表记录了每个线程最近等待的10个事件,而history_long表则记录了最近所有线程产生的10000个事件,这里的10和10000都是可以配置的。这三个表表结构相同,history和history_long表数据都来源于current表。长度通过控制参数:
zjy@performance_schema 11:10:03>show variables like 'performance_schema%history%size';
+--------------------------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------------------------+-------+
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_stages_history_size | 10 |
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_statements_history_size | 10 |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
+--------------------------------------------------------+-------+
3,setup_instruments:配置具体的instrument,主要包含4大类:idle、stage/xxx、statement/xxx、wait/xxx:
zjy@performance_schema 10:56:35>select name,count(*) from setup_instruments group by LEFT(name,5);
+---------------------------------+----------+
| name | count(*) |
+---------------------------------+----------+
| idle | 1 |
| stage/sql/After create | 111 |
| statement/sql/select | 179 |
| wait/synch/mutex/sql/PAGE::lock | 296 |
+---------------------------------+----------+
idle表示socket空闲的时间,stage类表示语句的每个执行阶段的统计,statement类统计语句维度的信息,wait类统计各种等待事件,比如IO,mutux,spin_lock,condition等。
4,setup_objects:配置监控对象,默认对mysql,performance_schema和information_schema中的表都不监控,而其它DB的所有表都监控。
zjy@performance_schema 11:00:18>select * from setup_objects;
+-------------+--------------------+-------------+---------+-------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | ENABLED | TIMED |
+-------------+--------------------+-------------+---------+-------+
| TABLE | mysql | % | NO | NO |
| TABLE | performance_schema | % | NO | NO |
| TABLE | information_schema | % | NO | NO |
| TABLE | % | % | YES | YES |
+-------------+--------------------+-------------+---------+-------+
5,setup_timers:配置每种类型指令的统计时间单位。MICROSECOND表示统计单位是微妙,CYCLE表示统计单位是时钟周期,时间度量与CPU的主频有关,NANOSECOND表示统计单位是纳秒。但无论采用哪种度量单位,最终统计表中统计的时间都会装换到皮秒。(1秒=1000000000000皮秒)
zjy@performance_schema 11:05:12>select * from setup_timers;
+-----------+-------------+
| NAME | TIMER_NAME |
+-----------+-------------+
| idle | MICROSECOND |
| wait | CYCLE |
| stage | NANOSECOND |
| statement | NANOSECOND |
+-----------+-------------+
二:instance表
1,cond_instances:条件等待对象实例
表中记录了系统中使用的条件变量的对象,OBJECT_INSTANCE_BEGIN为对象的内存地址。
2,file_instances:文件实例
表中记录了系统中打开了文件的对象,包括ibdata文件,redo文件,binlog文件,用户的表文件等,open_count显示当前文件打开的数目,如果重来没有打开过,不会出现在表中。
zjy@performance_schema 11:20:04>select * from file_instances limit 2,5;
+---------------------------------+--------------------------------------+------------+
| FILE_NAME | EVENT_NAME | OPEN_COUNT |
+---------------------------------+--------------------------------------+------------+
| /var/lib/mysql/mysql/plugin.frm | wait/io/file/sql/FRM | 0 |
| /var/lib/mysql/mysql/plugin.MYI | wait/io/file/myisam/kfile | 1 |
| /var/lib/mysql/mysql/plugin.MYD | wait/io/file/myisam/dfile | 1 |
| /var/lib/mysql/ibdata1 | wait/io/file/innodb/innodb_data_file | 2 |
| /var/lib/mysql/ib_logfile0 | wait/io/file/innodb/innodb_log_file | 2 |
+---------------------------------+--------------------------------------+------------+
3,mutex_instances:互斥同步对象实例
表中记录了系统中使用互斥量对象的所有记录,其中name为:wait/synch/mutex/*。LOCKED_BY_THREAD_ID显示哪个线程正持有mutex,若没有线程持有,则为NULL。
4,rwlock_instances: 读写锁同步对象实例
表中记录了系统中使用读写锁对象的所有记录,其中name为 wait/synch/rwlock/*。WRITE_LOCKED_BY_THREAD_ID为正在持有该对象的thread_id,若没有线程持有,则为NULL。READ_LOCKED_BY_COUNT为记录了同时有多少个读者持有读锁。(通过 events_waits_current 表可以知道,哪个线程在等待锁;通过rwlock_instances知道哪个线程持有锁。rwlock_instances的缺陷是,只能记录持有写锁的线程,对于读锁则无能为力)。
5,socket_instances:活跃会话对象实例
表中记录了thread_id,socket_id,ip和port,其它表可以通过thread_id与socket_instance进行关联,获取IP-PORT信息,能够与应用对接起来。
event_name主要包含3类:
wait/io/socket/sql/server_unix_socket,服务端unix监听socket
wait/io/socket/sql/server_tcpip_socket,服务端tcp监听socket
wait/io/socket/sql/client_connection,客户端socket
三:Wait表
1,events_waits_current:记录了当前线程等待的事件
2,events_waits_history:记录了每个线程最近等待的10个事件
3,events_waits_history_long:记录了最近所有线程产生的10000个事件
表结构定义如下:
CREATE TABLE `events_waits_current` (
`THREAD_ID` bigint(20) unsigned NOT NULL COMMENT '线程ID',
`EVENT_ID` bigint(20) unsigned NOT NULL COMMENT '当前线程的事件ID,和THREAD_ID确定唯一',
`END_EVENT_ID` bigint(20) unsigned DEFAULT NULL COMMENT '当事件开始时,这一列被设置为NULL。当事件结束时,再更新为当前的事件ID',
`EVENT_NAME` varchar(128) NOT NULL COMMENT '事件名称',
`SOURCE` varchar(64) DEFAULT NULL COMMENT '该事件产生时的源码文件',
`TIMER_START` bigint(20) unsigned DEFAULT NULL COMMENT '事件开始时间(皮秒)',
`TIMER_END` bigint(20) unsigned DEFAULT NULL COMMENT '事件结束结束时间(皮秒)',
`TIMER_WAIT` bigint(20) unsigned DEFAULT NULL COMMENT '事件等待时间(皮秒)',
`SPINS` int(10) unsigned DEFAULT NULL COMMENT '',
`OBJECT_SCHEMA` varchar(64) DEFAULT NULL COMMENT '库名',
`OBJECT_NAME` varchar(512) DEFAULT NULL COMMENT '文件名、表名、IP:SOCK值',
`OBJECT_TYPE` varchar(64) DEFAULT NULL COMMENT 'FILE、TABLE、TEMPORARY TABLE',
`INDEX_NAME` varchar(64) DEFAULT NULL COMMENT '索引名',
`OBJECT_INSTANCE_BEGIN` bigint(20) unsigned NOT NULL COMMENT '内存地址',
`NESTING_EVENT_ID` bigint(20) unsigned DEFAULT NULL COMMENT '该事件对应的父事件ID',
`NESTING_EVENT_TYPE` enum('STATEMENT','STAGE','WAIT') DEFAULT NULL COMMENT '父事件类型(STATEMENT, STAGE, WAIT)',
`OPERATION` varchar(32) NOT NULL COMMENT '操作类型(lock, read, write)',
`NUMBER_OF_BYTES` bigint(20) DEFAULT NULL COMMENT '',
`FLAGS` int(10) unsigned DEFAULT NULL COMMENT '标记'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
四:Stage 表
1,events_stages_current:记录了当前线程所处的执行阶段
2,events_stages_history:记录了当前线程所处的执行阶段10条历史记录
3,events_stages_history_long:记录了当前线程所处的执行阶段10000条历史记录
表结构定义如下:
CREATE TABLE `events_stages_current` (
`THREAD_ID` bigint(20) unsigned NOT NULL COMMENT '线程ID',
`EVENT_ID` bigint(20) unsigned NOT NULL COMMENT '事件ID',
`END_EVENT_ID` bigint(20) unsigned DEFAULT NULL COMMENT '结束事件ID',
`EVENT_NAME` varchar(128) NOT NULL COMMENT '事件名称',
`SOURCE` varchar(64) DEFAULT NULL COMMENT '源码位置',
`TIMER_START` bigint(20) unsigned DEFAULT NULL COMMENT '事件开始时间(皮秒)',
`TIMER_END` bigint(20) unsigned DEFAULT NULL COMMENT '事件结束结束时间(皮秒)',
`TIMER_WAIT` bigint(20) unsigned DEFAULT NULL COMMENT '事件等待时间(皮秒)',
`NESTING_EVENT_ID` bigint(20) unsigned DEFAULT NULL COMMENT '该事件对应的父事件ID',
`NESTING_EVENT_TYPE` enum('STATEMENT','STAGE','WAIT') DEFAULT NULL COMMENT '父事件类型(STATEMENT, STAGE, WAIT)'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
五:Statement 表
1,events_statements_current:通过 thread_id+event_id可以唯一确定一条记录。Statments表只记录最顶层的请求,SQL语句或是COMMAND,每条语句一行。event_name形式为statement/sql/*,或statement/com/*
2,events_statements_history
3,events_statements_history_long
表结构定义如下:
CREATE TABLE `events_statements_current` (
`THREAD_ID` bigint(20) unsigned NOT NULL COMMENT '线程ID',
`EVENT_ID` bigint(20) unsigned NOT NULL COMMENT '事件ID',
`END_EVENT_ID` bigint(20) unsigned DEFAULT NULL COMMENT '结束事件ID',
`EVENT_NAME` varchar(128) NOT NULL COMMENT '事件名称',
`SOURCE` varchar(64) DEFAULT NULL COMMENT '源码位置',
`TIMER_START` bigint(20) unsigned DEFAULT NULL COMMENT '事件开始时间(皮秒)',
`TIMER_END` bigint(20) unsigned DEFAULT NULL COMMENT '事件结束结束时间(皮秒)',
`TIMER_WAIT` bigint(20) unsigned DEFAULT NULL COMMENT '事件等待时间(皮秒)',
`LOCK_TIME` bigint(20) unsigned NOT NULL COMMENT '锁时间',
`SQL_TEXT` longtext COMMENT '记录SQL语句',
`DIGEST` varchar(32) DEFAULT NULL COMMENT '对SQL_TEXT做MD5产生的32位字符串',
`DIGEST_TEXT` longtext COMMENT '将语句中值部分用问号代替,用于SQL语句归类',
`CURRENT_SCHEMA` varchar(64) DEFAULT NULL COMMENT '默认的数据库名',
`OBJECT_TYPE` varchar(64) DEFAULT NULL COMMENT '保留字段',
`OBJECT_SCHEMA` varchar(64) DEFAULT NULL COMMENT '保留字段',
`OBJECT_NAME` varchar(64) DEFAULT NULL COMMENT '保留字段',
`OBJECT_INSTANCE_BEGIN` bigint(20) unsigned DEFAULT NULL COMMENT '内存地址',
`MYSQL_ERRNO` int(11) DEFAULT NULL COMMENT '',
`RETURNED_SQLSTATE` varchar(5) DEFAULT NULL COMMENT '',
`MESSAGE_TEXT` varchar(128) DEFAULT NULL COMMENT '信息',
`ERRORS` bigint(20) unsigned NOT NULL COMMENT '错误数目',
`WARNINGS` bigint(20) unsigned NOT NULL COMMENT '警告数目',
`ROWS_AFFECTED` bigint(20) unsigned NOT NULL COMMENT '影响的数目',
`ROWS_SENT` bigint(20) unsigned NOT NULL COMMENT '返回的记录数',
`ROWS_EXAMINED` bigint(20) unsigned NOT NULL COMMENT '读取扫描的记录数目',
`CREATED_TMP_DISK_TABLES` bigint(20) unsigned NOT NULL COMMENT '创建磁盘临时表数目',
`CREATED_TMP_TABLES` bigint(20) unsigned NOT NULL COMMENT '创建临时表数目',
`SELECT_FULL_JOIN` bigint(20) unsigned NOT NULL COMMENT 'join时,第一个表为全表扫描的数目',
`SELECT_FULL_RANGE_JOIN` bigint(20) unsigned NOT NULL COMMENT '引用表采用range方式扫描的数目',
`SELECT_RANGE` bigint(20) unsigned NOT NULL COMMENT 'join时,第一个表采用range方式扫描的数目',
`SELECT_RANGE_CHECK` bigint(20) unsigned NOT NULL COMMENT '',
`SELECT_SCAN` bigint(20) unsigned NOT NULL COMMENT 'join时,第一个表位全表扫描的数目',
`SORT_MERGE_PASSES` bigint(20) unsigned NOT NULL COMMENT '',
`SORT_RANGE` bigint(20) unsigned NOT NULL COMMENT '范围排序数目',
`SORT_ROWS` bigint(20) unsigned NOT NULL COMMENT '排序的记录数目',
`SORT_SCAN` bigint(20) unsigned NOT NULL COMMENT '全表排序数目',
`NO_INDEX_USED` bigint(20) unsigned NOT NULL COMMENT '没有使用索引数目',
`NO_GOOD_INDEX_USED` bigint(20) unsigned NOT NULL COMMENT '',
`NESTING_EVENT_ID` bigint(20) unsigned DEFAULT NULL COMMENT '该事件对应的父事件ID',
`NESTING_EVENT_TYPE` enum('STATEMENT','STAGE','WAIT') DEFAULT NULL COMMENT '父事件类型(STATEMENT, STAGE, WAIT)'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
六:Connection 表
1,users:记录用户连接数信息
2,hosts:记录了主机连接数信息
3,accounts:记录了用户主机连接数信息
zjy@performance_schema 12:03:27>select * from users;
+------------------+---------------------+-------------------+
| USER | CURRENT_CONNECTIONS | TOTAL_CONNECTIONS |
+------------------+---------------------+-------------------+
| debian-sys-maint | 0 | 36 |
| zjy | 1 | 22285 |
| dchat_php | 0 | 37864 |
| dxyslave | 2 | 9 |
| nagios | 0 | 10770 |
| dchat_data | 140 | 2233023 |
| NULL | 0 | 15866 |
| dchat_api | 160 | 2754212 |
| mha_data | 1 | 36 |
| backup | 0 | 15 |
| cacti | 0 | 4312 |
| kol | 10 | 172414 |
+------------------+---------------------+-------------------+
12 rows in set (0.00 sec)
zjy@performance_schema 12:03:34>select * from hosts;
+-----------------+---------------------+-------------------+
| HOST | CURRENT_CONNECTIONS | TOTAL_CONNECTIONS |
+-----------------+---------------------+-------------------+
| 192.168.100.218 | 150 | 2499422 |
| 192.168.100.240 | 10 | 172429 |
| 192.168.100.139 | 0 | 698 |
| 192.168.100.21 | 0 | 2 |
| 192.168.100.220 | 150 | 2526136 |
| 192.168.100.25 | 1 | 7 |
| NULL | 0 | 15867 |
| 192.168.100.241 | 0 | 21558 |
| 192.168.100.191 | 1 | 34 |
| localhost | 0 | 10807 |
| 192.168.100.118 | 1 | 2 |
| 192.168.100.251 | 0 | 4312 |
| 192.168.100.23 | 1 | 31 |
| 192.168.100.193 | 0 | 15 |
+-----------------+---------------------+-------------------+
14 rows in set (0.01 sec)
zjy@performance_schema 12:05:21>select * from accounts;
+------------------+-----------------+---------------------+-------------------+
| USER | HOST | CURRENT_CONNECTIONS | TOTAL_CONNECTIONS |
+------------------+-----------------+---------------------+-------------------+
| cacti | 192.168.100.251 | 0 | 4313 |
| debian-sys-maint | localhost | 0 | 36 |
| backup | 192.168.100.193 | 0 | 15 |
| dchat_api | 192.168.100.220 | 80 | 1382585 |
| dchat_php | 192.168.100.220 | 0 | 20292 |
| zjy | 192.168.100.139 | 0 | 698 |
| zjy | 192.168.100.241 | 0 | 21558 |
| mha_data | 192.168.100.191 | 1 | 34 |
| dxyslave | 192.168.100.118 | 1 | 2 |
| kol | 192.168.100.240 | 10 | 172431 |
| dxyslave | 192.168.100.25 | 1 | 7 |
| dchat_data | 192.168.100.218 | 70 | 1109974 |
| zjy | 192.168.100.23 | 1 | 31 |
| dchat_php | 192.168.100.218 | 0 | 17572 |
| dchat_data | 192.168.100.220 | 70 | 1123306 |
| NULL | NULL | 0 | 15868 |
| mha_data | 192.168.100.21 | 0 | 2 |
| dchat_api | 192.168.100.218 | 80 | 1371918 |
| nagios | localhost | 0 | 10771 |
+------------------+-----------------+---------------------+-------------------+
七:Summary 表: Summary表聚集了各个维度的统计信息包括表维度,索引维度,会话维度,语句维度和锁维度的统计信息
1,events_waits_summary_global_by_event_name:按等待事件类型聚合,每个事件一条记录
CREATE TABLE `events_waits_summary_global_by_event_name` (
`EVENT_NAME` varchar(128) NOT NULL COMMENT '事件名称',
`COUNT_STAR` bigint(20) unsigned NOT NULL COMMENT '事件计数',
`SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '总的等待时间',
`MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最小等待时间',
`AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '平均等待时间',
`MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最大等待时间'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
2,events_waits_summary_by_instance:按等待事件对象聚合,同一种等待事件,可能有多个实例,每个实例有不同的内存地址,因此
event_name+object_instance_begin唯一确定一条记录。
CREATE TABLE `events_waits_summary_by_instance` (
`EVENT_NAME` varchar(128) NOT NULL COMMENT '事件名称',
`OBJECT_INSTANCE_BEGIN` bigint(20) unsigned NOT NULL COMMENT '内存地址',
`COUNT_STAR` bigint(20) unsigned NOT NULL COMMENT '事件计数',
`SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '总的等待时间',
`MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最小等待时间',
`AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '平均等待时间',
`MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最大等待时间'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
3,events_waits_summary_by_thread_by_event_name:按每个线程和事件来统计,thread_id+event_name唯一确定一条记录。
CREATE TABLE `events_waits_summary_by_thread_by_event_name` (
`THREAD_ID` bigint(20) unsigned NOT NULL COMMENT '线程ID',
`EVENT_NAME` varchar(128) NOT NULL COMMENT '事件名称',
`COUNT_STAR` bigint(20) unsigned NOT NULL COMMENT '事件计数',
`SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '总的等待时间',
`MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最小等待时间',
`AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '平均等待时间',
`MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最大等待时间'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
4,events_stages_summary_global_by_event_name:按事件阶段类型聚合,每个事件一条记录,表结构同上。
5,events_stages_summary_by_thread_by_event_name:按每个线程和事件来阶段统计,表结构同上。
6,events_statements_summary_by_digest:按照事件的语句进行聚合。
CREATE TABLE `events_statements_summary_by_digest` (
`SCHEMA_NAME` varchar(64) DEFAULT NULL COMMENT '库名',
`DIGEST` varchar(32) DEFAULT NULL COMMENT '对SQL_TEXT做MD5产生的32位字符串。如果为consumer表中没有打开statement_digest选项,则为NULL',
`DIGEST_TEXT` longtext COMMENT '将语句中值部分用问号代替,用于SQL语句归类。如果为consumer表中没有打开statement_digest选项,则为NULL。',
`COUNT_STAR` bigint(20) unsigned NOT NULL COMMENT '事件计数',
`SUM_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '总的等待时间',
`MIN_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最小等待时间',
`AVG_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '平均等待时间',
`MAX_TIMER_WAIT` bigint(20) unsigned NOT NULL COMMENT '最大等待时间',
`SUM_LOCK_TIME` bigint(20) unsigned NOT NULL COMMENT '锁时间总时长',
`SUM_ERRORS` bigint(20) unsigned NOT NULL COMMENT '错误数的总',
`SUM_WARNINGS` bigint(20) unsigned NOT NULL COMMENT '警告的总数',
`SUM_ROWS_AFFECTED` bigint(20) unsigned NOT NULL COMMENT '影响的总数目',
`SUM_ROWS_SENT` bigint(20) unsigned NOT NULL COMMENT '返回总数目',
`SUM_ROWS_EXAMINED` bigint(20) unsigned NOT NULL COMMENT '总的扫描的数目',
`SUM_CREATED_TMP_DISK_TABLES` bigint(20) unsigned NOT NULL COMMENT '创建磁盘临时表的总数目',
`SUM_CREATED_TMP_TABLES` bigint(20) unsigned NOT NULL COMMENT '创建临时表的总数目',
`SUM_SELECT_FULL_JOIN` bigint(20) unsigned NOT NULL COMMENT '第一个表全表扫描的总数目',
`SUM_SELECT_FULL_RANGE_JOIN` bigint(20) unsigned NOT NULL COMMENT '总的采用range方式扫描的数目',
`SUM_SELECT_RANGE` bigint(20) unsigned NOT NULL COMMENT '第一个表采用range方式扫描的总数目',
`SUM_SELECT_RANGE_CHECK` bigint(20) unsigned NOT NULL COMMENT '',
`SUM_SELECT_SCAN` bigint(20) unsigned NOT NULL COMMENT '第一个表位全表扫描的总数目',
`SUM_SORT_MERGE_PASSES` bigint(20) unsigned NOT NULL COMMENT '',
`SUM_SORT_RANGE` bigint(20) unsigned NOT NULL COMMENT '范围排序总数',
`SUM_SORT_ROWS` bigint(20) unsigned NOT NULL COMMENT '排序的记录总数目',
`SUM_SORT_SCAN` bigint(20) unsigned NOT NULL COMMENT '第一个表排序扫描总数目',
`SUM_NO_INDEX_USED` bigint(20) unsigned NOT NULL COMMENT '没有使用索引总数',
`SUM_NO_GOOD_INDEX_USED` bigint(20) unsigned NOT NULL COMMENT '',
`FIRST_SEEN` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '第一次执行时间',
`LAST_SEEN` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '最后一次执行时间'
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8
7,events_statements_summary_global_by_event_name:按照事件的语句进行聚合。表结构同上。
8,events_statements_summary_by_thread_by_event_name:按照线程和事件的语句进行聚合,表结构同上。
9,file_summary_by_instance:按事件类型统计(物理IO维度)
10,file_summary_by_event_name:具体文件统计(物理IO维度)
9和10一起说明:
统计IO操作:COUNT_STAR,SUM_TIMER_WAIT,MIN_TIMER_WAIT,AVG_TIMER_WAIT,MAX_TIMER_WAIT
统计读 :COUNT_READ,SUM_TIMER_READ,MIN_TIMER_READ,AVG_TIMER_READ,MAX_TIMER_READ, SUM_NUMBER_OF_BYTES_READ
统计写 :COUNT_WRITE,SUM_TIMER_WRITE,MIN_TIMER_WRITE,AVG_TIMER_WRITE,MAX_TIMER_WRITE, SUM_NUMBER_OF_BYTES_WRITE
统计其他IO事件,比如create,delete,open,close等:COUNT_MISC,SUM_TIMER_MISC,MIN_TIMER_MISC,AVG_TIMER_MISC,MAX_TIMER_MISC
11,table_io_waits_summary_by_table:根据wait/io/table/sql/handler,聚合每个表的I/O操作(逻辑IO纬度)
统计IO操作:COUNT_STAR,SUM_TIMER_WAIT,MIN_TIMER_WAIT,AVG_TIMER_WAIT,MAX_TIMER_WAIT
统计读 :COUNT_READ,SUM_TIMER_READ,MIN_TIMER_READ,AVG_TIMER_READ,MAX_TIMER_READ
:COUNT_FETCH,SUM_TIMER_FETCH,MIN_TIMER_FETCH,AVG_TIMER_FETCH, MAX_TIMER_FETCH
统计写 :COUNT_WRITE,SUM_TIMER_WRITE,MIN_TIMER_WRITE,AVG_TIMER_WRITE,MAX_TIMER_WRITE
INSERT统计,相应的还有DELETE和UPDATE统计:COUNT_INSERT,SUM_TIMER_INSERT,MIN_TIMER_INSERT,AVG_TIMER_INSERT,MAX_TIMER_INSERT
12,table_io_waits_summary_by_index_usage:与table_io_waits_summary_by_table类似,按索引维度统计
13,table_lock_waits_summary_by_table:聚合了表锁等待事件,包括internal lock 和 external lock
internal lock通过SQL层函数thr_lock调用,OPERATION值为:
read normal、read with shared locks、read high priority、read no insert、write allow write、write concurrent insert、write delayed、write low priority、write normal
external lock则通过接口函数handler::external_lock调用存储引擎层,OPERATION列的值为:read external、write external
14,Connection Summaries表:account、user、host
events_waits_summary_by_account_by_event_name
events_waits_summary_by_user_by_event_name
events_waits_summary_by_host_by_event_name
events_stages_summary_by_account_by_event_name
events_stages_summary_by_user_by_event_name
events_stages_summary_by_host_by_event_name
events_statements_summary_by_account_by_event_name
events_statements_summary_by_user_by_event_name
events_statements_summary_by_host_by_event_name
15,socket_summary_by_instance、socket_summary_by_event_name:socket聚合统计表。
八:其他相关表
1,performance_timers:系统支持的统计时间单位
2,threads:监视服务端的当前运行的线程
统计应用:
关于SQL维度的统计信息主要集中在events_statements_summary_by_digest表中,通过将SQL语句抽象出digest,可以统计某类SQL语句在各个维度的统计信息
1,哪个SQL执行最多:
zjy@performance_schema 11:36:22>SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY COUNT_STAR desc LIMIT 1\G
*************************** 1. row ***************************
SCHEMA_NAME: dchat
DIGEST_TEXT: SELECT ...
COUNT_STAR: 1161210102
SUM_ROWS_SENT: 1161207842
SUM_ROWS_EXAMINED: 0
FIRST_SEEN: 2016-02-17 00:36:46
LAST_SEEN: 2016-03-07 11:36:29
各个字段的注释可以看上面的表结构说明:从2月17号到3月7号该SQL执行了1161210102次。
2,哪个SQL平均响应时间最多:
zjy@performance_schema 11:36:28>SELECT SCHEMA_NAME,DIGEST_TEXT,COUNT_STAR,AVG_TIMER_WAIT,SUM_ROWS_SENT,SUM_ROWS_EXAMINED,FIRST_SEEN,LAST_SEEN FROM events_statements_summary_by_digest ORDER BY AVG_TIMER_WAIT desc LIMIT 1\G
*************************** 1. row ***************************
SCHEMA_NAME: dchat
DIGEST_TEXT: SELECT ...
COUNT_STAR: 1
AVG_TIMER_WAIT: 273238183964000
SUM_ROWS_SENT: 50208
SUM_ROWS_EXAMINED: 5565651
FIRST_SEEN: 2016-02-22 13:27:33
LAST_SEEN: 2016-02-22 13:27:33
各个字段的注释可以看上面的表结构说明:从2月17号到3月7号该SQL平均响应时间273238183964000皮秒(1000000000000皮秒=1秒)
3,哪个SQL扫描的行数最多:
SUM_ROWS_EXAMINED
4,哪个SQL使用的临时表最多:
SUM_CREATED_TMP_DISK_TABLES、SUM_CREATED_TMP_TABLES
5,哪个SQL返回的结果集最多:
SUM_ROWS_SENT
6,哪个SQL排序数最多:
SUM_SORT_ROWS
通过上述指标我们可以间接获得某类SQL的逻辑IO(SUM_ROWS_EXAMINED),CPU消耗(SUM_SORT_ROWS),网络带宽(SUM_ROWS_SENT)的对比。
通过file_summary_by_instance表,可以获得系统运行到现在,哪个文件(表)物理IO最多,这可能意味着这个表经常需要访问磁盘IO。
7,哪个表、文件逻辑IO最多(热数据):
zjy@performance_schema 12:16:18>SELECT FILE_NAME,EVENT_NAME,COUNT_READ,SUM_NUMBER_OF_BYTES_READ,COUNT_WRITE,SUM_NUMBER_OF_BYTES_WRITE FROM file_summary_by_instance ORDER BY SUM_NUMBER_OF_BYTES_READ+SUM_NUMBER_OF_BYTES_WRITE DESC LIMIT 2\G
*************************** 1. row ***************************
FILE_NAME: /var/lib/mysql/ibdata1 #文件
EVENT_NAME: wait/io/file/innodb/innodb_data_file
COUNT_READ: 544
SUM_NUMBER_OF_BYTES_READ: 10977280
COUNT_WRITE: 3700729
SUM_NUMBER_OF_BYTES_WRITE: 1433734217728
*************************** 2. row ***************************
FILE_NAME: /var/lib/mysql/dchat/fans.ibd #表
EVENT_NAME: wait/io/file/innodb/innodb_data_file
COUNT_READ: 9370680
SUM_NUMBER_OF_BYTES_READ: 153529188352
COUNT_WRITE: 67576376
SUM_NUMBER_OF_BYTES_WRITE: 1107815432192
8,哪个索引使用最多:
zjy@performance_schema 12:18:42>SELECT OBJECT_NAME, INDEX_NAME, COUNT_FETCH, COUNT_INSERT, COUNT_UPDATE, COUNT_DELETE FROM table_io_waits_summary_by_index_usage ORDER BY SUM_TIMER_WAIT DESC limit 1;
+-------------+------------+-------------+--------------+--------------+--------------+
| OBJECT_NAME | INDEX_NAME | COUNT_FETCH | COUNT_INSERT | COUNT_UPDATE | COUNT_DELETE |
+-------------+------------+-------------+--------------+--------------+--------------+
| fans | PRIMARY | 29002695158 | 0 | 296373434 | 0 |
+-------------+------------+-------------+--------------+--------------+--------------+
1 row in set (0.29 sec)
通过table_io_waits_summary_by_index_usage表,可以获得系统运行到现在,哪个表的具体哪个索引(包括主键索引,二级索引)使用最多。
9,哪个索引没有使用过:
zjy@performance_schema 12:23:22>SELECT OBJECT_SCHEMA, OBJECT_NAME, INDEX_NAME FROM 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;
10,哪个等待事件消耗的时间最多:
zjy@performance_schema 12:25: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;
11,类似profiling功能:
分析具体某条SQL,该SQL在执行各个阶段的时间消耗,通过events_statements_xxx表和events_stages_xxx表,就可以达到目的。两个表通过event_id与nesting_event_id关联,stages表的nesting_event_id为对应statements表的event_id;针对每个stage可能出现的锁等待,一个stage会对应一个或多个wait,通过stage_xxx表的event_id字段与waits_xxx表的nesting_event_id进行关联。如:
View Code
总结:
本文通过对Performance Schema数据库的介绍,主要用于收集数据库服务器性能参数:①提供进程等待的详细信息,包括锁、互斥变量、文件信息;②保存历史的事件汇总信息,为提供MySQL服务器性能做出详细的判断;③对于新增和删除监控事件点都非常容易,并可以改变mysql服务器的监控周期,例如(CYCLE、MICROSECOND)。通过该库得到数据库运行的统计信息,更好分析定位问题和完善监控信息。类似的监控还有:
打开标准的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;
参考文章:
https://dev.mysql.com/doc/refman/5.6/en/performance-schema.html
http://www.cnblogs.com/cchust/p/5022148.html
http://www.cnblogs.com/cchust/p/5057498.html
http://www.cnblogs.com/cchust/p/5061131.html
http://mysqllover.com/?p=522
~~~~~~~~~~~~~~~ 万物之中,希望至美 ~~~~~~~~~~~~~~~
PERFORMANCE_SCHEMA 详解
http://keithlan.github.io/2015/07/17/22_performance_schema/
http://www.markleith.co.uk/
http://www.cnblogs.com/cchust/p/5022148.html?utm_source=tuicool&utm_medium=referral
MYSQL 是否支持 PERFORMANCE_SCHEMA 引擎
mysql> SELECT * FROM INFORMATION_SCHEMA.ENGINES ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ENGINE | SUPPORT | COMMENT | TRANSACTIONS | XA | SAVEPOINTS |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql服务器起用 performance_schema
my.cnf文件中
[mysqld]
performance_schema=ON
mysql> SHOW VARIABLES LIKE 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
1 row in set (0.11 sec)
Performance Schema Instrument命名规则
最上层的instrument 组件
idle
stage
statement
wait
idle instrument 组件
idle event描述来自:socket_instances.STATE 列: Section 22.9.3.5, “The socket_instances Table”.
stage instrument 组件
组成形式: stage/code_area/stage_name , code_area 一般是sql or myisam。
stage name 一般来自: SHOW PROCESSLIST,如:Sorting result ,Sending data
Statement instrument 组件
statement/abstract/* : 一般都是早期的stage,在抽象sql都还没来得及解析的时候。
statement/com: SQL 命令操作 如:statement/com/Connect
statement/sql: SQL语句操作 如:statement/sql/create_db
Wait Instrument 组件
wait/io : IO 等待事件
wait/io/file : 文件IO等待事件。等待文件操作完成的时间如:fwrite().但是物理IO有可能因为缓存的原因调用fwrite时不会写磁盘。
wait/io/socket: socket相关的IO等待
wait/io/table : 表相关的IO等待。一般对于记录rows来说有fetch,insert,update,delete四种操作。
不像其他等待事件,table I/O 还包含了其他的等待事件。
比如:table io可能包含了文件IO和内存IO。因为读取table rows的时候,有可能会去从文件读取数据。
* wait/lock:
* wait/lock/table : 表操作的锁等待事件
* wait/synch:
* wait/synch/cond :condition就是线程与线程之间的信号。
* wait/synch/mutex : mutex主要用来锁住一块共享资源。
* wait/synch/rwlock : 读写锁
收集所有的事件:
instrument 和 consumer 都会被enable , 所以一开始他们不会收集所有的事件。
为了让他们都enable 或者 enable event timing。
执行以下两条语句:
mysql> UPDATE setup_instruments SET ENABLED = 'YES', TIMED = 'YES';
Query OK, 338 rows affected (0.12 sec)
mysql> UPDATE setup_consumers SET ENABLED = 'YES';
Query OK, 8 rows affected (0.00 sec)
events_waits_history & events_waits_history_long 记录了每个thread最近10条和10000条event。
SELECT EVENT_ID, EVENT_NAME, TIMER_WAIT FROM events_waits_history WHERE THREAD_ID =21 ORDER BY EVENT_ID;
summary 表提供了整个时间段的一些统计信息。他们统计事件的处理方式和之前都不一样。
如果想知道某个instrument 被执行的最频繁,或者发生的频率非常高,
可以通过排序 events_waits_summary_global_by_event_name表,根据 COUNT_STAR 或者 SUM_TIMER_WAIT列。
mysql> SELECT EVENT_NAME, COUNT_STAR FROM events_waits_summary_global_by_event_name ORDER BY COUNT_STAR DESC LIMIT 10;
+--------------------------------------+------------+
| EVENT_NAME | COUNT_STAR |
+--------------------------------------+------------+
| wait/io/file/sql/FRM | 1682 |
| wait/io/file/innodb/innodb_data_file | 596 |
| idle | 204 |
| wait/io/file/sql/binlog | 170 |
| wait/io/file/myisam/kfile | 110 |
| wait/io/file/myisam/dfile | 90 |
| wait/io/file/sql/binlog_index | 47 |
| wait/io/file/innodb/innodb_log_file | 18 |
| wait/io/file/sql/dbopt | 14 |
| wait/io/file/sql/casetest | 10 |
+--------------------------------------+------------+
10 rows in set (0.31 sec)
mysql> SELECT EVENT_NAME, SUM_TIMER_WAIT FROM events_waits_summary_global_by_event_name ORDER BY SUM_TIMER_WAIT DESC LIMIT 10;
+--------------------------------------+-------------------+
| EVENT_NAME | SUM_TIMER_WAIT |
+--------------------------------------+-------------------+
| idle | 20173041031000000 |
| wait/io/file/innodb/innodb_data_file | 666638377892 |
| wait/io/file/sql/dbopt | 177140672226 |
| wait/io/file/sql/FRM | 91152018924 |
| wait/io/file/sql/binlog | 43352476474 |
| wait/io/file/innodb/innodb_log_file | 35962857780 |
| wait/io/file/sql/binlog_index | 20679496964 |
| wait/io/file/myisam/kfile | 16840088732 |
| wait/io/file/sql/ERRMSG | 8064330196 |
| wait/io/file/myisam/dfile | 4785274304 |
+--------------------------------------+-------------------+
10 rows in set (0.03 sec)
setup表用来配置和显示监控信息的。 例如:什么样的timer 被使用,
请查询setup_timers
mysql> SELECT * FROM setup_timers;
+-----------+-------------+
| NAME | TIMER_NAME |
+-----------+-------------+
| idle | MICROSECOND |
| wait | CYCLE |
| stage | NANOSECOND |
| statement | NANOSECOND |
+-----------+-------------+
4 rows in set (0.00 sec)
setup_instruments 列出了哪些event会被收集与监控:
mysql> SELECT * FROM setup_instruments limit 10;
+-------------------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+-------------------------------------------------------+---------+-------+
| wait/synch/mutex/sql/PAGE::lock | YES | YES |
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_sync | YES | YES |
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_active | YES | YES |
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_pool | YES | YES |
| wait/synch/mutex/sql/LOCK_des_key_file | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit_queue | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_done | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_flush_queue | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_index | YES | YES |
+-------------------------------------------------------+---------+-------+
10 rows in set (0.00 sec)
哪些event是不是instrument,可以给enabled设置yes or no
mysql> UPDATE setup_instruments SET ENABLED = 'NO' WHERE NAME = 'wait/synch/mutex/sql/LOCK_mysql_create_db';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0 Changed: 0 Warnings: 0
消费者:接收EVENTS 的表
performance schema 使用收集的events 来更新performance_schema 数据库的那些表,这些表扮演着事件信息消费者的角色。
setup_consumers 列出了可用的消费者 以及哪些是enabled.
mysql> SELECT * FROM setup_consumers;
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_stages_current | YES |
| events_stages_history | YES |
| events_stages_history_long | YES |
| events_statements_current | YES |
| events_statements_history | YES |
| events_statements_history_long | YES |
| events_waits_current | YES |
| events_waits_history | YES |
| events_waits_history_long | YES |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
http://www.javacoder.cn/?p=332
MySQL在5.6版本中包含了一个强大的特性——performance-schema
合理的使用这个数据库中的表,能为我们解决一些瓶颈问题提供帮助,但是在我使用的5.6.21版本中,已经包含了52张表,后续还有可能会持续增加,所以搞清楚各个表之间的关系很重要。
mysql> show tables like '%setup%';
+----------------------------------------+
| Tables_in_performance_schema (%setup%) |
+----------------------------------------+
| setup_actors |
| setup_consumers |
| setup_instruments |
| setup_objects |
| setup_timers |
+----------------------------------------+
5 rows in set (0.01 sec)
1)setup_instruments [测量仪器]
使用performance-schema诊断问题的过程类似于医生给病人看病的过程。"setup_instruments"犹如医生能够使用的测量仪器(比如温度计,CT,X光等),在MySQL的官方文档中,使用“instrument”一词来表示,这应该也是这个表的名称的来源。这个表的每一行代表一个"instrument","instrument"在MySQL的源码中表示为一段为了测量相应指标的代码。这张表的三个字段分别为“name enabled timed”,enabled表示是否启用该"instrument", "timed"表示是否对其进行定量的耗时分析。
mysql> SELECT * FROM setup_instruments limit 10;
+-------------------------------------------------------+---------+-------+
| NAME | ENABLED | TIMED |
+-------------------------------------------------------+---------+-------+
| wait/synch/mutex/sql/PAGE::lock | YES | YES |
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_sync | YES | YES |
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_active | YES | YES |
| wait/synch/mutex/sql/TC_LOG_MMAP::LOCK_pool | YES | YES |
| wait/synch/mutex/sql/LOCK_des_key_file | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_commit_queue | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_done | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_flush_queue | YES | YES |
| wait/synch/mutex/sql/MYSQL_BIN_LOG::LOCK_index | YES | YES |
+-------------------------------------------------------+---------+-------+
10 rows in set (0.01 sec)
2)setup_objects
setup_objects类似某类病人(精神病,男科,妇科等),在MySQL中指table, 表示哪些表需要“instrument”,该表的默认值为:
mysql> SELECT * FROM setup_objects limit 10;
+-------------+--------------------+-------------+---------+-------+
| OBJECT_TYPE | OBJECT_SCHEMA | OBJECT_NAME | ENABLED | TIMED |
+-------------+--------------------+-------------+---------+-------+
| TABLE | mysql | % | NO | NO |
| TABLE | performance_schema | % | NO | NO |
| TABLE | information_schema | % | NO | NO |
| TABLE | % | % | YES | YES |
+-------------+--------------------+-------------+---------+-------+
4 rows in set (0.00 sec)
表示出了系统表“mysql performance_schema information_schema”外,都需要对其进行“instrument”操作。
3)threads
"threads“类似于某个病人,在MySQL中的某个线程,这个表有一个非常重要的字段”INSTRUMENTED“,表示是否需要对该线程进行”instrument“,这个字段由”setup_actors“表中的内容决定。setup_actors的默认内容如下
mysql> SELECT THREAD_ID,NAME,TYPE,INSTRUMENTED,role FROM threads limit 10;
+-----------+---------------------------------+------------+--------------+------+
| THREAD_ID | NAME | TYPE | INSTRUMENTED | role |
+-----------+---------------------------------+------------+--------------+------+
| 1 | thread/sql/main | BACKGROUND | YES | NULL |
| 2 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 3 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 4 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 5 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 6 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 7 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 8 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 11 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
| 9 | thread/innodb/io_handler_thread | BACKGROUND | YES | NULL |
+-----------+---------------------------------+------------+--------------+------+
10 rows in set (0.01 sec)
线程分前台线程和后台线程,后台线程指MySQL为了完成任务二自己开启的线程,这样的线程的setup_objects.INSTRUMENTED字段默认为true。
前台线程值用户操作而开启的线程。这时如果当前用户匹配setup_actors中的某个记录,那么setup_objects.INSTRUMENTED=true.
threads表的另一个功能是可以代替"show processlist"指令和"information_schema.processlist"表的功能,其不用获取同步的mutex对象,不会又那么重的额外消耗(overhead).
4)setup_consumers
setup_consumers表类似于各种诊断报告是否需要记录的配置,这个表的默认配置为:select * from setup_consumers;
mysql> select * from setup_consumers;
+--------------------------------+---------+
| NAME | ENABLED |
+--------------------------------+---------+
| events_stages_current | YES |
| events_stages_history | YES |
| events_stages_history_long | YES |
| events_statements_current | YES |
| events_statements_history | YES |
| events_statements_history_long | YES |
| events_waits_current | YES |
| events_waits_history | YES |
| events_waits_history_long | YES |
| global_instrumentation | YES |
| thread_instrumentation | YES |
| statements_digest | YES |
+--------------------------------+---------+
12 rows in set (0.00 sec)
5)setup_actors
mysql> select * from setup_actors ;
+------+------+------+
| HOST | USER | ROLE |
+------+------+------+
| % | % | % |
+------+------+------+
1 row in set (0.16 sec)
6)setup_timers
mysql> select * from setup_timers ;
+-----------+-------------+
| NAME | TIMER_NAME |
+-----------+-------------+
| idle | MICROSECOND |
| wait | CYCLE |
| stage | NANOSECOND |
| statement | NANOSECOND |
+-----------+-------------+
4 rows in set (0.00 sec)
这个表的各个记录还存在层级关系,只有当上级的配置启用时才会考虑下级的配置。
层级关系为:
global_instrumentation
|----thread_instrumentation
| |----events_waits_current
| | |-events_waits_history
| | |-events_waits_history_long
| |----events_stages_current
| | |-events_stages_history
| | |-events_stages_history_long
| |----events_statements_current
| |-events_statements_history
| |-events_statements_history_long
|-----statements_digest
当global_instrumentation启用时,"thread_instrumentation"和"statements_digest"的配置才有可能生效。其他的配置类推。
只有当"setup_instruments","setup_objects",”setup_consumers“和"threads"都某一项测量指标都启用时才能收集到它的信息。
结束语
对于”statement, stages, waits“这三类统计信息,除了setup_consumers中配置的名称,还有summary版本的统计信息(summary_by_host_by_event_name, summary_by_user_event_name, summary_global_by_event_name)等按照各个维度的统计信息,外加file_instance, mutext_instance, rwlock_instance, table_io_waits_summary_by_index_usage, table_io_waits_summary_by_table,table_lock_waits_summary_by_table,外加一些杂项信息。
由于这些都是基础信息,网上有一个叫做ps_helper的项目,它提供了一些视图,存储过程来简化performence_schema数据库的使用,
MySQL官方的GUI工具——MySQL Workbench就含义ps_helper的安装向导入下图,
安装完后会新增一个名为"sys"的数据库,里面会包含一些视图和存储过程,具体的内容可以查看官方文档。
一些不错的资料:
ps_helper官方
MySQL Performance_Schema官网
Using-MySQL-5.6-Performance-Schema-To-Troubleshoot-Typical-Workload-Bottlenecks.pdf
本文转载自:http://www.mysqlops.com/2011/09/23/performance-schema.html
简单介绍performance_schema数据库
MySQL 5.5新增一个存储引擎:命名PERFORMANCE_SCHEMA ,主要用于收集数据库服务器性能参数。MySQL用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表
performance_schema提供以下功能:
- 提供进程等待的详细信息,包括锁、互斥变量、文件信息;
- 保存历史的事件汇总信息,为提供MySQL服务器性能做出详细的判断;
- 对于新增和删除监控事件点都非常容易,并可以随意改变mysql服务器的监控周期,例如(CYCLE、MICROSECOND)
通过以上得到的信息,DBA能够较明细得了解性能降低可能是由于哪些瓶颈?
l performance_schema功能开启和部分表功能
Performance的开启很简单,在my.cnf中[mysqld]加入performanc_schema,检查性能数据库是否启动的命令:
SHOW VARIABLES LIKE ‘performance_schema’;
若是返回的 值为ON,则说明性能数据库正常开启状态。
接下来你能看到每个表的变化:
1.Threads
SHOW PROCESSLIST;
SELECT * FROM performance_schema.threads;
看到theads表的作用了吗?监控服务器所有连接, thread_id这个在接下来非常有用,
2.Performance_timers
设置一些监控信息
root@localhost : performance_schema 05:23:28> SELECT * FROM performance_timers;
+————-+—————–+——————+—————-+
| TIMER_NAME | TIMER_FREQUENCY | TIMER_RESOLUTION | TIMER_OVERHEAD |
+————-+—————–+——————+—————-+
| CYCLE | 2603393034 | 1 | 66 |
| NANOSECOND | 1000000000 | 1000 | 682 |
| MICROSECOND | 1000000 | 1 | 227 |
| MILLISECOND | 1039 | 1 | 247 |
| TICK | 100 | 1 | 634 |
+————-+—————–+——————+—————-+
Performance_timers指定mysql服务可用的监控周期,CYCLE表示按每秒检测2603393034次。下面会设置监控周期
root@localhost : performance_schema 05:20:33> select * from setup_timers;
+——+————+
| NAME | TIMER_NAME |
+——+————+
| wait | CYCLE |
+——+————+
UPDATE setup_timers SET TIMER_NAME= ‘NANOSECOND’ WHERE NAME=’wait’;
3.Setup_consumers\ Setup_instruments
接下来设置哪些事件能够被收集
root@localhost : performance_schema 05:23:45> SELECT * FROM setup_consumers;
+———————————————-+———+
| NAME | ENABLED |
+———————————————-+———+
| events_waits_current | YES |
| events_waits_history | YES |
| events_waits_history_long | YES |
| events_waits_summary_by_thread_by_event_name | YES |
| events_waits_summary_by_event_name | YES |
| events_waits_summary_by_instance | YES |
| file_summary_by_event_name | YES |
| file_summary_by_instance | YES |
+———————————————-+———+
UPDATE setup_consumers SET ENABLED = ‘YES’ WHERE NAME=’ events_waits_history’;(表示能够被收集)
UPDATE setup_instruments SET ENABLED=’YES’ WHERE NAME=’ wait/synch/mutex/sql/PAGE::locK’;
实验收集等待信息的实例
到目前为止没有测试过是否能够检测到异常进程,见下来看看采集的数据
root@localhost : performance_schema 06:54:34> show processlist;
+——+——+———–+——————–+———+——+
| Id | User | Host | db | Command | Time |
+——+——+———–+——————–+———+——+
| 2731 | root | localhost | test | Query | 67 |
| 2732 | root | localhost | test | Sleep | 139 |
| 2733 | root | localhost | performance_schema | Query | 0 |
+——+——+———–+——————–+———+——+
root@localhost : performance_schema 06:54:16> SELECT * FROM threads where PROCESSLIST_ID IN(2731,2732,2733);
+———–+—————-+—————————+
| THREAD_ID | PROCESSLIST_ID | NAME |
+———–+—————-+—————————+
| 2742 | 2732 | thread/sql/one_connection |
| 2741 | 2731 | thread/sql/one_connection |
| 2743 | 2733 | thread/sql/one_connection |
+———–+—————-+—————————+
|
Session1 PID=2732; THREAD_ID: 2742 |
Session2 PID 2731;THREAD_ID: 2741 |
Session3 查看监控信息 |
1 |
LOCK TABLE task WRITE |
|
|
2 |
|
START TRANSACTIONupdate task set type=3 where uid=1238 and tdid=117004 and date=20110522;wait……………………. |
|
3 |
|
|
root@localhost : performance_schema 07:00:31> SELECT * FROM events_waits_current WHERE TIMER_END IS NULL\G;*************************** 1. row ***************************THREAD_ID: 2741EVENT_ID: 63EVENT_NAME: wait/synch/cond/sql/MDL_context::COND_wait_statusSOURCE: mdl.cc:983TIMER_START: 99074136559454208TIMER_END: NULLTIMER_WAIT: NULLSPINS: NULL OBJECT_SCHEMA: NULL OBJECT_NAME: NULL OBJECT_TYPE: NULL OBJECT_INSTANCE_BEGIN: 453433776 NESTING_EVENT_ID: NULL OPERATION: timed_wait NUMBER_OF_BYTES: NULL FLAGS: 0 1 row in set (0.00 sec) |
4 |
|
|
root@localhost : performance_schema 07:01:51> SELECT * FROM mutex_instances WHERE LOCKED_BY_THREAD_ID IS NOT NULL;+————————————————-+———————–+———————+| NAME | OBJECT_INSTANCE_BEGIN | LOCKED_BY_THREAD_ID |+————————————————-+———————–+———————+| wait/synch/mutex/sql/MDL_wait::LOCK_wait_status | 453433720 | 2741 |+————————————————-+———————–+———————+ |
5 |
Unlock tables |
|
|
在3步的Session3中看到events_waits_current ,看到Seesion2正在等待某个一个前提条件的释放,这个表中包含有正在等待哪个前提条件、OPERATION什么类型、SOURCE源代码位置、OBJECT_INSTANCE_BEGIN内存开始的位置
在4步的session3中mutex_instances表,存储着session2正在等待的是一个MDL的互斥变量的释放,OBJECT_INSTANCE_BEGIN内存开始位置453433720
About Me
............................................................................................................................................. ● 本文整理自网络 ● 本文在itpub(http://blog.itpub.net/26736162/abstract/1/)、博客园(http://www.cnblogs.com/lhrbest)和个人微信公众号(xiaomaimiaolhr)上有同步更新 ● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/ ● 本文博客园地址:http://www.cnblogs.com/lhrbest ● 本文pdf版、个人简介及小麦苗云盘地址:http://blog.itpub.net/26736162/viewspace-1624453/ ● 数据库笔试面试题库及解答:http://blog.itpub.net/26736162/viewspace-2134706/ ● DBA宝典今日头条号地址:http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826 ............................................................................................................................................. ● QQ群号:230161599(满)、618766405 ● 微信群:可加我微信,我拉大家进群,非诚勿扰 ● 联系我请加QQ好友(646634621),注明添加缘由 ● 于 2017-08-01 09:00 ~ 2017-08-31 22:00 在魔都完成 ● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解 ● 版权所有,欢迎分享本文,转载请保留出处 ............................................................................................................................................. ● 小麦苗的微店:https://weidian.com/s/793741433?wfr=c&ifr=shopdetail ● 小麦苗出版的数据库类丛书:http://blog.itpub.net/26736162/viewspace-2142121/ ............................................................................................................................................. 使用微信客户端扫描下面的二维码来关注小麦苗的微信公众号(xiaomaimiaolhr)及QQ群(DBA宝典),学习最实用的数据库技术。 小麦苗的微信公众号 小麦苗的DBA宝典QQ群1 小麦苗的DBA宝典QQ群2 小麦苗的微店 ............................................................................................................................................. |
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26736162/viewspace-2144018/,如需转载,请注明出处,否则将追究法律责任。