解析大型文本文件并将数据移动到数据库中

Parse large text files and move the data into a database

我有一个大约 1.5Gb 的相当大的文本文件。我必须逐行解析文件并将这些行插入到 Derby 数据库中。我阅读了很多关于性能和如何解析文件等的论坛。我的问题是我对我的所有进程进行了基准测试,读取和解析一行需要 1 毫秒,但我必须确保我的那一行'我尝试插入不存在,如果存在,那么我必须对其进行一些更新。这部分过程大约需要 9 毫秒。

总共 10 毫秒,这对于文件包含大约 1000 万行来说确实很多。

我正在使用 PreparedStatement 进行查询。

有什么方法可以加快代码的查询部分吗?

由于您已经在使用 SQLiteStatement,我唯一能想到的另一件事是确保您在 i/o 操作中使用 BufferedInputStream / BufferedOutputStream

编辑 不好意思,这个答案是为了 android 开发

你关闭自动提交了吗?

dbConnection.setAutoCommit(false);

使用批量插入而不是像这里这样一个一个地插入:

    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;

    String insertTableSQL = "INSERT INTO DBUSER"
            + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
            + "(?,?,?,?)";

    try {
        dbConnection = getDBConnection();
        preparedStatement = dbConnection.prepareStatement(insertTableSQL);

        dbConnection.setAutoCommit(false);

        preparedStatement.setInt(1, 101);
        preparedStatement.setString(2, "mkyong101");
        preparedStatement.setString(3, "system");
        preparedStatement.setTimestamp(4, getCurrentTimeStamp());
        preparedStatement.addBatch();

        preparedStatement.setInt(1, 102);
        preparedStatement.setString(2, "mkyong102");
        preparedStatement.setString(3, "system");
        preparedStatement.setTimestamp(4, getCurrentTimeStamp());
        preparedStatement.addBatch();

        preparedStatement.setInt(1, 103);
        preparedStatement.setString(2, "mkyong103");
        preparedStatement.setString(3, "system");
        preparedStatement.setTimestamp(4, getCurrentTimeStamp());
        preparedStatement.addBatch();

        preparedStatement.executeBatch();

        dbConnection.commit();

        System.out.println("Record is inserted into DBUSER table!");

    } catch (SQLException e) {

        System.out.println(e.getMessage());
        dbConnection.rollback();

    } finally {

        if (preparedStatement != null) {
            preparedStatement.close();
        }

        if (dbConnection != null) {
            dbConnection.close();
        }

    }

看看:https://builds.apache.org/job/Derby-docs/lastSuccessfulBuild/artifact/trunk/out/tuning/tuningderby.pdf