Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 批量插入需要难以置信的长时间_Java_Mysql - Fatal编程技术网

Java 批量插入需要难以置信的长时间

Java 批量插入需要难以置信的长时间,java,mysql,Java,Mysql,一次插入大量行时遇到问题。 我们正在本地托管MySQL数据库的机器上运行该项目,以确保服务器的性能不会出现问题。 这么长的插入时间有什么办法吗 代码中的注释准确地解释了问题出在哪一行 public void batchExecute(字符串查询,BatchArgSetter BatchArgSetter){ Connection con=this.getConnection(); PreparedStatement=null; int i=0; 试一试{ con.setAutoCommit(假)

一次插入大量行时遇到问题。
我们正在本地托管MySQL数据库的机器上运行该项目,以确保服务器的性能不会出现问题。
这么长的插入时间有什么办法吗

代码中的注释准确地解释了问题出在哪一行

public void batchExecute(字符串查询,BatchArgSetter BatchArgSetter){
Connection con=this.getConnection();
PreparedStatement=null;
int i=0;
试一试{
con.setAutoCommit(假);
对账单=对账单(查询);
while(batchArgSetter.hasNext()){
//此代码用于设置参数和添加批处理是即时的。大约每1000次0.05毫秒。
setArgs(语句,batchArgSetter.getArgs(batchArgSetter.next());
语句addBatch();
i++;
if(i%1000==0 | | i==batchArgSetter.getSize()){
//然而,这条线需要很长时间。
//每批大约30秒。测试9600个条目的总时间为5分钟42秒
//我们尝试测试一个100k+条目的集合,在id运行10分钟后停止测试+
语句。executeBatch();
con.commit();
}
}
}捕获(SQLE异常){
e、 printStackTrace();
}最后{
closeAll(语句,null,con);
}
}
SQL类的设置
公共类SqlConnection{
私有字符串用户名;
私有字符串密码;
私有字符串url;
private HikariConfig config=new HikariConfig();
私人HikariDataSource ds;
公共SqlConnection(Setting.PersistenceSetting){
this.username=setting.getUsername();
this.password=setting.getPassword();
this.url=“jdbc:mysql://“+setting.getUrl()+”:“+setting.getPort()+”/“+setting.getDatabase()+”?serverTimezone=UTC”;
设置(setting.getPoolSize());
}
专用void设置(int poolSize){
config.setJdbcUrl(url);
config.setUsername(用户名);
config.setPassword(密码);
config.addDataSourceProperty(“cachePrepsmts”、“true”);
config.addDataSourceProperty(“prepStmtCacheSize”、“250”);
config.addDataSourceProperty(“prepStmtCacheSqlLimit”,“2048”);
config.setMaximumPoolSize(池大小);
config.setLeakDetectionThreshold(2000);
config.setAutoCommit(true);
ds=新的HikariDataSource(配置);
}
我调用SQL方法的代码
private void insertMembers(列表成员){
sql.batchExecute(“在重复密钥更新用户名=?,鉴别器=?,化身=?;”上插入用户值(?,?,?),新的BatchArgSetter(成员){
@凌驾
公共对象[]getArgs(成员m){
返回新对象[]{
m、 getIdLong(),
m、 getUser().getName(),
m、 getUser().getDiscriminator(),
m、 getUser().getEffectiveAvatarUrl(),
m、 getUser().getName(),
m、 getUser().getDiscriminator(),
m、 getUser().getEffectiveAvatarUrl()
};
}
});
sql.batchExecute(“在重复密钥更新昵称=?”上插入帮会成员值(?,?),新的BatchArgSetter(成员){
@凌驾
公共对象[]getArgs(成员){
返回新对象[]{
member.getIdLong(),
member.getGuild().getIdLong(),
member.get昵称(),
member.get昵称()
};
}
});
}

欢迎使用堆栈溢出。请学习堆栈溢出的工作原理,并阅读如何提高问题的质量。然后,您的问题将包括您作为一个完整的源代码,其他人可以对其进行编译和测试。有关SQL相关问题,请参阅。现在,一次提交跨越所有插入,可能每1000个wou中有一个ld会更好。但我怀疑,它是否会产生巨大的差异。我会看看一些db数字。java方面现在看起来很干净。适当的索引和确定性,它们被使用会很好,否则每次插入/更新都会有一个完整的表扫描。