InnoDB是MySQL數據庫發展至今一款相當重要的數據庫存儲引擎,其不只支持事務特性,而且具備豐富的統計信息,便於數據庫管理人員瞭解最近InnoDB存儲引擎的運行狀態。node
早期版本的InnoDB存儲引擎經過在內部新建一張名爲innodb_monitor的表進行實現,InnoDB內部檢測到該表後,會定時輸出InnoDB引擎相關統計信息。而如今的InnoDB存儲引擎經過命令show engine innodb status實現對InnoDB存儲引擎統計信息的查看,在MySQL客戶端執行show engine innodb status\G,獲取到的結果可能以下:mysql
=====================================sql
140708 21:35:01 INNODB MONITOR OUTPUT數據庫
=====================================緩存
Per second averages calculated from the last 54 seconds併發
-----------------dom
BACKGROUND THREAD異步
-----------------ide
srv_master_thread loops: 129 1_second, 128 sleeps, 8 10_second, 76 background, 76 flushoop
srv_master_thread log flush and writes: 129
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 53606, signal count 55906
Mutex spin waits 525951, rounds 2435436, OS waits 40130
RW-shared spins 36905, rounds 444653, OS waits 9660
RW-excl spins 5298, rounds 156489, OS waits 2307
Spin rounds per wait: 4.63 mutex, 12.05 RW-shared, 29.54 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 19FFF
Purge done for trx's n:o < 13696 undo n:o < 0
History list length 15274
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 19F65, not started
mysql tables in use 1, locked 0
MySQL thread id 52, OS thread handle 0x7f8bfd1b9700, query id 1040279 localhost root
SELECT c FROM sbtest1 WHERE id=199077
………………... any other TRANSACTIONS ……………………..
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
………………. other three read threads …………….….
I/O thread 6 state: waiting for completed aio requests (write thread)
………………. other three write threads …………….….
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 1; buffer pool: 0; flash cache 0
296 OS file reads, 47547 OS file writes, 40960 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 755.62 writes/s, 750.19 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 21249841, node heap has 294 buffer(s)
17860.30 hash searches/s, 13956.39 non-hash searches/s
---
LOG
---
Log sequence number 184703155
Log flushed up to 184699426
Last checkpoint at 109334220
1 pending log writes, 0 pending chkp writes
40703 log i/o's done, 749.84 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 10994319360; in additional pool allocated 0
Dictionary memory allocated 43910
Buffer pool size 655359
Free buffers 646841
Database pages 8224
Old database pages 3015
Modified db pages 5270
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 1, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 149, created 8075, written 6557
0.00 reads/s, 38.13 creates/s, 5.52 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 8224, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
Async Flush: 0, Sync Flush: 0, LRU List Flush: 0, Flush List Flush: 6557
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
61 read views open inside InnoDB
Main thread process no. 30423, id 140238424880896, state: sleeping
Number of rows inserted 451965, updated 103933, deleted 51957, read 21009001
962.26 inserts/s, 1924.65 updates/s, 962.15 deletes/s, 381640.41 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
根據上面的顯示,能夠把InnoDB存儲引擎的統計信息分爲:
1. 基礎信息
2. 後臺線程(background thread)
3. 信號量(semaphores)
4. 事務(transactions)
5. I/O
6. insert buffer & adaptive hash
7. log
8. buffer pool & memory
9. row operations
下面結合上節中顯示的信息分別對以上分類進行說明。
Per second averages calculated from the last 54 seconds
InnoDB內部統計的信息都是一段時間內的平均值,默認狀況下爲60s一個時間間隔計算,上述顯示的54s是執行show engine innodb status命令距上次統計結束爲54s,下面顯示的信息爲54s內的平均值,通常認爲,該值在比較大時纔有意義,由於當該值比較小時,偶然的波動會形成統計信息變化很大,不能很好地反應InnoDB存儲引擎內部實際狀況。
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 129 1_second, 128 sleeps, 8 10_second, 76 background, 76 flush
srv_master_thread log flush and writes: 129
MySQL內部後臺有個線程負責數據的刷盤和undo的清理(不使用獨立的purge線程)工做,後臺線程是一個死循環,根據InnoDB存儲引擎的繁忙程度執行數據刷盤操做,主要操做以下圖:
從background thread信息中,能夠看出1s的循環有129次,10s的循環有8次,128次sleep和76次進入background模式,進行了76次數據刷盤和129第二天志刷盤,整個background thread信息顯示,InnoDB內部處於相對空閒狀態。
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 53606, signal count 55906
Mutex spin waits 525951, rounds 2435436, OS waits 40130
RW-shared spins 36905, rounds 444653, OS waits 9660
RW-excl spins 5298, rounds 156489, OS waits 2307
Spin rounds per wait: 4.63 mutex, 12.05 RW-shared, 29.54 RW-excl
信號量段顯示了InnoDB存儲引擎內部鎖信息,在大併發量請求時,InnoDB不可避免存在資源競爭,這個時候,部分事務須要經過鎖等待,等待相關事務釋放資源後才能繼續執行,上面顯示的餓信息就是InnoDB內部鎖等待信息。除了上面部分信息外,若是InnoDB存儲引擎存在死鎖狀態,還會在以上信息下面增長相應的的死鎖信息。
transactions
------------
TRANSACTIONS
------------
Trx id counter 19FFF
Purge done for trx's n:o < 13696 undo n:o < 0
History list length 15274
事務段信息顯示當前InnoDB內部正在執行的事務,由於InnoDB存儲引擎支持事務併發執行,因此InnoDB內部同時存在許多事務。
事務id在InnoDB內部是遞增的,從當前事務信息看,下一個事務id爲0x19FFF,而當前purge到的undo日誌號no爲13695,當前等待purge操做的事務有15274個,單從這些信息上就能夠看出,當前InnoDB內部處於繁忙狀態,等待pruge的事務數在不斷增長。
上面一部分能夠看作是事務的總結信息,而LIST OF TRANSACTIONS FOR EACH SESSION:後面顯示的是當前InnoDB內部每一個事務的具體狀態:
---TRANSACTION 19F65, not started
mysql tables in use 1, locked 0
MySQL thread id 52, OS thread handle 0x7f8bfd1b9700, query id 1040279 localhost root
SELECT c FROM sbtest1 WHERE id=199077
上述顯示了當前InnoDB內部1個事務的信息,not started說明該事務處於非活動狀態,若是是活動狀態則爲active,mysql tables in use說明當前事務操做的表數,locked表示表被鎖的個數(innodb內部支持行鎖,因此通常表鎖使用比較少,只有在如alter table等操做是纔會鎖表),mysql thread id爲當前鏈接分配的id,os thread handle爲鏈接線程的句柄,query id爲當前查詢的id,後面顯示了具體的查詢SQL。
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
………………. other three read threads …………….….
I/O thread 6 state: waiting for completed aio requests (write thread)
………………. other three write threads …………….….
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 1; buffer pool: 0; flash cache 0
296 OS file reads, 47547 OS file writes, 40960 OS fsyncs
reads/s, 0 avg bytes/read, 755.62 writes/s, 750.19 fsyncs/s
I/O段顯示當前InnoDB引擎內部I/O使用狀況,InnoDB內部採用異步I/O的模式進行數據的讀寫操做,默認狀況下有1個insert buffer線程,1個日誌線程,4個讀數據線程和4個寫數據線程。
線程信息後面部分爲I/O統計信息,總共進行了296次讀取,47547次寫入,fsync爲40960次,平均讀數據爲0,寫輸入爲755.62次/s, fsyncs爲750.19次/s,從以上信息分析,該InnoDB存儲引擎內部事務基本上都是寫入事務,I/O基本已經達到性能瓶頸。
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
上面部分顯示插入緩衝信息,從MySQL 5.5開始,insert buffer不止統計insert信息還包括delete和update信息,被稱之爲change buffer(delete mark爲update的信息,InnoDB內部update經過把記錄的delete flag設置成true實現)。
單從上面的信息來看,change buffer沒有涉及到須要合併的insert/update/delete操做,insert buffer功能沒能獲得利用。
Hash table size 21249841, node heap has 294 buffer(s)
17860.30 hash searches/s, 13956.39 non-hash searches/s
InnoDB內部對已某張頁查詢達到必定數量後,內部會爲該頁採用哈希方式創建索引,InnoDB認爲,當某張頁在一段時間被頻繁使用時,那麼在後面被用到的機率也將很是高,因此爲該頁創建哈希索引能加速該頁的查找。hash table size顯示了內部自適應哈希表的大小,hash search和non-hash search分別顯示了InnoDB內部查找時使用自適應哈希和普通索引查找每秒的次數,單從上述信息看來自適應哈希查找佔到了55%左右,自適應哈希效果明顯。
---
LOG
---
Log sequence number 184703155
Log flushed up to 184699426
Last checkpoint at 109334220
1 pending log writes, 0 pending chkp writes
40703 log i/o's done, 749.84 log i/o's/second
InnoDB log信息顯示了當前內部重作日誌(redo log)的寫入狀況,InnoDB內部經過LSN來實現redo log和事務的對應,log sequence number說明當前系統內部事務執行LSN爲184703155,而log flushed up to表示redo log已經保存到磁盤的LSN爲184699426,而last checkpoint at表示最近數據和redo log都保存到磁盤的LSN爲109334220。
經過上面的信息能夠得知:若是這個時候發生某種意外致使MySQL意外宕機,LSN > 184699426的事務可能會被丟失,而LSN在109334220和184699426中間的事務須要經過redo log恢復。LSN < 109334220的數據,由於已經保存到磁盤了,因此不會由於宕機而丟失。
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 10994319360; in additional pool allocated 0
Dictionary memory allocated 43910
Buffer pool size 655359
Free buffers 646841
Database pages 8224
Old database pages 3015
Modified db pages 5270
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 1, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 149, created 8075, written 6557
0.00 reads/s, 38.13 creates/s, 5.52 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 8224, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
Async Flush: 0, Sync Flush: 0, LRU List Flush: 0, Flush List Flush: 6557
InnoDB內部採用buffer pool的形式緩存數據,對於InnoDB存儲引擎來講,buffer pool越大,內存中緩存的數據也就越多,系統的性能就越好。上述信息顯示,InnoDB佔用內存大約10G,
buffer的大小大約爲655359(單位16K,下同),而空閒大約爲646841,被使用的頁爲8224 + 3015,其中髒頁爲8224, 3015爲非髒頁。
從上述信息顯示,InnoDB內存過大,數據所有落在內存中。
Buffer pool hit rate 1000 / 1000內存命中率100%,等待flush的頁爲6557
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
61 read views open inside InnoDB
Main thread process no. 30423, id 140238424880896, state: sleeping
Number of rows inserted 451965, updated 103933, deleted 51957, read 21009001
962.26 inserts/s, 1924.65 updates/s, 962.15 deletes/s, 381640.41 reads/s
row operations顯示了InnoDB存儲引擎內部記錄操做信息,打開了61個read views,後面顯示了主線程信息和狀態,最後顯示了自啓動以來,全部的insert/update/delete/read數量和每秒的平均值。