I have a loop making the sequential insert of millions of rows into a single MySql table.
I wonder if it's possible to parallelize the insert or use some feature that increases insert performance.
Code:
public static java.sql.Connection getConexaoMySQL() {
//atributo do tipo Connection
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String serverName = "localhost";
String mydatabase ="tweets";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; String username = "root";
String password = "admin";
connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
status = ("Banco de Dados--->Conectado com sucesso!");
}
else {
status = ("Banco de Dados--->Não foi possivel realizar conexão");
}
return connection;
}
catch (ClassNotFoundException e)
{
System.out.println("O driver expecificado nao foi encontrado.");
return null;
} catch (SQLException e) {
System.out.println("Nao foi possivel conectar ao Banco de Dados.");
return null;
}
}
public static void insert(List<TweetsDB> list){
for (TweetsDB x : list) {
preparedStmt.setString (1, x.getCandidate);
preparedStmt.setString (2, x.getIDTweet);
preparedStmt.setString (3, x.getIDUser);
preparedStmt.setString (4, x.getUserScreenName);
preparedStmt.setString (5, x.getUserName);
preparedStmt.setString (6, x.getRetweets);
preparedStmt.setTimestamp(7, x.getDate);
preparedStmt.setString (8, x.getText);
preparedStmt.setString (9, x.getHashtags);
// execute the preparedstatement
preparedStmt.execute();
}
}