1. 程式人生 > >phoenix 批量插入優化(一次commit,多次commit比較)

phoenix 批量插入優化(一次commit,多次commit比較)

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 static void 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(int i=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 static void 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(int i=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倍,並且資料量越大,第二種效果更明顯,甚至能達到幾十倍幾百倍