0

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

总共有 10 毫秒,这对于该文件包含大约 1000 万行来说非常重要。

我正在使用PreparedStatement查询。

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

4

2 回答 2

2

你打开了 Autocommit 吗?

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

于 2015-01-30T14:00:55.417 回答
0

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

编辑 我的不好,这个答案是针对android开发的

于 2015-01-30T13:57:00.027 回答