I'm inserting a few million of rows into a MySql table. I'm using prepared statement as shown below.
Would creating a single insert string like the one directly below be expected to be substantially faster?
Single string approach from Is 22 seconds a good time for inserting 500 rows in mysql? :
INSERT INTO example
(example_id, name, value, other_value)
VALUES
(100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2'),
(102, 'Name 3', 'Value 3', 'Other 3'),
(103, 'Name 4', 'Value 4', 'Other 4');
What I'm currently doing:
//
// method to do upload
//
public static void doUpload(Connection conn) {
log.info("Deleting existing data...");
Database.update("truncate table attribute", conn);
log.info("Doing inserts");
String sqlString = "insert into attribute values (null,?,?,?)";
int max = 1000000;
PreparedStatement ps = Database.getPreparedStatement(sqlString, conn);
for (int i = 0; i < max; i++) {
// add params
String subjectId = i+"";
addParam(subjectId, "GENDER", getGender(), ps);
addParam(subjectId, "AGE", getAge(), ps);
addParam(subjectId, "CITY", getCity(), ps);
addParam(subjectId, "FAVORITE_COLOR", getColor(), ps);
addParam(subjectId, "PET", getPet(), ps);
if (i % 1000 == 0) {
log.info("Executing " + i + " of " + max);
Database.execute(ps);
log.info("Done with batch update");
ps = Database.getPreparedStatement(sqlString, conn);
}
}
if (Database.isClosed(ps) == false) {
Database.execute(ps);
}
}
//
// method to add param to the prepared statement
//
private static void addParam(String subjectId, String name, String val, PreparedStatement ps) {
ArrayList<String> params;
params = new ArrayList<String>();
params.add(subjectId + "");
params.add(name);
params.add(val);
Database.addToBatch(params, ps);
}
//
// addToBatch
//
public static void addToBatch(List<String> params, PreparedStatement ps) {
try {
for (int i = 0; i < params.size(); i++) {
ps.setString((i + 1), params.get(i));
}
ps.addBatch();
} catch (Exception exp) {
throw new RuntimeException(exp);
}
}
What is the fastest way to do this type of insert?
I'm currently inserting 1000 rows in about 5 seconds. Is it reasonable to expect much better than this? I'm running locally and have already dropped all indexes on the table I'm inserting into.