mysql> show GLOBAL status like 'questions';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Questions | 2009191409 |
+---------------+------------+
1 row in set (0.00 sec)
mysql> show global status like 'uptime';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Uptime | 388402 |
+---------------+--------+
1 row in set (0.00 sec)
QPS=questions/uptime=5172,mysql自啟動以來的平均QPS,如果要計算某一時間段內的QPS,可在高峰期間獲取間隔時間t2-t1,然後分別計算出t2和t1時刻的q值,QPS=(q2-q1)/(t2-t1)
mysql> show global status like 'com_update';
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| Com_update | 87094306 |
+---------------+----------+
1 row in set (0.00 sec)
mysql> show global status like 'com_select';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| Com_select | 1108143397 |
+---------------+------------+
1 row in set (0.00 sec)
mysql> show global status like 'com_delete';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Com_delete | 379058 |
+---------------+--------+
1 row in set (0.00 sec)
mysql> show global status like 'uptime';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Uptime | 388816 |
+---------------+--------+
1 row in set (0.00 sec)
QPS=(com_update+com_insert+com_delete+com_select)/uptime=3076,某一時間段內的QPS查詢方法同上。
mysql> show global status like 'com_commit';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| Com_commit | 7424815 |
+---------------+---------+
1 row in set (0.00 sec)
mysql> show global status like 'com_rollback';
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| Com_rollback | 1073179 |
+---------------+---------+
1 row in set (0.00 sec)
mysql> show global status like 'uptime';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Uptime | 389467 |
+---------------+--------+
1 row in set (0.00 sec)
TPS=(com_commit+com_rollback)/uptime=22
mysql> show global status like 'threads_%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 480 | //代表當前此時此刻線程緩存中有多少空閒線程
| Threads_connected | 153 | //代表當前已建立連接的數量,因為一個連接就需要一個線程,所以也可以看成當前被使用的線程數
| Threads_created | 20344 | //代表從最近一次服務啟動,已創建線程的數量
| Threads_running | 2 | //代表當前激活的(非睡眠狀態)線程數
+-------------------+-------+
4 rows in set (0.00 sec)
mysql> show global status like 'Connections';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| Connections | 381487397 |
+---------------+-----------+
1 row in set (0.00 sec)
線程緩存命中率=1-Threads_created/Connections = 99.994%
我們設置的線程緩存個數
mysql> show variables like '%thread_cache_size%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| thread_cache_size | 500 |
+-------------------+-------+
1 row in set (0.00 sec)
根據Threads_connected可預估thread_cache_size值應該設置多大,一般來說250是一個不錯的上限值,如果內存足夠大,也可以設置成thread_cache_size值和threaads_connected值相同;
或者通過觀察threads_created值,如果該值很大或一直在增長,可以適當增加thread_cache_size的值;在休眠狀態下每個線程大概占用256KB左右的內存,所以當內存足夠時,設置太小也不會節約太多內存,除非該值已經超過幾千。
mysql> show global status like 'open_tables%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 2228 |
+---------------+-------+
1 row in set (0.00 sec)
我們設置的打開表的緩存和表定義緩存
mysql> show variables like 'table_open_cache';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| table_open_cache | 16384 |
+------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'table_defi%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| table_definition_cache | 2000 |
+------------------------+-------+
1 row in set (0.00 sec)
mysql每打開一個表,都會讀入一些數據到table_open_cache 緩存 中,當mysql在這個緩存中找不到相應的信息時,才會去磁盤上直接讀取,所以該值要設置得足夠大以避免需要重新打開和重新解析表的定義,一般設置為max_connections的10倍,但最好保持在10000以內。
還有種依據就是根據狀態open_tables的值進行設置,如果發現open_tables的值每秒變化很大,那麼可能需要增大table_open_cache的值。
table_definition_cache 通常簡單設置為服務器中存在的表的數量,除非有上萬張表。
與MyISAM不同,InnoDB的open table和open file並無直接聯系,即打開frm表時其相應的ibd文件可能處於關閉狀態;
故InnoDB只會用到table_definiton_cache,不會使用table_open_cache;
其frm文件保存於table_definition_cache中,而idb則由innodb_open_files決定(前提是開啟了innodb_file_per_table),最好將innodb_open_files設置得足夠大,使得服務器可以保持所有的.ibd文件同時打開。
mysql> show global status like 'Max_used_connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 1785 |
+----------------------+-------+
1 row in set (0.00 sec)
我們設置的max_connections大小
mysql> show variables like 'max_connections%';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 4000 |
+-----------------+-------+
1 row in set (0.00 sec)
通常max_connections的大小應該設置為比Max_used_connections狀態值大,Max_used_connections狀態值反映服務器連接在某個時間段是否有尖峰,如果該值大於max_connections值,代表客戶端至少被拒絕了一次,可以簡單地設置為符合以下條件:Max_used_connections/max_connections=0.8
mysql> show global status like 'innodb_buffer_pool_read%';
+---------------------------------------+--------------+
| Variable_name | Value |
+---------------------------------------+--------------+
| Innodb_buffer_pool_read_ahead_rnd | 0 |
| Innodb_buffer_pool_read_ahead | 268720 | //預讀的頁數
| Innodb_buffer_pool_read_ahead_evicted | 0 |
| Innodb_buffer_pool_read_requests | 480291074970 | //從緩沖池中讀取的次數
| Innodb_buffer_pool_reads | 29912739 | //表示從物理磁盤讀取的頁數
+---------------------------------------+--------------+
5 rows in set (0.00 sec)
緩沖池命中率 = (Innodb_buffer_pool_read_requests)/(Innodb_buffer_pool_read_requests + Innodb_buffer_pool_read_ahead + Innodb_buffer_pool_reads)=99.994%
如果該值小於99.9%,建議就應該增大innodb_buffer_pool_size的值了,該值一般設置為內存總大小的75%-85%,或者計算出操作系統所需緩存+mysql每個連接所需的內存(例如排序緩沖和臨時表)+MyISAM鍵緩存,剩下的內存都給innodb_buffer_pool_size,不過也不宜設置太大,會造成內存的頻繁交換,預熱和關閉時間長等問題。
mysql> show global status like 'key_%';
+------------------------+-----------+
| Variable_name | Value |
+------------------------+-----------+
| Key_blocks_not_flushed | 0 |
| Key_blocks_unused | 106662 |
| Key_blocks_used | 107171 |
| Key_read_requests | 883825678 |
| Key_reads | 133294 |
| Key_write_requests | 217310758 |
| Key_writes | 2061054 |
+------------------------+-----------+
7 rows in set (0.00 sec)
mysql> show variables like '%key_cache_block_size%';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| key_cache_block_size | 1024 |
+----------------------+-------+
1 row in set (0.00 sec)
mysql> show variables like '%key_buffer_size%';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| key_buffer_size | 134217728 |
+-----------------+-----------+
1 row in set (0.00 sec)
緩沖區的使用率=1-(Key_blocks_unused*key_cache_block_size/ key_buffer_size)=18.6%
讀命中率=1-Key_reads /Key_read_requests=99.98%
寫命中率=1-Key_writes / Key_write_requests =99.05%
可看到緩沖區的使用率並不高,如果很長一段時間後還沒有使用完所有的鍵緩沖,可以考慮把緩沖區調小一點。
鍵緩存命中率可能意義不大,因為它和應用相關,有些應用在95%的命中率下就工作良好,有些則需要99.99%,所以從經驗上看,每秒的緩存未命中次數更重要,假設一個獨立磁盤每秒能做100個隨機讀,那麼每秒有5個緩沖未命中可能不會導致I/O繁忙,但每秒80個就可能出現問題。
每秒緩存未命中=Key_reads/uptime=0.33
mysql>
show global status like 'Created_tmp%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Created_tmp_disk_tables | 19226325 |
| Created_tmp_files | 117 |
| Created_tmp_tables | 56265812 |
+-------------------------+----------+
3 rows in set (0.00 sec)
mysql>
show variables like '%tmp_table_size%';
+----------------+----------+
| Variable_name | Value |
+----------------+----------+
| tmp_table_size | 67108864 |
+----------------+----------+
1 row in set (0.00 sec)
可看到總共創建了56265812 張臨時表,其中有19226325 張涉及到了磁盤IO,大概比例占到了0.34,證明數據庫應用中排序,join語句涉及的數據量太大,需要優化SQL或者增大tmp_table_size的值,我設的是64M。該比值應該控制在0.2以內。
mysql> show status like 'Binlog_cache%';
+-----------------------+----------+
| Variable_name | Value |
+-----------------------+----------+
| Binlog_cache_disk_use | 15 |
| Binlog_cache_use | 95978256 |
+-----------------------+----------+
2 rows in set (0.00 sec)
mysql> show variables like 'binlog_cache_size';
+-------------------+---------+
| Variable_name | Value |
+-------------------+---------+
| binlog_cache_size | 1048576 |
+-------------------+---------+
1 row in set (0.00 sec)
Binlog_cache_disk_use表示因為我們binlog_cache_size設計的內存不足導致緩存二進制日志用到了臨時文件的次數
Binlog_cache_use 表示 用binlog_cache_size緩存的次數
當對應的Binlog_cache_disk_use 值比較大的時候 我們可以考慮適當的調高 binlog_cache_size 對應的值
mysql> show variables like '%innodb_log_buffer_size%';
+------------------------+---------+
| Variable_name | Value |
+------------------------+---------+
| innodb_log_buffer_size | 8388608 |
+------------------------+---------+
1 row in set (0.00 sec)
mysql> show status like 'innodb_log_waits';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Innodb_log_waits | 0 |
+------------------+-------+
1 row in set (0.00 sec)
innodb_log_buffer_size我設置了8M,應該足夠大了;Innodb_log_waits表示因log buffer不足導致等待的次數,如果該值不為0,可以適當增大innodb_log_buffer_size的值。
mysql> show global status
like 'Handler_read%';
+-----------------------+--------------+
| Variable_name | Value |
+-----------------------+--------------+
| Handler_read_first | 19180695 |
| Handler_read_key | 30303690598 |
| Handler_read_last | 290721 |
| Handler_read_next | 51169834260 |
| Handler_read_prev | 1267528402 |
| Handler_read_rnd | 219230406 |
| Handler_read_rnd_next | 344713226172 |
+-----------------------+--------------+
7 rows in set (0.00 sec)
Handler_read_first:使用索引掃描的次數,該值大小說不清系統性能是好是壞
Handler_read_key:通過key進行查詢的次數,該值越大證明系統性能越好
Handler_read_prev:此選項表明在進行索引掃描時,按照索引倒序從數據文件裡取數據的次數,一般就是ORDER
BY ... DESC
Handler_read_rnd:該值越大證明系統中有大量的沒有使用索引進行排序的操作,或者join時沒有使用到index
Handler_read_rnd_next:使用數據文件進行掃描的次數,該值越大證明有大量的全表掃描,或者合理地創建索引,沒有很好地利用已經建立好的索引
mysql>
show global status like 'Innodb_buffer_pool_wait_free';
+------------------------------+-------+
| Variable_name | Value |
+------------------------------+-------+
| Innodb_buffer_pool_wait_free | 0 |
+------------------------------+-------+
1 row in set (0.00 sec)
該值不為0表示buffer pool沒有空閒的空間了,可能原因是innodb_buffer_pool_size設置太大,可以適當減少該值。
mysql> show global status like 'select_full_join';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Select_full_join | 10403 |
+------------------+-------+
1 row in set (0.00 sec)
該值表示在join操作中沒有使用到索引的次數,值很大說明join語句寫得很有問題
mysql> show global status like 'select_range';
+---------------+----------+
| Variable_name | Value |
+---------------+----------+
| Select_range | 22450380 |
+---------------+----------+
1 row in set (0.00 sec)
該值表示第一個表使用ranges的join數量,該值很大說明join寫得沒有問題,通常可查看select_full_join和select_range的比值來判斷系統中join語句的性能情況
mysql>
show global status like 'Select_range_check';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| Select_range_check | 0 |
+--------------------+-------+
1 row in set (0.00 sec)
如果該值不為0需要檢查表的索引是否合理
mysql>
show global status like 'Slow_queries';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Slow_queries | 114111 |
+---------------+--------+
1 row in set (0.00 sec)
該值表示mysql啟動以來的慢查詢個數,即執行時間超過long_query_time的次數,可根據Slow_queries/uptime的比值判斷單位時間內的慢查詢個數,進而判斷系統的性能。
mysql>
show global status like 'table_lock%';
+-----------------------+------------+
| Variable_name | Value |
+-----------------------+------------+
| Table_locks_immediate | 1644917567 |
| Table_locks_waited | 53 |
+-----------------------+------------+
2 rows in set (0.00 sec)
這兩個值的比值:Table_locks_waited /Table_locks_immediate 趨向於0,如果值比較大則表示系統的鎖阻塞情況比較嚴重