3
I have a loop doing millions of rows sequential Insert in a single Mysql table.
I would like to know if it is possible to parallelize Insert or use some resource that increases insertion 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();
}
}
I am without Mysql here to test and generate a detailed answer for you, but some alternatives are: use
INSERT... ON DUPLICATE KEY UPDATE
;LOAD DATA INFILE
(data from a file, for example. It is thebulk copy
); andaddBatch
, running it later. It necessarily needs to be using JDBC/JPA or you can call on the command line, for example?– Bruno César
As Bruno commented, I usually solve these problems (especially when it comes to taking data through spreadsheets/flat file, for example) using
LOCAL DATA INFILE
.– Fellipe Soares
I am just reading from a . csv file and writing to the table. I am also trying to import using LOCAL DATA INFILE or by Workbench. But I am not able to import the data in each column. Example of . csv: https://goo.gl/Fa3Fij
– Roger Rubens