MySQL默認數據庫介紹 php
你們在學習MySQL時,安裝後都會發現裏邊已經自帶了幾個默認的數據庫,我裝的MySQL5.5裏邊自帶六個數據庫 html
網上查了一些資料對這幾個數據庫的功能作一下學習。 java
1.information_schema mysql
這個庫中有不少的表 ios
information_schema數據庫表說明: git
SCHEMATA表:提供了當前mysql實例中全部數據庫的信息。是show databases的結果取之此表。 github
TABLES表:提供了關於數據庫中的表的信息(包括視圖)。詳細表述了某個表屬於哪一個schema,表類型,表引擎,建立時間等信息。是show tables from schemaname的 結果取之此表。 面試
COLUMNS表:提供了表中的列信息。詳細表述了某張表的全部列以及每一個列的信息。是show columns from schemaname.tablename的結果取之此表。 sql
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提供如下功能:
經過以上獲得的信息,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/,如需轉載,請註明出處,不然將追究法律責任。