高效批量插入大量数据----JDBC-4

平安夜,只要像我这样的单身狗还在键盘前码字,其他人都去浪的浪啪啪啪的啪啪啪再见唉~苦逼

不说了,开始我的正题。今天要说的是如何高效插入大量数据,会对三种方法进行速度比较。

代码如下:

注意:为获得较为直观的感受,一下所有操作都是针对oracle数据库进行操作!


示例一:使用Statement进行插入(速度最慢的一种)

        /**
	 * 向  Oracle 的 customers 数据表中插入 10 万条记录
	 * 测试如何插入, 用时最短. 
	 * @author HelloWorld 2015/12/24
	 */
	@Test
	public void test1() {
		Connection connection = null;
		
		Statement statement = null;
		String sql = null;
		
		try {
			connection = getConnection();//获取数据库连接,具体操作可查看往期<span style="font-family: Arial, Helvetica, sans-serif;">博文</span>

			statement = connection.createStatement();
			
			long begin = System.currentTimeMillis();//记录开始插入的时间
			                                                                                                                                                          for(int i = 0;i<100000;i++) {	//插入十万条数据			                                                                                                    sql = "insert into users values("+(i+1)
						+",'user_"+i+"')";
				statement.executeUpdate(sql);//所用时间为:176867,机子很垃圾所以跑的很慢
			}
			
			long end = System.currentTimeMillis();
			
			System.out.println("所用时间为:"+(end-begin));//打印所需时间
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//释放相关资源,具体可看往期博文
		}
		
	}

示例二:使用PreparedStatement进行插入(速度快于Statement,原因是其可以重复使用预编译语句

      /**
       *@author HelloWorld 2015/12/24
       *
       */
       @Test
	public void testBatchWithPreparedStatement() {
		Connection connection = null;
		
		PreparedStatement statement = null;
		String sql = null;
		
		try {
			connection = getConnection();<span style="font-family: Arial, Helvetica, sans-serif;">//获取数据库连接,具体操作可查看往期</span><span style="font-family: Arial, Helvetica, sans-serif;">博文</span>
			
			sql = "insert into users values(?,?)";
			statement = connection.prepareStatement(sql);
			
			long begin = System.currentTimeMillis();
			
			for(int i = 0;i<100000;i++) {
				statement.setInt(1, i+1);
				statement.setString(2, "<span style="font-family: Arial, Helvetica, sans-serif;">user_</span>"+i);
				statement.executeUpdate();//所用时间为:92099
			}
			
			long end = System.currentTimeMillis();
			
			System.out.println("所用时间为:"+(end-begin));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//释放相应资源
		}
	}


示例三:使用batch()(效率最高

        /**
         *@autor HelloWorld 2015/12/24
         */                                                                                                                                                       @Test
	public void testBatch() {
		Connection connection = null;
		
		PreparedStatement statement = null;
		String sql = null;
		
		try {
			connection = getConnection();//获取数据库连接具体可以看往期博文
			
			sql = "insert into users values(?,?)";
			statement = connection.prepareStatement(sql);
			
			long begin = System.currentTimeMillis();
			
			for(int i = 0;i<100000;i++) {
				statement.setInt(1, i+1);
				statement.setString(2, "name_"+i);
				statement.setDate(3, date);
				
				statement.addBatch();//使用batch()
				 //所用时间为:1497
				if((i+1) % 350 ==0) {//300 所用时间为:1738  400所用时间为:2130 200所用时间为:3600

					statement.executeBatch();
					statement.clearBatch();
				}
			}
			//如果除不尽保证把所有数据都能插入
			if(100000%350!=0) {
				statement.executeBatch();
				statement.clearBatch();
			}
			
			long end = System.currentTimeMillis();
			
			System.out.println("所用时间为:"+(end-begin));
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//释放相应资源 
		}
	}


实验总结:

Statement:所用时间为:176867,机子很垃圾所以跑的很慢

PreparedStatement:所用时间为:92099

使用batch能加快效率的原因是:不立刻执行插入数据操作,要进行“收集”之后,在进行统一插入

(例如:用车拉西瓜,如果每摘下一个就送到销售地,效率就很低,但是如果装满一车再送到销售地就会大大提高效率!)

一下是使用batch插入数据在不同的一次性插入数目下所用的时间表

一次插入数目:200 300 350 400

使用的时间:3600 1738 1497 2130

显然效率远远高于前两种,而且不同的“收集”数目下效率也不尽相同


OK~睡觉啦。


如有不足请多多指教奋斗,如果觉得对你有帮助请点赞支持~害羞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值