设为首页 收藏本站
查看: 657|回复: 0

[经验分享] Oracle入库速度测试(Java版)

[复制链接]

尚未签到

发表于 2016-7-30 16:46:10 | 显示全部楼层 |阅读模式
测试环境:
    Intel Xeon 2.4G四核心 2.5G内存
    Server 2003 Enterprise Edition Service Pack 2
    Oracle9i Enterprise Edition 9.2.0.1.0
    jdk1.5.0_12
    ojdbc14.jar
建立测试表:
CREATE TABLE TEST
(
TEST_ID NUMBER(10, 0),
TEST_NAME VARCHAR2(50),
TEST_TIME TIMESTAMP,
TEST_VALUE NUMBER(10, 3)
);
连接Oracle过程略。
一个测试用的类:
public class Util {
public static Random rand = new Random();
public static String atoz = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String genString(int length)
{
StringBuilder re = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
re.append(atoz.charAt(rand.nextInt(52)));
}
return re.toString();
}
public static double genDouble()
{
double d1 = 2500 * rand.nextDouble();
double d2 = 500000 * rand.nextDouble();
return d1 + d2;
}
}

拼sql入库:
public static void simpleInsert(int total) throws Exception {
Thread.sleep(3000);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp current = new Timestamp(System.currentTimeMillis());
String currentStr = dateFormat.format(current);
Connection conn = DriverManager.getConnection(dbURL, user, password);
try {
long begin = System.currentTimeMillis();
Statement s = conn.createStatement();
for (int i = 1; i <= total; i++) {
String sql = String.format("INSERT INTO TEST (TEST_ID, TEST_NAME, TEST_TIME, TEST_VALUE) VALUES (%s, '%s', to_date('%s','yyyy-MM-dd HH24:mi:ss'), %s)", i, Util.genString(5), currentStr, Util.genDouble());
s.executeUpdate(sql);
}
long end = System.currentTimeMillis();
System.out.printf("Count:%d Time:%d\n", total, (end - begin));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
conn.close();
}
}
测试结果:
Count:10240 Time:25203
Count:10240 Time:23891
Count:10240 Time:24516
Count:10240 Time:24891
Count:10240 Time:25063
Count:10240 Time:24359
Count:10240 Time:23547
Count:10240 Time:24312
Count:10240 Time:23938
Count:10240 Time:24687

总结:
平均入库速度每1万条23.87秒
绑定参数法入库:
public static void traditionalInsert(int total) throws Exception {
Thread.sleep(3000);
Timestamp current = new Timestamp(System.currentTimeMillis());
Connection conn = DriverManager.getConnection(dbURL, user, password);
try {
long begin = System.currentTimeMillis();
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("INSERT INTO TEST (TEST_ID,TEST_NAME,TEST_TIME,TEST_VALUE) VALUES (?, ?, ?, ?)");
for (int i = 1; i <= total; i++) {
ps.setInt(1, i);
ps.setString(2, Util.genString(5));
ps.setTimestamp(3, current);
ps.setBigDecimal(4, new BigDecimal(Util.genDouble()));
ps.addBatch();
if ((i % 500) == 0) {
ps.executeBatch();
}
}
ps.executeBatch();
conn.commit();
long end = System.currentTimeMillis();
System.out.printf("Count:%d Time:%d\n", total, (end - begin));
} catch (Exception ex) {
ex.printStackTrace();
conn.rollback();
} finally {
conn.close();
}
}
测试结果:
Count:512000 Time:33000
Count:512000 Time:31344
Count:512000 Time:31407
Count:512000 Time:31281
Count:512000 Time:31891
Count:512000 Time:31219
Count:512000 Time:31844
Count:512000 Time:32125
Count:512000 Time:32047
Count:512000 Time:33141

总结:
平均入库速度每100万条62.36秒
绑定参数法虽然比较快,但是还有没有挖掘的空间?
每500条执行一次executeBatch是不是最合适的?
Java中有没有数组绑定法?
有待进一步考察……

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-251356-1-1.html 上篇帖子: ORACLE常用脚本—PROFILE的管理 下篇帖子: oracle函数笔记(主要是日期函数)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表