從MySQL的源碼剖析Innodb buffer的命中率計(jì)算
按官方手冊(cè)推薦Innodb buffer Hit Ratios的計(jì)算是:
100-((iReads / iReadRequests)*100) iReads : mysql->status->Innodb_buffer_pool_reads iReadRequests: mysql->status->Innodb_buffer_pool_read_requests
出處: http://dev.mysql.com/doc/mysql-monitor/2.0/en/mem_graphref.html
搜”Hit Ratios”
推薦有興趣的同學(xué)把這個(gè)頁面都看一下應(yīng)該也會(huì)有很大收獲.
另外在hackmysql: www.hackmysql.com網(wǎng)站上的: mysqlsqlreport中關(guān)于buffer命中計(jì)算是:
$ib_bp_read_ratio = sprintf "%.2f",
($stats{'Innodb_buffer_pool_read_requests'} ?
100 - ($stats{'Innodb_buffer_pool_reads'} /
$stats{'Innodb_buffer_pool_read_requests'}) * 100 :0);
即:
ib_bp_hit=100-(Innodb_buffer_pool_reads/Innodb_buffer_pool_read_requests)*100
另外我們知道查看Innodb Buffer Hit Ratios的地方是:
Buffer pool hit rate : XXXX/1000;
那個(gè)XXX/1000即是buffer pool hit ratios的命中.
這樣也可以從代碼里看一下這個(gè)bp命中計(jì)算:
storage/innobase/buf/buf0buf.c # void buf_print_io storage/innodbase/include/buf0buf.h #struct buf_block_struct
在buf0buf.c 中的buf_print_io函數(shù)中可以看到:
void
buf_print_io(
…
if (buf_pool->n_page_gets > buf_pool->n_page_gets_old) {
fprintf(file, "Buffer pool hit rate %lu / 1000\n",
(ulong)
(1000 - ((1000 * (buf_pool->n_pages_read
- buf_pool->n_pages_read_old))
/ (buf_pool->n_page_gets
- buf_pool->n_page_gets_old))));
} else {
fputs("No buffer pool page gets since the last printout\n",
file);
}
buf_pool->n_page_gets_old = buf_pool->n_page_gets;
buf_pool->n_pages_read_old = buf_pool->n_pages_read;
…
}
結(jié)合:
storage\innobase\include\buf0buf.h中
struct buf_block_struct{
…
ulint n_pages_read; /* number read operations */
…
ulint n_page_gets; /* number of page gets performed;
also successful searches through
the adaptive hash index are
counted as page gets; this field
is NOT protected by the buffer
pool mutex */
…
ulint n_page_gets_old;/* n_page_gets when buf_print was
last time called: used to calculate
hit rate */
…
ulint n_pages_read_old;/* n_pages_read when buf_print was
last time called */
…
從這個(gè)來看innodb buffer hit Ratios的命中計(jì)算需要本次取的值和上次值做一個(gè)減法公式應(yīng)該為
ib_bp_hit=1000 – (t2.iReads – t1.iReads)/(t2.iReadRequest – t1.iReadRequest)*1000
t(n): 時(shí)間點(diǎn) 兩個(gè)時(shí)間間隔最少是30秒以上,在小意義不大.
iReads: Innodb_buffer_pool_reads iReadRequest: Innodb_buffer_pool_read_requests
對(duì)innodb的輸出參數(shù)有興趣的可以關(guān)注: storage/innobase/buf/Srv0srv.c 中的:
void srv_export_innodb_status()
思考:
對(duì)于innodb_buffer_pool_read_requests, innodb_buffer_pool_reads這種累加值,當(dāng)很大時(shí)進(jìn)行: innodb_buffer_pool_reads/innodb_buffer_pool_read_requests 相來講只能得到從開始到現(xiàn)在的命中率的表現(xiàn)了. 如果想得到現(xiàn)在近五分鐘,近一分鐘或是8點(diǎn)到9點(diǎn)每分鐘的命中率情況,如果還是按著innodb_buffer_pool_reads/innodb_buffer_pool_read_requests 進(jìn)行計(jì)算,只能得到mysqld開起累計(jì)在8點(diǎn)-9點(diǎn)的每分鐘的累計(jì)平均命中情況.
所以如果想到每(五)分鐘的命中情況,就需要本次取得的值和一(五)分鐘前的值進(jìn)行相減,然后進(jìn)行運(yùn)算.這樣才能得到一個(gè)當(dāng)下的bp命中情況.
兩種方法沒實(shí)質(zhì)的對(duì)錯(cuò)的問題,但相對(duì)于源碼中的那種計(jì)算方式更容讓發(fā)現(xiàn)數(shù)據(jù)庫的抖動(dòng)問題.
能解決的問題:
偶而的數(shù)據(jù)庫性能抖動(dòng)能直觀的反應(yīng)出來.
相關(guān)文章
MySql使用skip-name-resolve解決外網(wǎng)鏈接客戶端過慢問題
在騰訊云上面搭建的mysql使用開發(fā)的電腦上navicat進(jìn)行訪問時(shí)總是特別的慢,原來是Mysql會(huì)對(duì)請(qǐng)求的地址進(jìn)行域名解析,開發(fā)的電腦并沒有域名,所以會(huì)導(dǎo)致特別的慢,下面通過本文給大家分享MySql使用skip-name-resolve解決外網(wǎng)鏈接客戶端過慢問題2017-07-07
mysql數(shù)據(jù)庫如何轉(zhuǎn)移到oracle
這篇文章主要介紹了mysql數(shù)據(jù)庫如何轉(zhuǎn)移到oracle,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
解決mysql報(bào)錯(cuò)ERROR 1049 (42000): Unknown dat
對(duì)于錯(cuò)誤代碼1049(42000):Unknown database ‘?dāng)?shù)據(jù)庫‘,這個(gè)錯(cuò)誤通常表示您正在嘗試訪問一個(gè)不存在的數(shù)據(jù)庫,本文給出了解決方法,您可以按照文中步驟進(jìn)行操作,需要的朋友可以參考下2024-01-01
mysql字符串的‘123’轉(zhuǎn)換為數(shù)字的123的實(shí)例
下面小編就為大家?guī)硪黄猰ysql字符串的‘123’轉(zhuǎn)換為數(shù)字的123的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
mybatis+mysql 使用存儲(chǔ)過程生成流水號(hào)的實(shí)現(xiàn)代碼
這篇文章主要介紹了mybatis+mysql 使用存儲(chǔ)過程生成流水號(hào)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-01-01
MySQL Community Server 8.0.11安裝配置方法圖文教程
這篇文章主要為大家詳細(xì) 介紹了MySQL Community Server 8.0.11安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
MySQL派生表聯(lián)表查詢實(shí)戰(zhàn)過程
派生表是查詢結(jié)果組成的虛擬表,派生表是在外部查詢的FROM子句中定義的,不需要手動(dòng)創(chuàng)建,下面這篇文章主要給大家介紹了關(guān)于MySQL派生表聯(lián)表查詢的相關(guān)資料,需要的朋友可以參考下2022-03-03
mysql設(shè)置指定ip遠(yuǎn)程訪問連接實(shí)例
這篇文章主要介紹了mysql設(shè)置指定ip遠(yuǎn)程訪問連接的方法,分別實(shí)例講述了從任意主機(jī)和指定ip訪問遠(yuǎn)程MySQL數(shù)據(jù)庫的方法,代碼簡單功能實(shí)用,需要的朋友可以參考下2014-10-10

