2012-11-12 27 views
1

我有下面的代碼在一個MySQL表中插入行:MySQL的prepareStatement批緩慢

String sqlInsert = "INSERT INTO test_perf_table (id, name) VALUES (?, ?);"; 
PreparedStatement sqlInsertSt = connection.prepareStatement(sqlInsert); 

for (int i = 0; i < SETSIZE; i++) { 
    sqlInsertSt.setInt(1, ids[i]); 
    sqlInsertSt.setString(2, names[i]); 
    sqlInsertSt.addBatch(); 
} 
int[] updateCounts = sqlInsertSt.executeBatch(); 

的問題是,我只是存檔每秒21個交易這是非常低的,本來期待的要快10倍。

所以我的問題是,我的代碼是否需要改進或可能是數據庫配置問題?

MySQL服務器在Win8中運行@localhost,使用my-large.ini默認配置,使用mysql-connector-java-5.1.13驅動程序。

不知道,但我認爲引擎是MyISAM的,因爲我沒有指定任何

編輯:

CREATE TABLE test_perf_table (
    id INT, 
    name VARCHAR(20) 
); 
+0

「SHOW CREATE TABLE」如何顯示,這可能會顯示一些重要的細節。另外,是不是你的'for'循環每次都會覆蓋參數? –

+0

沒有每個循環向批處理添加一個語句,如果我沒有錯 – fxe

回答

2

我剛剛處理了這家最近。

最大的不同之處在於將參數rewriteBatchedStatements=true添加到您的jdbc連接url中。

您也可以通過在事務中執行executeBatch()來看到性能改進。

+0

性能增加相當多高達50k TPS!謝謝 – fxe

+0

非常好。那只是用rewriteBatchedStatements,還是你需要事務來獲得這樣的性能? – GreyBeardedGeek

+0

在我原來的代碼我忘了setAutoCommit(false)但是我只能存檔5k TPS 我需要至少20k TPS做多批次平均每批10k,rewriteBatchedStatements增加到50k TPS – fxe