MySql 的批量操作,要加rewriteBatchedStatements參數































在測試時,發現三十萬的數據居然需要十分鐘左右的時間。首先想到的就是Mysql的相關配置是不是有問題,反復修改了Mysql的相應配置參數,收效甚微。
在Mysql的官網上查到如下:
http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
關于rewriteBatchedStatements參數,Mysql官方的說明:
Should the driver use multiqueries (irregardless of the setting of "allowMultiQueries") as well as rewriting of prepared statements for INSERT into multi-value inserts when executeBatch() is called? Notice that this has the potential for SQL injection if using plain java.sql.Statements and your code doesn't sanitize input correctly. Notice that for prepared statements, server-side prepared statements can not currently take advantage of this rewrite option, and that if you don't specify stream lengths when using PreparedStatement.set*Stream(), the driver won't be able to determine the optimum number of parameters per batch and you might receive an error from the driver that the resultant packet is too large. Statement.getGeneratedKeys() for these rewritten statements only works when the entire batch includes INSERT statements.
解決辦法:
下載最新的JDBC的驅動程序。
MYSQL URL的配置參數如下:
jdbc:mysql://54.200.190.80:3306/ccb_ucstar?rewriteBatchedStatements=true
經過測試。三十多萬的數據。70秒內搞定!
posted on 2013-03-13 20:38 MikyTan 閱讀(2797) 評論(0) 編輯 收藏 所屬分類: Mysql