今天对Insert进行了性能测试,结果反差很大,平时都是单条插入,虽然性能要求没有那么高,但是突然在项目中,人家给定时间内完成,这就尴尬了.

优化数据库,优化服务器,优化代码,反正通过各种优化提高数据的处理速度.

接下来对jdbc插入做一个测试,测试代码入如下:

    /**
* 生成插入语句
* @author fgq 2017年12月26日 下午6:40:03
* @return
*/
public static List<String> getInsertSql(){
String sqlModel = "insert into test_cost1(id,name)values('{id}','{name}')";
List<String> insertSqls = new ArrayList<String>();
for(int i=0;i<10000;i++){
sqlModel = sqlModel.replace("{id}", UUIDUtil.getRandomUUID());
sqlModel = sqlModel.replace("{name}", "danny");
insertSqls.add(sqlModel);
}
return insertSqls;
}
    /**
* Statement单条插入
* @author fgq 2017年12月26日 下午6:39:05
* @param conn
* @throws Exception
*/
public static void executeSQL(Connection conn) throws Exception {
List<String> insertSql = getInsertSql();
Statement stmt = conn.createStatement();
for(String sql : insertSql){
stmt.execute(sql);
}
} /**
* Statement批量插入
* @author fgq 2017年12月26日 下午6:39:24
* @param conn
* @throws Exception
*/
public static void executeBatchSQL(Connection conn) throws Exception {
List<String> insertSql = getInsertSql();
Statement stmt = conn.createStatement();
for(String sql : insertSql){
stmt.addBatch(sql);
}
stmt.executeBatch();
}
    /**
* PreparedStatement批量插入
* @author fgq 2017年12月26日 下午6:40:34
* @param conn
* @throws Exception
*/
public static void batchInsertData(Connection conn) throws Exception{
String prefix = "insert into test_cost1(id,name)values(?,?)";
PreparedStatement pst = conn.prepareStatement(prefix);
for(int j = 0;j<10000;j++){
pst.setString(1, UUIDUtil.getRandomUUID());
pst.setString(2, "liming");
pst.addBatch();
}
pst.executeBatch();
} /**
* PreparedStatement单条插入
* @author fgq 2017年12月26日 下午6:40:16
* @param conn
* @throws Exception
*/
public static void insertData(Connection conn) throws Exception{
String prefix = "insert into test_cost1(id,name)values(?,?)";
PreparedStatement pst = conn.prepareStatement(prefix);
for(int j = 0;j<10000;j++){
pst.setString(1, UUIDUtil.getRandomUUID());
pst.setString(2, "liming");
pst.executeUpdate();
}
}
    public static void main(String[] args) throws Exception {
final String url = "jdbc:oracle:thin:@123.123.123.123:1521/orcl";
final String name = "oracle.jdbc.driver.OracleDriver";
final String user = "test";
final String password = "test";
Connection conn = null;
Class.forName(name);//指定连接类型
conn = DriverManager.getConnection(url, user, password);//获取连接
if (conn!=null) {
System.out.println("获取连接成功");
long startTime = System.currentTimeMillis();
insertData(conn);
System.out.println("执行1000插入耗时:"+(System.currentTimeMillis() - startTime));
}else {
System.out.println("获取连接失败");
}
}

通过上面10000条测试结果,发现效率最高的是

batchInsertData

最慢的是

insertData
所以在进行第三方库的插入,最好选择效率最高的,而且在批量执行的时候,最好不要有数据冲突,否则执行失败,所以与业务无关的主键很重要.

