在Java中更快地将txt文件读取到mySQL数据库

在Java中更快地将txt文件读取到mySQL数据库,java,mysql,performance,readfile,text-parsing,Java,Mysql,Performance,Readfile,Text Parsing,我试图读取17000多个文件(每个文件包含100到23000行)并将数据解析到mysql数据库中。问题是它做得太慢了,我不知道瓶颈在哪里 private void readFile() { PreparedStatement prepStatement = null; String queryInsItem = "INSERT IGNORE INTO item VALUES(?)"; String queryInsUser = "INSERT IGNORE INTO

我试图读取17000多个文件(每个文件包含100到23000行)并将数据解析到mysql数据库中。问题是它做得太慢了,我不知道瓶颈在哪里

private void readFile() { 
    PreparedStatement prepStatement = null;

    String queryInsItem = "INSERT IGNORE INTO item VALUES(?)";

    String queryInsUser = "INSERT IGNORE INTO user VALUES(?)";

    String queryInsRating = "INSERT IGNORE INTO rating VALUES(?,?,?,?)";

    try {
        int x = 1;
        int itemID = 0;
        int userID = 0;
        int rating = 0;
        java.util.Date date = null;
        java.sql.Date sqlDate = null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        String line = null;

        conn.setAutoCommit(false);
        System.out.println("Loading...");
          File dir = new File("src/bigdata/training_set/");
          File[] directoryListing = dir.listFiles();
          if (directoryListing != null) {
            for (File itemFile : directoryListing) {
                in = new BufferedReader(new FileReader(itemFile));
                line = in.readLine();
                itemID = Integer.parseInt(line.substring(0, line.length()-1));
                userID = 0;
                rating = 0;
                date = null;
                sqlDate = null;

                // Add to item table
                prepStatement = conn.prepareStatement(queryInsItem);
                prepStatement.setInt(1, itemID);
                prepStatement.executeUpdate();
                conn.commit();
                prepStatement.close();


                while ((line = in.readLine()) != null) {
                    // Splits the line to corresponding variables
                    userID = Integer.parseInt(line.substring(0, line.indexOf(",")));
                    rating = Integer.parseInt(line.substring(line.indexOf(",")+1, line.lastIndexOf(",")));
                    date= dateFormat.parse(line.substring(line.lastIndexOf(",")+1, line.length()));

                    sqlDate = new java.sql.Date(date.getTime());

                    // Add to user table
                    prepStatement = conn.prepareStatement(queryInsUser);
                    prepStatement.setInt(1, userID);
                    prepStatement.executeUpdate();
                    conn.commit();
                    prepStatement.close();

                    // Add to rating table
                    prepStatement = conn.prepareStatement(queryInsRating);
                    prepStatement.setInt(1, userID);
                    prepStatement.setInt(2, itemID);
                    prepStatement.setInt(3, rating);
                    prepStatement.setDate(4, sqlDate);
                    prepStatement.executeUpdate();
                    conn.commit();
                    prepStatement.close();

                }
                in.close();
                System.out.println("File " + x++ +" done.");
            }
          }


    } catch (IOException | ParseException | SQLException e) {e.printStackTrace();}

    System.out.println("Done.");
}
我尝试先str.split行,然后将其更改为indexOf/lastIndexOf,但没有如中所述的明显改进。同一线程中的其他人提到要使用线程,但在我的情况下,这是正确的方法吗

以下是原始数据的片段:

5317:
2354291,3,2005-07-05
185150,2,2005-07-05
868399,3,2005-07-05
以上是指:

[item_id]:
[user_id],[rating],[date]
[user_id],[rating],[date]
[user_id],[rating],[date]
  • 如果这些表上有
    AUTO_INCREMENT
    PRIMARY KEY
    ,请注意
    INSERT IGNORE
    会疯狂地烧掉ID
  • “批量”插入。如果收集100-1000行,用这些行构建一个INSERT,然后执行该语句,插入将以10倍的速度运行
  • 不要试图一次批处理23000行,您可能会遇到一些问题(很难预测是什么问题)
  • 另一方面,如果您可以对这些文件执行
    加载数据
    ,那么就可以删除所有解析代码!它的运行速度至少与批处理插入一样快
      • 如果这些表上有
        AUTO_INCREMENT
        PRIMARY KEY
        ,请注意
        INSERT IGNORE
        会疯狂地烧掉ID
      • “批量”插入。如果收集100-1000行,用这些行构建一个INSERT,然后执行该语句,插入将以10倍的速度运行
      • 不要试图一次批处理23000行,您可能会遇到一些问题(很难预测是什么问题)
      • 另一方面,如果您可以对这些文件执行
        加载数据
        ,那么就可以删除所有解析代码!它的运行速度至少与批处理插入一样快
          • 如果这些表上有
            AUTO_INCREMENT
            PRIMARY KEY
            ,请注意
            INSERT IGNORE
            会疯狂地烧掉ID
          • “批量”插入。如果收集100-1000行,用这些行构建一个INSERT,然后执行该语句,插入将以10倍的速度运行
          • 不要试图一次批处理23000行,您可能会遇到一些问题(很难预测是什么问题)
          • 另一方面,如果您可以对这些文件执行
            加载数据
            ,那么就可以删除所有解析代码!它的运行速度至少与批处理插入一样快
              • 如果这些表上有
                AUTO_INCREMENT
                PRIMARY KEY
                ,请注意
                INSERT IGNORE
                会疯狂地烧掉ID
              • “批量”插入。如果收集100-1000行,用这些行构建一个INSERT,然后执行该语句,插入将以10倍的速度运行
              • 不要试图一次批处理23000行,您可能会遇到一些问题(很难预测是什么问题)
              • 另一方面,如果您可以对这些文件执行
                加载数据
                ,那么就可以删除所有解析代码!它的运行速度至少与批处理插入一样快

              MySQL本机支持将文本文件加载到数据库表中,通常比使用insert语句编写代码快得多:也许您更适合将文件转换为可以通过LOAD DATA INFILE加载到MySQL中的格式。根据我的经验,它的速度更快。MySQL本机支持将文本文件加载到数据库表中,并且通常比使用insert语句编写代码快得多:也许您更适合将文件转换为可以通过LOAD DATA INFILE加载到MySQL中的格式。根据我的经验,它的速度更快。MySQL本机支持将文本文件加载到数据库表中,并且通常比使用insert语句编写代码快得多:也许您更适合将文件转换为可以通过LOAD DATA INFILE加载到MySQL中的格式。根据我的经验,它的速度更快。MySQL本机支持将文本文件加载到数据库表中,并且通常比使用insert语句编写代码快得多:也许您更适合将文件转换为可以通过LOAD DATA INFILE加载到MySQL中的格式。根据我的经验,速度更快。我选择了加载数据方法。现在至少快60%!非常感谢。我选择了加载数据方法。现在至少快60%!非常感谢。我选择了加载数据方法。现在至少快60%!非常感谢。我选择了加载数据方法。现在至少快60%!非常感谢。