PERFORMANCE_SCHEMA 詳解

http://keithlan.github.io/2015/07/17/22_performance_schema/html

http://www.markleith.co.uk/java

http://www.cnblogs.com/cchust/p/5022148.html?utm_source=tuicool&utm_medium=referralmysql

 

MYSQL 是否支持 PERFORMANCE_SCHEMA 引擎git

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_schemagithub

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    組件sql

  • 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,insertupdate,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張表,後續還有可能會持續增長,因此搞清楚各個表之間的關係很重要。app

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的安裝嚮導入下圖,

 

mysql_workbench_ps_helper_setup

安裝完後會新增一個名爲"sys"的數據庫,裏面會包含一些視圖和存儲過程,具體的內容能夠查看官方文檔。

一些不錯的資料:
ps_helper官方 
MySQL Performance_Schema官網
Using-MySQL-5.6-Performance-Schema-To-Troubleshoot-Typical-Workload-Bottlenecks.pdf

相關文章
相關標籤/搜索