当前位置:网站首页>Data batch writing
Data batch writing
2022-06-12 21:34:00 【Besieged city_ city with high walls】
Write data in bulk
1.Statement write in
@Test
public void testInsert2() {
Connection conn = null;
PreparedStatement ps = null;
try {
// Turn off auto submit
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
// 1. Get the connection
conn = JDBCUtils.getConnection();
// 2. establish statement object
ps = conn.createStatement();
for(int i = 1 ; i <= 20000 ; i++) {
String sql = "insert into goods(name)values("+ "name_" + i +")";
//1. " Save " sql
ps.addBatch(sql);
if(i % 500 == 0) {
//2. Save enough 500, Do it once batch
ps.executeBatch();
//3. Submit
conn.commit();
}
}
long end = System.currentTimeMillis();
System.out.println(" The time spent is :" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.PreparedStatement Batch write
@Test
public void testInsert2() {
Connection conn = null;
PreparedStatement ps = null;
try {
// Turn off auto submit
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
// 1. Get the connection
conn = JDBCUtils.getConnection();
// 2. establish prepareStatement object
String sql = "insert into goods(name)values(?)";
ps = conn.prepareStatement(sql);
for(int i = 1 ; i <= 20000 ; i++) {
ps.setObject(1, "name_" + i); // The representative is set to the first ? The value of position number is Object type
//1. " Save " sql
ps.addBatch();
if(i % 500 == 0) {
//2. Save enough 500, Do it once batch
ps.executeBatch();
//3. Submit
conn.commit();
//4. Empty batch
ps.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println(" The time spent is :" + (end - start));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.Statement and PreparedStatement Relationships and differences
Statement and PreparedStatement The relationship and difference between .
Relationship :PreparedStatement Inherited from Statement, It's all interfaces
difference :PreparedStatement You can use placeholders , It's precompiled , Batch processing is better than Statement Efficient
How to choose to use Statement and PreparedStatement object
JDBC Driver optimization is based on what features are used . choice PreparedStatement still Statement It depends on how you use them . about Carry out only once SQL Statement selection Statement It's the best . contrary , If SQL Statement is selected for multiple executions PreparedStatement It's the best .
PreparedStatement The first execution cost of is very high . Its performance is reflected in the following repeated execution
Statement object :
Simple to perform without parameters SQL sentence ;
characteristic :
a. Execute only single sql sentence ;
b. Can only execute... Without parameters sql sentence ;
c. From the perspective of operational principles , Database received sql After the statement, you need to sql Statement is compiled before execution ;
d. Compare with other interfaces , It is suitable for executing a single without parameters sql sentence , In this case, the implementation efficiency is relatively high .PreparedStatement object
Execute with or without IN Precompiled parameters SQL sentence ;
characteristic :
a. Inherited from Statement Interface ( It means that the functions are relatively more comprehensive );
b. With precompiled features ;
c. Batch processing sql sentence ;
d. Handle with unknown parameters sql sentence ;
e. It's safe , That is, malicious sql Statement injection attack ;
f. In dealing with a single statement , Execution efficiency is not Statement fast ;
g. Improve the readability and maintainability of the program .
Execution efficiency ( If they are all inserted in the database table 10000 In case of records ):
1. Use Statement object when 31 second
2. precompile PreparedStatement when 14 second
3. Use PreparedStatement + The batch when 485 millisecond
1. Use Statement object
Using range : When performing similar SQL( Structure is the same , The specific values are different ) The number of statements is relatively small
advantage : Grammar is simple
shortcoming : Hard coding is inefficient , Poor safety .
principle : Hard encoding , Each execution is similar SQL Will compile
public void exec(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);// Set up manual submission
Statement st = conn.createStatement();
for(int i=0;i<10000;i++){
String sql="insert into t1(id) values ("+i+")";
st.executeUpdate(sql);
}
Long endTime = System.currentTimeMillis();
System.out.println("Statement when :"+(endTime-beginTime)/1000+" second ");// computing time
st.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
execution time :Statement when :31 second
2. precompile PreparedStatement
Using range : When performing similar sql The number of statements is more ( For example, user login , Frequent operations on tables …) The sentence is the same , But the specific values are different , It's called dynamic SQL
advantage : Statement is compiled only once , Reduce compilation times . Improved security ( Stopped SQL Inject )
shortcoming : Perform nonsimilar SQL When the sentence is , Slower .
principle : be similar SQL Compile only once , Reduce compilation times
Case execution process :
public void exec2(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);// Manual submission
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=0;i<10000;i++){
pst.setInt(1, i);
pst.execute();
}
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("Pst when :"+(endTime-beginTime)+" second ");// computing time
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
execution time :Pst when :14 second
3. Use PreparedStatement + The batch
Using range : You need to update multiple records in the database table at one time
advantage : Reduce and SQL Number of engine interactions , Improve efficiency again , Similar statements are compiled only once , Reduce compilation times . Improved security ( Stopped SQL Inject )
shortcoming :
principle : The batch : Reduce and SQL Number of engine interactions , Pass to at one time SQL Engine multiple SQL.
A term is used to explain :
PL/SQL engine : stay oracle In the implementation of pl/sql The engine of code , Standard found in implementation sql I'll give it to you sql The engine processes .
SQL engine : Executive Standards sql The engine of .
Case execution process :
public void exec3(Connection conn){
try {
conn.setAutoCommit(false);
Long beginTime = System.currentTimeMillis();
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=1;i<=10000;i++){
pst.setInt(1, i);
pst.addBatch();// Add batch , package
if(i%1000==0){
// You can set different sizes ; Such as 50,100,500,1000 wait
pst.executeBatch();
conn.commit();
pst.clearBatch();
}//end of if
}//end of for
pst.executeBatch();
Long endTime = System.currentTimeMillis();
System.out.println("pst+batch when :"+(endTime-beginTime)+" millisecond ");
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
execution time :pst+batch when :485 millisecond
边栏推荐
- #113 Path Sum II
- drf 接收嵌套数据并创建对象, 解决:drf NOT NULL constraint failed
- Deep Hough voting for 3D object detection in point clouds
- 同花顺能开户吗,在APP上可以直接开通券商安全吗
- Redis cluster mget optimization
- Teambition 协作应用心得分享|社区征文
- #981 Time Based Key-Value Store
- zgc 并发标识和并发转移阶段的多视图地址映射
- ZGC concurrent identity and multi view address mapping in concurrent transition phase
- Introduction to the characteristics of balancer decentralized exchange market capitalization robot
猜你喜欢

阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds

SQL调优指南笔记18:Analyzing Statistics Using Optimizer Statistics Advisor

leetcode:207. Class Schedule Card

leetcode:207. 课程表

Introduction to the characteristics of balancer decentralized exchange market capitalization robot

leetcode:210. 課程錶 II

shell语言

@loadbalance annotation of resttemplate

图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?

测试基础之:单元测试
随机推荐
leetcode:210. Schedule II
Pointer and array & pointer and const & struct and Const
zgc 并发标识和并发转移阶段的多视图地址映射
Pytorch how to set random number seed to make the result repeatable
Linux backup MySQL
数据批量写入
SQL调优指南笔记11:Histograms
复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展
风控建模十:传统建模方法存在的问题探讨及改进方法探索
Binary search
求解一维数组前缀和
CVPR 2022 | 应对噪声标签,西安大略大学、字节跳动等提出对比正则化方法
阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds
To delete a character from a string
DRF receives nested data and creates objects. Solution: DRF not NULL constraint failed
Libmysqlclient A static library
#113 Path Sum II
KDD2022 | GraphMAE:自监督掩码图自编码器
GNS安装与配置
Li Mu [practical machine learning] 1.4 data annotation