JDBC插入性能优化对比的更多相关文章

  1. 大数据应用之HBase数据插入性能优化实测教程

    引言: 大家在使用HBase的过程中,总是面临性能优化的问题,本文从HBase客户端参数设置的角度,研究HBase客户端数据批量插入性能优化的问题.事实胜于雄辩,数据比理论更有说服力,基于此,作者设计 ...

  2. MySQL插入性能优化

    目录 MySQL插入性能优化 代码优化 values 多个 一个事务 插入字段尽量少,尽量用默认值 关闭 unique_checks bulk_insert_buffer_size 配置优化 inno ...

  3. MySQL批量SQL插入性能优化

    对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时或十几个小时之久.因此,优化数据库插入性能是很有意义的. ...

  4. MySQL插入性能优化(转)

    原文:http://tech.uc.cn/?p=634 对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时 ...

  5. 大数据应用之HBase数据插入性能优化之多线程并行插入测试案例

    一.引言: 上篇文章提起关于HBase插入性能优化设计到的五个参数,从参数配置的角度给大家提供了一个性能测试环境的实验代码.根据网友的反馈,基于单线程的模式实现的数据插入毕竟有限.通过个人实测,在我的 ...

  6. laravel 5.1 性能优化对比 - 框架提供的方法

    写了一个项目发现性能不如人意. 于是便测试下, 看下性能瓶颈在什么地方. 使用 ab -n 20 http://www.lartest.com/ 软件环境: OS : windows 8.1 CPU: ...

  7. Flask性能优化对比

    基于Flask的网关:Flask,Uwsgi,Gevent,Gunicorn(gevent),Tornado,Twisted !/usr/bin/python -- coding:utf-8 -- 美 ...

  8. Android Sqlite 批量插入性能优化

    db.beginTransaction(); try { for (...) { db.execSQL("...", new Object[]{}); } db.setTransa ...

  9. 【转】MySQL批量SQL插入各种性能优化

    原文:http://mp.weixin.qq.com/s?__biz=MzA5MzY4NTQwMA==&mid=403182899&idx=1&sn=74edf28b0bd29 ...

随机推荐

  1. Oracle数据库模型(OLAP/OLTP)

    数据库模型 选择数据库模型: 联机事务处理OLTP(on-line transaction processing) OLTP是传统的关系数据库的主要应用,基本的.日常的事务处理.例如银行交易. OLT ...

  2. https://blog.newrelic.com/2014/05/02/25-php-developers-follow-online/

    w https://blog.newrelic.com/2014/05/02/25-php-developers-follow-online/ 1. Rob Allen. Zend Framework ...

  3. Java 语言基础之数组(一)

    数组定义及格式: 数组: 同一种类型数据的集合, 就是一个容器 定义数组格式1: 元素类型[] 数组名 = new 元素类型[元素个数(即数组长度)]; 说明: 数组是一个容器.而容器属于一个实体,实 ...

  4. centos7 docker 安装配置

    docker快速入门测试 ########################################## #docker安装配置 #环境centos7 #配置docker阿里源 echo '#D ...

  5. 去除MyEclipse 中新建servlet多余的注释问题

    1.找到你的MyEclipse 的安装目录 2.点击文件位置,找到安装目录下的Common 文件夹下的plugins 3.找到com.genuitec.eclipse.wizards.jar  文件, ...

  6. sql server安装教程(2008 R2,图形界面安装/命令提示符安装即静默安装)

    转自:http://blog.51cto.com/jimshu/585023 SQL Server 2008(32/64位)下载地址: 链接:https://pan.baidu.com/s/1eR5b ...

  7. 005-环境安装【docker、fabric】

    1.参考地址:https://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html#install-curl 一.前置条件和系统配置 1.安 ...

  8. Android图片加载框架Picasso最全使用教程1

    Picasso介绍 Picasso是Square公司开源的一个Android图形缓存库 A powerful image downloading and caching library for And ...

  9. redis的一些命令

    字符串操作 EX在设置值的时候设置过期时间,ttl查看过期时间 expire能单独设置过期时间 查看所有的key key * 列表操作 lpush从列表左边添加值,rpush从列表右边添加值 lran ...

  10. 一行代码实现笔记本跳过微信认证连接WIFI

    一行代码实现笔记本跳过微信认证连接WIFI 本文作者原创,没有参考其他文章,方法很简单但是很实用,转载请注明出处,谢谢! 问题 有一些WIFI需要通过微信认证才能连接,手机当然是可以的,但是我们手头的 ...