phoenix 批量插入优化(一次commit,多次commit比较)

来源:互联网 发布:佳能mp280清零软件 编辑:程序博客网 时间:2024/06/04 17:00

1、没插入phoenix表一条,commit一次

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.PreparedStatement;

import java.sql.Statement;


public class Test_phoenix {


    public staticvoid main(String[] args) throws SQLException {

        Statement stmt = null;

        ResultSet rset = null;

        int n=50;

        Connection con = DriverManager.getConnection("jdbc:phoenix:43.247.90.151");

        stmt = con.createStatement();


//        stmt.executeUpdate("create table test (mykey integer not null primary key,mycolumn varchar)");

        Long a=System.currentTimeMillis();

        for(inti=0;i<n;i++){

        stmt.executeUpdate("upsert into test values ("+i+",'Hello')");

        con.commit();

        }

        Long b=System.currentTimeMillis();

        System.out.println(b-a);


        con.close();

    }

}


2、插入多条后做一次commit

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class Phoenix_test {


    public staticvoid main(String[] args) throws SQLException {

        Statement stmt = null;


        Connection con = DriverManager.getConnection("jdbc:phoenix:43.247.90.151");

        stmt = con.createStatement();

        int n =50;


//        stmt.executeUpdate("create table test (mykey integer not null primary key,mycolumn varchar)");

        Long a=System.currentTimeMillis();

        for(inti=0;i<n;i++){

        stmt.executeUpdate("upsert into test values ("+i*5+",'Hello')");

        }

        con.commit();

        Long b=System.currentTimeMillis();

        System.out.println(b-a);


        con.close();

    }

}



结论:相同环境下,同时插入50条,第二种速度是第一种的5-10倍,并且数据量越大,第二种效果更明显,甚至能达到几十倍几百倍



原创粉丝点击