Skip to content

Instantly share code, notes, and snippets.

@hishidama
Created June 23, 2012 01:50
Show Gist options
  • Save hishidama/2976200 to your computer and use it in GitHub Desktop.
Save hishidama/2976200 to your computer and use it in GitHub Desktop.
Oracle JDBC setTimestampとsetStringの実行時間比較
/* Oracle 11.2.0.1.0 */
create table table3(
time1 timestamp
);
http://blog.goo.ne.jp/hishidama/e/9eb71db638ddecb95d7a61f68726ad2b
Oracle版SqoopのOraOopでTIMESTAMPやDATE型をStringで扱っていたので、java.sql.Timestampで扱うのとどちらが速いのか計ってみた。
A:st.setTimestamp(t)
B:st.setString(s)
C:st.setTimestamp(Timestamp.valueOf(s))
D:st.setString(s)(SQL上でto_timestamp(?, '書式'))
Aが最も速く、Bは遅かった。
文字列からTimestampに変換するなら、BやDよりもCの方が速い。
(Javaプログラムを動かすマシンよりもDBサーバーの方がすごく高性能なら、Dの方が速くなるかも?)
INSERT文のaddBatch 100回につきexecuteBatchを1回、executeBatch 100回につきコミットを1回
A:2799ミリ秒
B:3881ミリ秒
C:3501ミリ秒
D:3707ミリ秒
INSERT文のaddBatch 1000回につきexecuteBatchを1回、executeBatch 1回につきコミットを1回
A:2114ミリ秒
B:3160ミリ秒
C:2430ミリ秒
D:3108ミリ秒
INSERT文のaddBatch 10000回につきexecuteBatchを1回、executeBatch 10回につきコミットを1回
A:1581ミリ秒
B:2196ミリ秒
C:1716ミリ秒
D:2161ミリ秒
package example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
public class InsertTable3 {
public static void main(String[] args) throws SQLException,
InterruptedException {
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String usr = "hishidama";
String pwd = "hishidama";
Connection conn = DriverManager.getConnection(url, usr, pwd);
try {
conn.setAutoCommit(false);
new ExecTimestamp().execute(conn);
new ExecString().execute(conn);
new ExecStringToTime().execute(conn);
new ExecString2().execute(conn);
} finally {
conn.close();
}
}
static abstract class Exec {
private String name;
protected Exec(String name) {
this.name = name;
}
public void execute(Connection conn) throws SQLException,
InterruptedException {
String sql = createSql();
PreparedStatement st = conn.prepareStatement(sql);
for (int i = 0; i < 10; i++) {
truncate(conn);
System.gc();
Thread.sleep(1000);
loop(conn, st);
}
}
void truncate(Connection conn) throws SQLException {
Statement st = conn.createStatement();
try {
st.executeUpdate("truncate table table3");
} finally {
st.close();
}
}
protected String createSql() {
return "insert into table3 values(?)";
}
void loop(Connection conn, PreparedStatement st) throws SQLException {
long s = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
setPrepared(st, i);
st.addBatch();
if (((i + 1) % 100) == 0) {
st.executeBatch();
}
if (((i + 1) % 10000) == 0) {
conn.commit();
}
}
long e = System.currentTimeMillis();
System.out.printf("%s\t%d%n", name, e - s);
}
protected abstract void setPrepared(PreparedStatement st, int i)
throws SQLException;
}
static class ExecTimestamp extends Exec {
public ExecTimestamp() {
super("Timestamp");
}
static final Timestamp TIME = new Timestamp(0);
@Override
protected void setPrepared(PreparedStatement st, int i)
throws SQLException {
st.setTimestamp(1, TIME);
}
}
static class ExecString extends Exec {
public ExecString() {
super("String");
}
static final String STR = new Timestamp(0).toString();
@Override
protected void setPrepared(PreparedStatement st, int i)
throws SQLException {
st.setString(1, STR);
}
}
static class ExecString2 extends Exec {
public ExecString2() {
super("String2");
}
@Override
protected String createSql() {
return "insert into table3 values(to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss.ff'))";
}
static final String STR = new Timestamp(0).toString();
@Override
protected void setPrepared(PreparedStatement st, int i)
throws SQLException {
st.setString(1, STR);
}
}
static class ExecStringToTime extends Exec {
public ExecStringToTime() {
super("StringToTime");
}
static final String STR = new Timestamp(0).toString();
@Override
protected void setPrepared(PreparedStatement st, int i)
throws SQLException {
st.setTimestamp(1, Timestamp.valueOf(STR));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment