Java 如何在毫秒内将数千条记录更新到MySQL数据库

Java 如何在毫秒内将数千条记录更新到MySQL数据库,java,mysql,jdbc,Java,Mysql,Jdbc,我想在不到一秒钟的时间内将大约10K条记录更新到MySQL数据库中。我写了下面的代码,将记录列表更新到DB中大约需要6-8秒 public void updateResultList(List<?> list) { String user = "root"; String pass = "root"; String jdbcUrl = "jdbc:mysql://12.1.1.1/db_1?useSSL=fals

我想在不到一秒钟的时间内将大约10K条记录更新到MySQL数据库中。我写了下面的代码,将记录列表更新到DB中大约需要6-8秒

public void updateResultList(List<?> list) {
            String user = "root";
            String pass = "root";
            String jdbcUrl = "jdbc:mysql://12.1.1.1/db_1?useSSL=false";
            String driver = "com.mysql.jdbc.Driver";
            PreparedStatement pstm = null;

            try {
                Class.forName(driver);
                Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
                myConn.setAutoCommit(false);
                for(int i=0; i<list.size(); i++) {
                    Object[] row = (Object[]) list.get(i);
                    int candidateID = Integer.valueOf(String.valueOf(row[0]));
                    String result = String.valueOf(row[14]);
                    int score = Integer.valueOf(String.valueOf(row[19]));
                    String uploadState = (String) row[20];

                    String sql = "UPDATE personal_info SET result = ?, score = ?, uploadState = ? "
                                + " WHERE CandidateID = ?";

                    pstm = (PreparedStatement) myConn.prepareStatement(sql);
                    pstm.setString(1, result);
                    pstm.setInt(2, score);
                    pstm.setString(3, uploadState);
                    pstm.setInt(4, candidateID);
                    pstm.addBatch();
                    pstm.executeBatch();

                }
                myConn.commit();
                myConn.setAutoCommit(true);
                pstm.close();
                myConn.close();

            }
            catch (Exception exc) {
                exc.printStackTrace();
                try {
                    throw new ServletException(exc);
                } catch (ServletException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }   
       }
public void updateResultList(列表){
字符串user=“root”;
字符串pass=“root”;
String jdbcUrl=“jdbc:mysql://12.1.1.1/db_1?useSSL=false";
String driver=“com.mysql.jdbc.driver”;
PreparedStatement pstm=null;
试一试{
Class.forName(驱动程序);
Connection myConn=DriverManager.getConnection(jdbcUrl、user、pass);
myConn.setAutoCommit(假);

for(int i=0;i您的
pstm.executeBatch()
应该在
for
循环之后


请首先参阅,您只需要初始化
prepareStatement
一次,您需要在
for
循环之前初始化它

其次,您应该避免执行
pstm.executeBatch();
对于每个循环,它将花费更多的资源,您需要以指定的数量执行它,例如100500或更多,也不要在
for
循环之外只执行一次,因为它将消耗更多的内存资源

Class.forName(driver);
Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
myConn.setAutoCommit(false);
String sql = "UPDATE personal_info SET result = ?, score = ?, uploadState = ? "
                + " WHERE CandidateID = ?";
pstm = (PreparedStatement) myConn.prepareStatement(sql);
for(int i=0; i<list.size(); i++) {
    Object[] row = (Object[]) list.get(i);
    int candidateID = Integer.valueOf(String.valueOf(row[0]));
    String result = String.valueOf(row[14]);
    int score = Integer.valueOf(String.valueOf(row[19]));
    String uploadState = (String) row[20];
    pstm.setString(1, result);
    pstm.setInt(2, score);
    pstm.setString(3, uploadState);
    pstm.setInt(4, candidateID);
    pstm.addBatch();
    if(i%500==0){//execute when it meet a specified amount
        pstm.executeBatch();
    }
}
pstm.executeBatch();
myConn.commit();
myConn.setAutoCommit(true);
Class.forName(驱动程序);
Connection myConn=DriverManager.getConnection(jdbcUrl、user、pass);
myConn.setAutoCommit(假);
String sql=“更新个人信息集结果=?,分数=?,上载状态=?”
+“其中CandidateID=?”;
pstm=(PreparedStatement)myConn.prepareStatement(sql);

对于(int i=0;i,您可以使用
rewriteBatchedStatements=true
将插入批处理到临时表中,然后使用单个UPDATE语句更新主表,而不是批处理单个更新。在我的机器上,使用本地MySQL实例,以下代码大约需要2.5秒

longt0=System.nanoTime();
连接设置自动提交(错误);
字符串sql=null;
sql=“更新个人信息集结果=?,分数=?,上载状态=?其中CandidateID=?”;
PreparedStatement ps=conn.prepareStatement(sql);
String tag=“X”;

对于(inti=1;我尝试调用
executeBatch()
for
循环之后。我曾尝试在for循环外部调用它,但它只更新了最后一条记录。为什么在提交后将autoCommit设置为true?因为我最初将其设置为false,所以最后将其默认为true。您还需要移动
pstm=(PreparedStatement)myConn.prepareStatement(sql)
跳出循环…这只是将最后一条记录更新到数据库中。@JayeshB-我认为您弄错了。请重试。并删除不必要的
设置自动提交(true)
最后。感谢您提供的代码,这有助于减少一些时间。更新2000条记录仍需要大约5秒的时间。500可能不合适,您需要测试。更合适的计数这是迄今为止最好的解决方案。上载2000条记录花费了1234毫秒。感谢您的帮助。