MySQL 10w+數(shù)據(jù) insert 優(yōu)化

由于業(yè)務(wù)原因,遇到了如題所述的業(yè)務(wù)問題,事務(wù)執(zhí)行時(shí)間在30s~50s 不等,效果非常不理想

方案1. jdbc批處理

5w+ 數(shù)據(jù)測試,分別使用了mybatis insert()()(拼接xml), mybatis的批處理和 jdbc的批處理。

可以看到在jdbc執(zhí)行時(shí)間方面是差不多的,但是在方法執(zhí)行時(shí)間上,批處理要稍微快了一些,但是還是不理想

5w+ 數(shù)據(jù)測試1

5w+ 數(shù)據(jù)測試2

方案2. 優(yōu)化MySQL 參數(shù)

修改 my.ini

innodb_buffer_pool_size :

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.

Innodb的緩沖池會(huì)緩存數(shù)據(jù)和索引,設(shè)置的越大訪問表中的數(shù)據(jù)所需的磁盤I/O就越少。

修改innodb_buffer_pool_size = 512M測試一下效率,這速度簡直感人!

10w+數(shù)據(jù)測試1

innodb_log_buffer_size :

The size of the buffer InnoDB uses for buffering log data. As soon as

it is full, InnoDB will have to flush it to disk. As it is flushed once per second anyway, it does not make sense to have it very large

(even with long transactions).

表示InnoDB寫入到磁盤上的日志文件時(shí)使用的緩沖區(qū)的字節(jié)數(shù),默認(rèn)值為8M。當(dāng)緩沖區(qū)充滿時(shí),InnoDB將刷新數(shù)據(jù)到磁盤。由于它每秒刷新一次,所以將它設(shè)置得非常大是沒有意義的 (即使是長事務(wù))。

innodb_log_file_size :

Size of each log file in a log group. You should set the combined size

of log files to about 25%-100% of your buffer pool size to avoid

unneeded buffer pool flush activity on log file overwrite. However,

note that a larger logfile size will increase the time needed for the

recovery process.

該值越大,緩沖池中必要的檢查點(diǎn)刷新活動(dòng)就會(huì)越少,節(jié)省磁盤I/ O。但是越大的日志文件,mysql的崩潰恢復(fù)就越慢

設(shè)置上述兩個(gè)參數(shù)innodb_log_file_size=64M innodb_log_buffer_size=16M,效率提升的并不明顯。

10w+數(shù)據(jù)測試2

總結(jié)

數(shù)據(jù)量大時(shí),批處理在方法執(zhí)行時(shí)間上要比 mybatis xml拼接快一點(diǎn) (批處理只編譯一條SQL,而拼接的方式SQL會(huì)很長)

性能瓶頸優(yōu)化還是要從數(shù)據(jù)庫下手,目前來看MySQL 大數(shù)據(jù)量時(shí)很依賴 innodb_buffer_pool_size (緩沖池)

參考

https://my.oschina.net/realfighter/blog/368225

擴(kuò)展閱讀

教你88秒插入1000萬條數(shù)據(jù)到mysql數(shù)據(jù)庫表

MySQL每秒57萬的寫入,帶你飛~

在一個(gè)千萬級(jí)的數(shù)據(jù)庫查尋中,如何提高查詢效率?

設(shè)計(jì)抗住千萬級(jí)流量的架構(gòu)思路

6月份Github上最熱門的Java開源項(xiàng)目!

作者:殷天文

來源:http://www.itdecent.cn/p/09689e6cc2d6

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容