本篇內(nèi)容主要講解“如何查看MySQL的運(yùn)行狀態(tài)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何查看mysql的運(yùn)行狀態(tài)”吧!
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、樺南網(wǎng)絡(luò)推廣、小程序開發(fā)、樺南網(wǎng)絡(luò)營銷、樺南企業(yè)策劃、樺南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供樺南建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
通過performance_schema.events_statements_history表,查看數(shù)據(jù)庫最近執(zhí)行的一些sql語句:
(root@localhost)[(none)]> SELECT thread_id ,event_name ,source ,sys.format_time(timer_wait) ,sys.format_time(lock_time) ,sql_text ,current_schema ,message_text ,rows_affected ,rows_sent ,rows_examined FROM performance_schema.events_statements_history WHERE current_schema != 'performance_schema' ORDER BY timer_wait DESC limit 10 \G *************************** 1. row *************************** thread_id: 561166 event_name: statement/sql/select source: socket_connection.cc:101 sys.format_time(timer_wait): 53.64 ms sys.format_time(lock_time): 429.00 us sql_text: select * from oa_v_position_list current_schema: oa_2016 message_text: NULL rows_affected: 0 rows_sent: 4 rows_examined: 18051 *************************** 2. row *************************** thread_id: 153896 event_name: statement/sql/select source: socket_connection.cc:101 sys.format_time(timer_wait): 51.76 ms sys.format_time(lock_time): 96.00 us sql_text: select count(id) as num from formmain_2477 where (field0003 = 'SJCL-201911008' and ifnull(field0003, '0') != '0') current_schema: oa_2016 message_text: NULL rows_affected: 0 rows_sent: 1 rows_examined: 222 *************************** 3. row *************************** ....
我們可以使用performance_schema.events_statements_summary_by_digest表查詢經(jīng)過統(tǒng)計(jì)之后的top sql語句:
(root@localhost)[(none)]> SELECT schema_name ,digest_text ,count_star ,sys.format_time(sum_timer_wait) AS sum_time ,sys.format_time(min_timer_wait) AS min_time ,sys.format_time(avg_timer_wait) AS avg_time ,sys.format_time(max_timer_wait) AS min_time ,sys.format_time(sum_lock_time) AS sum_lock_time ,sum_rows_affected ,sum_rows_sent ,sum_rows_examined FROM performance_schema.events_statements_summary_by_digest WHERE schema_name IS NOT NULL ORDER BY count_star DESC limit 10 \G *************************** 1. row *************************** schema_name: oa_2016 digest_text: SET `autocommit` = ? count_star: 1604399319 sum_time: 13.57 h min_time: 2.00 ns avg_time: 30.46 us min_time: 39.87 s sum_lock_time: 0 ps sum_rows_affected: 0 sum_rows_sent: 0 sum_rows_examined: 0 *************************** 2. row *************************** schema_name: oa_2016 digest_text: COMMIT count_star: 368723348 sum_time: 4.15 h min_time: 10.07 us avg_time: 40.52 us min_time: 21.54 s sum_lock_time: 40.27 s sum_rows_affected: 0 sum_rows_sent: 0 sum_rows_examined: 0 *************************** 3. row *************************** schema_name: oa_2016 digest_text: SELECT * FROM `jk_JOB_DETAILS` WHERE `SCHED_NAME` = ? AND `JOB_NAME` = ? AND `JOB_GROUP` = ? count_star: 361183117 sum_time: 19.88 h min_time: 5.00 ns avg_time: 198.10 us min_time: 4.97 s sum_lock_time: 4.93 h sum_rows_affected: 0 sum_rows_sent: 361182289 sum_rows_examined: 361182289 ......
提示:performance_schema.events_statements_summary_by_digest記錄的sql并不完整,默認(rèn)情況下只截取了1024字節(jié),所以該表提供的數(shù)據(jù)只能算作慢日志分析的一個補(bǔ)充。如果需要完整sql文本,還得依賴慢日志分析。
mysql> SELECT thread_id ,event_name ,source ,sys.format_time(timer_wait) ,sys.format_time(lock_time) ,sql_text ,current_schema ,message_text ,rows_affected ,rows_sent ,rows_examined FROM performance_schema.events_statements_history WHERE errors>0 \G *************************** 1. row *************************** thread_id: 6172541 event_name: statement/sql/select source: socket_connection.cc:101 sys.format_time(timer_wait): 135.89 us sys.format_time(lock_time): 0 ps sql_text: SELECT id,waybill_num,oms_order_status FROM store_order WHERE is_sync_css_status_end IS NULL OR is_sync_css_status_end!='01' ORDER BY create_time ASC, css_search_time ASC LIMIT 0,200 current_schema: oms message_text: Table 'oms.store_order' doesn't exist rows_affected: 0 rows_sent: 0 rows_examined: 0 1 row in set (0.02 sec)
# 查看MDL鎖等待事件的instrments(采集器)是否開啟 mysql> SELECT * FROM performance_schema.setup_instruments WHERE name LIKE '%metadata/sql/mdl%'; +----------------------------+---------+-------+ | NAME | ENABLED | TIMED | +----------------------------+---------+-------+ | wait/lock/metadata/sql/mdl | NO | NO | +----------------------------+---------+-------+ 1 row in set (0.00 sec) # 啟用與MDL鎖等待事件相關(guān)的instruments(采集器) mysql> update performance_schema.setup_instruments set ENABLED='YES' where name like '%metadata/sql/mdl%'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update performance_schema.setup_instruments set TIMED='YES' where name like '%metadata/sql/mdl%'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from performance_schema.setup_instruments where name like '%metadata/sql/mdl%'; +----------------------------+---------+-------+ | NAME | ENABLED | TIMED | +----------------------------+---------+-------+ | wait/lock/metadata/sql/mdl | YES | YES | +----------------------------+---------+-------+ 1 row in set (0.01 sec) # 然后使用sys.schema_table_lock_wait視圖進(jìn)行查詢(注意:請自行模擬一個會話事務(wù)不提交, 另外一個會話發(fā)生DDL的操作,就可以查看到MDL鎖等待的內(nèi)容)。 mysql> select * from sys.schema_table_lock_waits \G
mysql> select * from sys.innodb_buffer_stats_by_table order by allocated desc limit 10 ; +---------------+--------------------+------------+------------+-------+--------------+-----------+-------------+ | object_schema | object_name | allocated | data | pages | pages_hashed | pages_old | rows_cached | +---------------+--------------------+------------+------------+-------+--------------+-----------+-------------+ | mysql | help_keyword | 96.00 KiB | 43.99 KiB | 6 | 6 | 6 | 283 | | InnoDB System | SYS_COLUMNS | 80.00 KiB | 44.50 KiB | 5 | 5 | 5 | 696 | | mdm | employee_jz | 64.00 KiB | 19.77 KiB | 4 | 4 | 4 | 177 | | mysql | innodb_index_stats | 64.00 KiB | 28.20 KiB | 4 | 4 | 4 | 288 |
mysql> SELECT * FROM information_schema.key_column_usage WHERE constraint_schema = 'oms' AND referenced_table_schema IS NOT NULL \G; *************************** 1. row *************************** CONSTRAINT_CATALOG: def CONSTRAINT_SCHEMA: oms CONSTRAINT_NAME: qrtz_blob_triggers_ibfk_1 TABLE_CATALOG: def TABLE_SCHEMA: oms TABLE_NAME: qrtz_blob_triggers COLUMN_NAME: SCHED_NAME ORDINAL_POSITION: 1 POSITION_IN_UNIQUE_CONSTRAINT: 1 REFERENCED_TABLE_SCHEMA: oms REFERENCED_TABLE_NAME: qrtz_triggers REFERENCED_COLUMN_NAME: SCHED_NAME *************************** 2. row *************************** CONSTRAINT_CATALOG: def CONSTRAINT_SCHEMA: oms
通常在開發(fā)規(guī)范中禁止使用外鍵。
mysql> SELECT TABLE_SCHEMA ,TABLE_NAME ,INDEX_NAME ,COLUMN_NAME ,CARDINALITY FROM information_schema.STATISTICS GROUP BY TABLE_SCHEMA ,TABLE_NAME limit 100; +--------------+---------------------------+-------------------------+------------------+-------------+ | TABLE_SCHEMA | TABLE_NAME | INDEX_NAME | COLUMN_NAME | CARDINALITY | +--------------+---------------------------+-------------------------+------------------+-------------+ | mdm | department | index_unique_department | deptid | 381 | | mdm | employee | PRIMARY | sn | 1544 | | mdm | employee_jz | index_unique_employee | sn | 1626 | .......
mysql> select * from information_schema.partitions where partition_name is not null; Empty set (0.02 sec)
# 將表和索引的統(tǒng)計(jì)信息數(shù)據(jù)存儲到磁盤中,默認(rèn)是開啟的 (root@localhost)[mysql]> show variables like 'innodb_stats_persistent'; +-------------------------+-------+ | Variable_name | Value | +-------------------------+-------+ | innodb_stats_persistent | ON | +-------------------------+-------+ 1 row in set (0.00 sec) # 持久化信息被存儲在mysql數(shù)據(jù)庫的如下兩張表中: (root@localhost)[mysql]> show tables from mysql like '%stats%'; +---------------------------+ | Tables_in_mysql (%stats%) | +---------------------------+ | innodb_index_stats | | innodb_table_stats | +---------------------------+ 2 rows in set (0.00 sec) # innodb_stats_auto_recalc變量控制是否啟用統(tǒng)計(jì)信息的自動重新計(jì)算功能,默認(rèn)是開啟的。如果啟用,當(dāng)表中的數(shù)據(jù)量超過10%時會 觸發(fā)統(tǒng)計(jì)信息自動重新計(jì)算功能 (root@localhost)[mysql]> show variables like 'innodb_stats_auto_recalc' -> ; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_stats_auto_recalc | ON | +--------------------------+-------+ 1 row in set (0.00 sec)
到此,相信大家對“如何查看mysql的運(yùn)行狀態(tài)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!