提升mysql性能的關鍵參數之innodb_buffer_pool_size、innodb_buffer_pool_instances。
首先我們先看一下,我的電腦(i3-3220,4G記憶體,沒有配置innodb_buffer_pool_size參數的情況下,抛開程式的性能,經過1個線程-8個線程的測試結果)環境下,大家可以看到如下的圖,線程并發情況下,時間不斷的攀升,這讓人好苦惱,說好的高性能,高并發呢?
結果:

不過經過一個多星期的努力,不經意間,我在同僚的電腦上發現,同樣的代碼測試後,時間和我的不是一個等量級的,要比我的電腦快很多,他的電腦(其他條件等同,CPU為i3-4150),8個商品8000挂單下能達到135ms,首先說明CPU的提升對程式處理來說是非常有效果的,但是依然在高并發的情況下,表現的很差勁。
時至今日,通過比對mysql的配置檔案my.ini,不經意間,我們發現innodb_buffer_pool_size在8M和48M的配置下,性能簡直就不在一個等級上,OK,那就繼續調查吧,最終得出如下結論
innodb_buffer_pool_size=2G
innodb_buffer_pool_instances=1
詳細的解釋,可以看如下介紹
# InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
# row data. The bigger you set this the less disk I/O is needed to
# access data in tables. On a dedicated database server you may set this
# parameter up to 80% of the machine physical memory size. Do not set it
# too large, though, because competition of the physical memory may
# cause paging in the operating system. Note that on 32bit systems you
# might be limited to 2-3.5G of user level memory per process, so do not
# set it too high.
簡單來說,就是pool-size可以緩存索引和行資料,值越大,IO讀寫就越少,如果單純的做資料庫服務,該參數可以設定到電腦實體記憶體的80%,哇,這個參數簡直太棒了。緊接着,我們經過大量的測試發現,在 innodb_buffer_pool_size=2G和innodb_buffer_pool_instances=1 的參數設定情況下,效果最優。
OK上圖
發生了什麼事請,該參數下,20個商品,各下1000單,平均時間在55ms,别逗我了,mysql,你怎麼不早一點告訴我。
是以,推薦如下,mysql在高并發讀寫情況下,并且為innodb資料表時,請設定