1.批量執(zhí)行SQL語(yǔ)句
當(dāng)需要成批插入或者更新記錄時(shí),可以采用Java的批量更新機(jī)制,這一機(jī)制允許多條語(yǔ)句一次性提交給數(shù)據(jù)庫(kù)批量處理。通常情況下比單獨(dú)提交處理更有效率
JDBC的批量處理語(yǔ)句包括下面三個(gè)方法:
- addBatch(String):添加需要批量處理的SQL語(yǔ)句或是參數(shù);
- executeBatch():執(zhí)行批量處理語(yǔ)句;
- clearBatch():清空緩存的數(shù)據(jù)
通常我們會(huì)遇到兩種批量執(zhí)行SQL語(yǔ)句的情況:
- 多條SQL語(yǔ)句的批量處理;
- 一個(gè)SQL語(yǔ)句的批量傳參;
*高效的批量插入
jdbc.properties配置文件
user=root
password=root
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useSSL=false&serverTimezone=UTC
driverClass=com.mysql.cj.jdbc.Driver
數(shù)據(jù)庫(kù)中提供一個(gè)goods表。創(chuàng)建如下:
CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(25)
);
實(shí)現(xiàn)方式匯總
package com.lty5.blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.Test;
import com.lty3.util.JDBCUtils;
/**
* 測(cè)試使用PreparedStatement實(shí)現(xiàn)批量數(shù)據(jù)的操作
*
* update、delete本身就具有批量操作的效果
* 此時(shí)的批量操作,主要指的是批量插入。使用PreparedStatement如何實(shí)現(xiàn)更高效的批量插入?
*
*
*
* 題目: 向goods表中插入20000條數(shù)據(jù)
* CREATE TABLE goods(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(25)
);
方式一: 使用Statement
Connection conn = JDBCUtils.getConnection();
Statement st = conn.createStatement();
for(int i = 1 ; i <= 20000 ; i++){
String sql = "insert into goods(name)values('name_" + i + "')";
st.execute(sql);
}
方式二: 使用PreparedStatement
*/
public class InsertTest {
// 批量插入操作的方式二 : 使用PreparedStatement替換Statement
@Test
public void testInsert1() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)values(?)";
ps = conn.prepareStatement(sql);
for(int i = 1 ; i <= 20000 ; i++) { // 花費(fèi)的時(shí)間為: 1528443
ps.setObject(1, "name_" + i);
ps.execute();
}
long end = System.currentTimeMillis();
System.out.println("花費(fèi)的時(shí)間為:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps);
}
}
/**
* 批量插入的方式三 :
* 1. addBatch(String) 2.executeBatch() 3.clearBatch()
* 2. mysql服務(wù)器默認(rèn)是關(guān)閉批處理的,我們需要通過(guò)一個(gè)參數(shù),讓mysql開啟批處理的支持。
* ?rewriteBatchedStatements=true 寫在配置文件的url后面
* 3. 使用更新的mysql 驅(qū)動(dòng):mysql-connector-java-5.1.37-bin.jar
*/
@Test
public void testInsert2() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
String sql = "insert into goods(name)values(?)";
ps = conn.prepareStatement(sql);
for(int i = 1 ; i <= 20000 ; i++) { // 花費(fèi)的時(shí)間為:5540
ps.setObject(1, "name_" + i);
//1. "攢" sql
ps.addBatch();
if(i % 500 == 0) {
//2. 攢夠500,執(zhí)行一次batch
ps.executeBatch();
//3. 清空batch
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("花費(fèi)的時(shí)間為:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps);
}
}
/**
* 批量插入的方式四 : 設(shè)置連接不允許自動(dòng)提交數(shù)據(jù)
*/
@Test
public void testInsert3() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtils.getConnection();
// 設(shè)置不允許自動(dòng)提交數(shù)據(jù)
conn.setAutoCommit(false);
String sql = "insert into goods(name)values(?)";
ps = conn.prepareStatement(sql);
for(int i = 1 ; i <= 1000000 ; i++) { // 花費(fèi)的時(shí)間為:23058
ps.setObject(1, "name_" + i);
//1. "攢" sql
ps.addBatch();
if(i % 500 == 0) {
//2. 攢夠500,執(zhí)行一次batch
ps.executeBatch();
//3. 清空batch
ps.clearBatch();
}
}
// 提交數(shù)據(jù) (統(tǒng)一將所有數(shù)據(jù)都進(jìn)行提交)
conn.commit();
long end = System.currentTimeMillis();
System.out.println("花費(fèi)的時(shí)間為:" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, ps);
}
}
}