作者:殷天文

jianshu.com/p/09689e6cc

由於業務原因,遇到了如題所述的業務問題,事務執行時間在30s~50s 不等,效果非常不理想

方案1. jdbc批處理

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

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

5w+ 數據測試1
5w+ 數據測試2

方案2. 優化MySQL 參數

修改 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 toaccess data in tables. On a dedicated database server you may set thisparameter up to 80% of the machine physical memory size. Do not set ittoo large, though, because competition of the physical memory maycause paging in the operating system. Note that on 32bit systems youmight be limited to 2-3.5G of user level memory per process, so do not

set it too high.

Innodb的緩衝池會緩存數據和索引,設置的越大訪問表中的數據所需的磁碟I/O就越少。

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

10w+數據測試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寫入到磁碟上的日誌文件時使用的緩衝區的位元組數,默認值為8M。當緩衝區充滿時,InnoDB將刷新數據到磁碟。由於它每秒刷新一次,所以將它設置得非常大是沒有意義的 (即使是長事務)。

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 therecovery process.

該值越大,緩衝池中必要的檢查點刷新活動就會越少,節省磁碟I/ O。但是越大的日誌文件,mysql的崩潰恢復就越慢

設置上述兩個參數innodb_log_file_size=64M innodb_log_buffer_size=16M,效率提升的並不明顯。

10w+數據測試2

總結

數據量大時,批處理在方法執行時間上要比 mybatis xml拼接快一點 (批處理只編譯一條SQL,而拼接的方式SQL會很長)

性能瓶頸優化還是要從資料庫下手,目前來看MySQL 大數據量時很依賴 innodb_buffer_pool_size (緩衝池)

參考

my.oschina.net/realfigh

推薦閱讀(點擊可跳轉)

1. Linux最常用命令!簡單易學,能解決95%以上的問題!

2. 擼一段 SQL ? 還是擼一段代碼?

3. 幾個高逼格 Linux 命令!

4. 有助於改善性能的Java代碼技巧

看到這裡了,就關注一個唄

推薦閱讀:

相關文章