【问题标题】:Get fast resultset with mysql jdbc使用 mysql jdbc 快速获取结果集
【发布时间】:2019-08-26 06:50:25
【问题描述】:

我有一个包含 6001215 行数据的 lineitem 表,我想快速获取此查询 SELECT * FROM LINEITEM 的结果,然后处理内存中结果集中的数据。 我有这段代码,但还是很慢。

           String Query = "SELECT * FROM LINEITEM";
           Properties p = new Properties();
           p.setProperty("user", "root");
           p.setProperty("password", "root");
           p.setProperty("MaxPooledStatements", "10000");
           p.setProperty("cachePrepStmts", "true");
           p.setProperty("useServerPrepStmts", "true");
           Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/tpch",p);
           conn.setAutoCommit(false);
           PreparedStatement preparedStatement = conn.prepareStatement(Query);
           resultSet = preparedStatement.executeQuery(Query);

       } catch (Exception e) {
           System.err.println("Got an exception! ");
           System.err.println(e.getMessage());
       }

【问题讨论】:

  • 您真的想将 600 万行拉入内存吗?你在哪里实际处理结果集
  • 你应该打电话给preparedStatement.executeQuery()而不是preparedStatement.executeQuery(Query)
  • 我需要将结果集所有结果供以后使用。除了留在记忆里,还有别的办法吗?
  • 这真的取决于你想要做什么
  • 我想从每一行获取哈希码并保存

标签: java mysql resultset


【解决方案1】:

请尝试在查询运行前建立数据库连接,在一个方法中建立连接会降低性能,当你一次又一次调用该方法时,每次都会创建数据库连接并使用preparedStatement.executeQuery();而不是preparedStatement.executeQuery(Query);

使用 POJO 类实现将从数据库表中获取的 Resulset 保存到实体列表中,然后随时检索列值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 2013-06-18
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2021-11-30
    • 1970-01-01
    相关资源
    最近更新 更多