【问题标题】:Parse large text files and move the data into a database解析大型文本文件并将数据移动到数据库中
【发布时间】:2015-03-30 00:20:16
【问题描述】:

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

总共 10 毫秒,这对于该文件包含大约 1000 万行而言确实非常重要。

我使用PreparedStatement 进行查询。

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

【问题讨论】:

    标签: java stored-procedures javadb dbconnection


    【解决方案1】:

    你打开了自动提交吗?

    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

    【讨论】:

    • 我的朋友你是我的英雄:)
    【解决方案2】:

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

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

    【讨论】:

    • 他/她明确表示:“进入 Derby 数据库”。 Apache Derby 不是 SQLite。
    • 啊哈,抱歉,正在浏览安卓问题,不知道我是怎么来到这里的:/
    • 我如何使用 BufferedInputStream 但感谢您指出这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多